2

.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?

Gauls
  • 1,955
  • 6
  • 28
  • 44
  • I have worked in such a kind of scenario and I suggest it is not doable. Let the comments flood in and will have more information. – CodeSpread Nov 29 '12 at 12:10
  • What is that odd looking `` in the middle of your `web.config / ` - please remove that. Also: the `` in your `web.config` as well as the `` tag in your `client.config` aren't properly closed .... – marc_s Nov 29 '12 at 14:26

3 Answers3

1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I think you have explained how we can have different configs for different sections. But here the question is adding endpoints in app.config. – CodeSpread Nov 29 '12 at 12:14
  • @CodeSpread: you can have this `` in your `app.config` (or `web.config`) and put the actual client configuration into an external file, that can be shared amongst multiple projects.... – marc_s Nov 29 '12 at 12:28
  • you are correct. but can you please answer me about the original question "adding endpoints in app.config" in a different project when you have web.config available. – CodeSpread Nov 29 '12 at 13:07
  • @CodeSpread: as I'm already mentioning in my answer - you cannot add configuration to a **class-library `app.config`** since .NET doesn't look at those - that's absolutely correct – marc_s Nov 29 '12 at 13:11
  • Ok. Actually i have already answered the same in the very first comment.Then after seeing your answer, I thought you are suggesting a way to do that.--codespread – CodeSpread Nov 29 '12 at 13:15
  • @ marc_s : Alright so are you saying i can create client.config in the common project? – Gauls Nov 29 '12 at 13:40
  • @Gauls: yes - you just need to make sure all projects that need it have a current copy of it sitting next to their own `web.config` (e.g. by copying around the config file, or by establishing "symbol links" with that file - whatever). – marc_s Nov 29 '12 at 13:45
  • @marc_s : I created client.config in common proj and rest of bit is in web.config . Also added client.config within each project as "add as link". when i run web app it errors saying "unable to open configSource file client.config "?? – Gauls Nov 29 '12 at 14:02
  • @Gauls: do you **copy** the `client.config` to your **output directory** where the `web.config` exists at runtime?? – marc_s Nov 29 '12 at 14:02
  • @marc_s: I have selected to copy to output directory for common and web projects still same error :( – Gauls Nov 29 '12 at 14:09
  • @Gauls: can you upload your `web.config` and a screenshot of your web application's main directory (where `web.config` is located)? Here inside your question, or to some other site where we can go have a look at it .... – marc_s Nov 29 '12 at 14:11
0

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

CodeSpread
  • 327
  • 2
  • 5
0

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

Community
  • 1
  • 1
Rich C
  • 802
  • 2
  • 13
  • 33