Question

.net 4.0

I have two web projects using their own web.config file and using the same endpoints to call the same WCF web service.

Both use a common project (dll) which has an app.config file. I would like to move the endpoint info into that config file - is this possible? I want endpoint information

<client>  
    <endpoint/> 
<client/> 

to be shared by both web app from common place?

Web.config file binding info omitted here

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <system.serviceModel>
      <bindings/>          
      <client configSource="client.config">
   <system.serviceModel/>
</configuration>

client.config file within common project

 <client>
      <endpoint
        name="endpoint1"
        address="http://localhost/ServiceModelSamples/service.svc"
        binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_IHello"
        behaviorConfiguration="IHello_Behavior"
        contract="IHello"/ >

Apology for typo two things i changed 1. client.config had a element took that out. 2. within the build properties of project added COPY "$(TargetDir)*.config" "$(ProjectDir)"

Just to mention both in web.config (configSource)& client.config VStudio shouts design time but runs fine?

Was it helpful?

Solution

You can share parts of the WCF config by "outsourcing" it:

<system.serviceModel>
    <client configSource="client.config" />
    .....
</system.serviceModel>

and then have this content in your client.config:

<?xml version="1.0" encoding="utf-8"?>
<client>
    <endpoint name="ABC"
        address="http://......."
        binding="......."
        contract="......" />
</client>

You cannot put stuff into the app.config of a common assembly, since those config files will not be used / looked at by the .NET framework. You need to put your info into the main config file of the app or web site (web.config) - but you can "externalize" certain sections into separate, external files and sharing them like that.

OTHER TIPS

I have worked in such a kind of scenario and I suggest it is not doable.

An alternative is to use a custom ChannelFactory class and then specify the client.config path by injection or by default rules.

Here is how to write the custom ChannelFactory that accepts a custom configuration path: http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx

I used this.GetType.Assembly.GetExecutingAssembly().ManifestModule.Name + ".config" to dynamically get a default config file that matches the name of my shared library.

You can have both of your web apps instantiate the custom ChannelFactory with the configuration path or you can add a layer of abstraction in your common library that interacts with the web service and then your web apps use the new abstract object instead of a direct dependency on the web service. This works great when you need to accommodate updates to the web service without disturbing your web apps.

More details in this SO Answer: Using ConfigurationManager to load config from an arbitrary location

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top