How to configure WCF in a separate dll project
Asked Answered
N

6

7

I'm developing a web application (ASP.NET 3.5) that will consume a number of web services. I have created a separate dll-project for each web service: these projects contains the service reference and client code.

However, the calling website MUST have the <system.serviceModel> information (the <bindings> and <client> nodes) in it's web.config, even though this information is also in the dll's app.config file! I have tried copying the serviceclass.dll.config over to the bin directory of the website, but this didn't help.

Is there any way to centralize the configuration of a WCF client?

Napier answered 2/2, 2009 at 11:37 Comment(1)
For anyone searching this up, take a look at this answer: https://mcmap.net/q/270626/-wcf-how-can-i-programmatically-recreate-these-app-config-valuesCouture
M
4

I've only limited WCF experience, all with BasicHTTP bindings. But I'm allergic to WCF's xml files and have managed to avoid them thus far. I don't recomend this generally but I put the configuration details in my apps existing configuration store and then apply them programatically. E.g. With a Web service proxy I use the constructor for the Client that takes 'bindings'and 'endpoint' and programatically apply the settings to the bindings & endpoint.

A more elegent solution appears to be descibed here: Reading WCF Configuration from a Custom Location, but I haven't tried it yet.

Mornay answered 2/2, 2009 at 12:47 Comment(1)
Thank you. I've tried to code in the link you've supplied and it does exactly what I want.Napier
C
4

From my experience, library projects never read app.config.

So you can really delete the file because it is not used. The library's host configuration is read instead, so that is the only place the endpoint and binding configuration should be.

Consuelaconsuelo answered 2/2, 2009 at 13:57 Comment(1)
exactly - if you have a DLL that's hosted / used by an app, only the app's config will matter. That's a .NET basic design decision. That's why the settings in the separate project DLL's app.config will not be used AT ALL.... :-(Milliemillieme
V
3

It's possible to forgo xml config and build up the Binding and Endpoint classes associated with the service in the constructor or a custom "Service Factory". iDesign has some good information on this: http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11 (See In Proc Factory)

In their approach, you set attributes on your services to specify at a high level how they should work (ie [Internet], [Intranet], [BusinessToBusiness]), and the service factory configures the service according to best practices for each scenario. Their book describes building this sort of service: http://www.amazon.com/Programming-WCF-Services-Juval-Lowy/dp/0596526997

If you just want to share configuration XML config, maybe use the configSource attribute to specify a path for configuration: http://weblogs.asp.net/cibrax/archive/2007/07/24/configsource-attribute-on-system-servicemodel-section.aspx

Venereal answered 2/2, 2009 at 13:35 Comment(0)
I
3

Remember that a configuration file is is read by an executable that has an entry point. A library dll does not have an entry point so it is not the assembly that will read it. The executing assembly must have a configuration file to read.

If you would like to centralize your web configs then I would suggest you look into nesting them in IIS with virtual directories. This will allow you to use the configuration inheritance to centralize whatever you need.

Irate answered 2/2, 2009 at 14:10 Comment(0)
C
1

There are 2 options.

Option 1. Working with channels.

If you are working with channels directly, .NET 4.0 and .NET 4.5 has the ConfigurationChannelFactory. The example on MSDN looks like this:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "Test.config";
Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration(
    fileMap,
    ConfigurationUserLevel.None);

ConfigurationChannelFactory<ICalculatorChannel> factory1 = 
    new ConfigurationChannelFactory<ICalculatorChannel>(
        "endpoint1", 
        newConfiguration, 
        new EndpointAddress("http://localhost:8000/servicemodelsamples/service"));
ICalculatorChannel client1 = factory1.CreateChannel();

As pointed out by Langdon, you can use the endpoint address from the configuration file by simply passing in null, like this:

var factory1 = new ConfigurationChannelFactory<ICalculatorChannel>(
        "endpoint1", 
        newConfiguration, 
        null);
ICalculatorChannel client1 = factory1.CreateChannel();

This is discussed in the MSDN documentation.

Option 2. Working with proxies.

If you're working with code-generated proxies, you can read the config file and load a ServiceModelSectionGroup. There is a bit more work involved than simply using the ConfigurationChannelFactory but at least you can continue using the generated proxy (that under the hood uses a ChannelFactory and manages the IChannelFactory for you.

Pablo Cibraro shows a nice example of this here: Getting WCF Bindings and Behaviors from any config source

Celisse answered 16/4, 2013 at 19:14 Comment(0)
A
0

First of all class libraries (DLLs) do not have their own configuration, however they can read the configuration of their host (Web/Executable etc.). That being said, I still maintain an app.config file on the library projects as a template and easy reference.

As far as the service configuration itself is concerned, WCF configuration can make somebody easily pull their hair out. It is an over-engineered over-complicated piece. The goal of your applications should be to depend least on the configuration, while maintaining flexibility of deployment scenarios your product is going to come across.

Advance answered 11/1, 2013 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.