cocopan
cocopan

Reputation: 109

dynamically change web service url on a windows service using app.config

In my windows service I have 2 web references

My windows service only contains an app.config not a web.config

my app config looks like this:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyFirstWindowsService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <bindings />
        <client />
    </system.serviceModel>
    <applicationSettings>
        <MyFirstWindowsService.Properties.Settings>
            <setting name="MyFirstWindowsService_postDataToMapicsWC_XAXSSTRN"
                serializeAs="String">
                <value>http://TESTDEV/web/services/XAXSSTRN.XAXSSTRNHttpSoap11Endpoint/</value>
            </setting>
            <setting name="MyFirstWindowsService_com_goodmanmfg_testdev_GETITMINFO"
                serializeAs="String">
                <value>http://TESTDEV/web/services/GETITMINFO.GETITMINFOHttpSoap11Endpoint/</value>
            </setting>
        </MyFirstWindowsService.Properties.Settings>
    </applicationSettings>
</configuration>

[![enter image description here][1]][1]

the problem is my web services when i click on them the URL field is still saying

"Web reference URL" http://PROD/web/services/XAXSSTRN.XAXSSTRNHttpSoap11Endpoint [![enter image description here][2]][2]

what is the end goal? To release this windows service to dev and prod environments without having to rebuild the entire solution.

The ideal behavior is :

  1. Build the latest code in dev mode (test it, if all test good then , step 2)

  2. Provide an app.config file with prod urls to dropped inside the folder

  3. Release

    As you can see what I am trying to avoid is the need of dropping the file manually changing those 2 web services , rebuilding the solution AND THEN releasing...

Upvotes: 0

Views: 1568

Answers (1)

Flydog57
Flydog57

Reputation: 7111

Classic ASP.NET apps get their configuration from a hierarchy of web.config values. Other apps (console applications, Windows Forms apps, WPF, Services, ...) get their configuration from a configuration file named [NameOfExe].exe.config (and occasionally from [NameOfAssembly].dll.config. That file is located in the same folder as the exe itself.

For example, if your service is MyWcfService.exe, you will very likely find a MyWcfService.exe.config file in the same folder (for example, in the bin/debug folder). Its contents should be the same as your app.config.

Visual Studio makes this all "just work" by creating an app.config file in your source folder and then, at build time, copying the contents of that file to the appropriately named [NameOfExe].exe.config file in the same folder as the EXE.

In the normal case, you might have one set of URLs (and perhaps other data) for your dev environment, another for QA, another for Integration Test and another for Prod. You can manage this through the use of configuration transforms.

I think this goes some way towards answering your questions. In summary

  1. App.config files have nearly the same capabilities as web.config files
  2. App.config files get "compiled" to [NameOfExe].exe.config files at build time and placed in the same folder at the EXE
  3. Configuration transforms may help you out with managing your URLs

Your other choice is managing a set of [NameOfExe].exe.[Environment].config files and manually putting them in the right place.

Upvotes: 1

Related Questions