Search code examples
asp.netwcfiisweb-config

ASP.Net Web.config client endpoint name


I have a service that has references to other services with some of them being references to different environments of the same service (e.g. prod/test).

I am using the #if precompile directive to include different versions of these references with the using statement. Example:

#if Debug
using ServiceTest
#else
using ServiceProd
#endif

In the Web.config file I have two child nodes inside the <client> node. Example:

<client>
  <endpoint address="http://test.domain.com/Service"
    binding="basicHttpBinding" bindingConfiguration="Service"
    contract="ServiceTest" name="Service" />
  <endpoint address="http://prod.domain.com/Service"
    binding="basicHttpBinding" bindingConfiguration="Service"
    contract="ServiceProd" name="Service" />
</client>

Is the above part of the Web.config valid or not? More precisely, can there be any side-effects because of having two endpoints with the same name and binding configurations? The main concern is having a wrong endpoint called (e.g. calling prod endpoint instead of test or the other way around).

Any guidance and advice regarding the above will be really appreciated.


Solution

  • Is the above part of the Web.config valid or not?

    Every time when you run your application CLR reads web.config file and deserialize it as an object. To deserialize XML it's uses classes declaration in "configSections" section of your config file. So, the answer "valid or not" depends on implementation of "client" configuration section. I believe this should be part of your application or code from nuget library. This is why we can't answer you with confidence.

    More precisely, can there be any side-effects because of having two endpoints with the same name and binding configurations?

    Frankly, I don't understand how this should work. In what manner 3rd library should know that it needs to load first but not second client endpoint?

    Possible solutions.

    You can use web.config transformation. There are tons of resources about this feature of .NET Framework.

    1. The simplest scenario would be to substitute endpoint address keeping the same endpoint name.

    2. Another common scenario is to give different endpoint names and keep "alive" endpoint name in AppSettings. In this case your code should resolve endpoint name first and then actual endpoint address.