Search code examples
visual-studioweb-servicesweb-configcustom-sections

How to access web.config sections in a Web service called from another application?


I'm working on a Web service with Visual Studio, framework 4.7.1. One of its Web methods needs to call another Web service (provided by another company). It converts the parameters it receives (that are consistent with our main application's business logic) into values the other Web service can handle (according to it's own business logic). To do this, it relies heavily on data stored in the Web.config file.

I tested it directly (start the Web service and call the Web methods with automatically generated pages on a Web browser page) and everything worked fine.

Now, I need to build a test application (also in Visual Studio, framework 4.7.1) to call the same Web methods. On first testing it, I noticed that the Web service was trying to access the test application's config file instead of its own (as described in Can't read Web.config with ConfigurationManager.AppSettings ).

So I created an applicationSettings section in the Web.config and moved all the data from appSettings into it. It worked fine.

Now, however, I notice that the same thing happens with the custom sections. One of them looks like this:

<configSections>
    <section name="jobTypeLists" type="AdelSoft_WS_FRA.JobTypesSection" />
</configSections>


<jobTypeLists>
    <jobTypes>
        <jobType codeCustomerType="A" codeJobType="JobForA" />
        <jobType codeCustomerType="B" codeJobType="JobForB" />
    </jobTypes>
</jobTypeLists>

I can see how such a structure could fit into its own .settings file, but I have another one that is much more complicated. (Like, the text nodes can have up to four ancestors.) To keep this short-ish, I'm not providing it now, but it can easily be arranged.

ConfigurationManager.GetSection("jobTypeLists") returns null when called from the test application. Same with WebConfigurationManager.GetSection("jobTypeLists").

I've also tried accessing the configuration file with ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), but I can't seem to find my sections in the Configuration object it returns.

I'm not sure it means anything, but the Configuration object's FilePath property contains "C:\Folder\InnerFolder\WebServiceFolder\web.config.config". Why this second ".config"? I tried passing the same string to ConfigurationManager.OpenExeConfiguration(), without the ".config" extension: it returned null. (As it should, I feel.)

The Configuration object has 10 section groups and 22 sections, which I can't make heads or tails from. Likewise, I can list them.


Solution

  • Actually, there are two ways for a Visual Studio project to reference a Web service: as a regular reference (like you would any other project) or as a Web reference.

    I was using the former, and therein lay my mistake.

    To reference the Web service, I started it, copied the URL from the browser window that it opened, and pasted it into the "URL" text box in the "Add a Web reference" window from my test application. From there on, it worked fine.

    (By the way, I have kept the regular reference as well, because I'm using some constants from the Web service to handle return values.)