Coffka
Coffka

Reputation: 759

WCF: modifying baseAddress during setup or at the runtime

I have the win-service that hosts the WCF-service. The Win-service is running on computer "MyComp1". The WCF-service App.config looks like:

      <baseAddresses>
        <add baseAddress="http://localhost:8732/MyService" />
      </baseAddresses>

When I'm trying to import WSDL from that service (for example using Delphi WSDLImp.exe) I'm getting errors like "Couldn't import http://localhost:8732/MyService?xsd=xsd0" And it is right behavior cause the service isn't running on localhost. But locations of XSDs in generated WSDL contain localhost-like addresses.

Now I want to modify baseAddress during setup or at the runtime because I don't want users to manually edit App.config. I've heard about FlatWSDL but are there any other techniques to do this?

Upvotes: 0

Views: 882

Answers (1)

John Isaiah Carmona
John Isaiah Carmona

Reputation: 5366

You can use the System.Xml.XmlDocument to programatically change your App.config file.

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

xmlDoc.SelectNodes("/configuration/system.serviceModel/services/service/host/baseAddresses/add")
    .Cast<XmlNode>().ToList()
    .ForEach(o => o.Attributes["baseAddress"].Value = "http://localhost:8732/MyService");

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

Just make sure to use the correct XPath expression of your base address. Hope this helps.

Upvotes: 3

Related Questions