How to configure WCF in a separate dll project

I am developing a web application (ASP.NET 3.5) that will consume several web services. I created a separate DLL project for each web service: these projects contain a link to the service and client code.

However, the calling website MUST have the <system.serviceModel> ( <bindings> and <client> nodes) in it web.config, although this information is also in the app.config dll file! I tried to copy the serviceclass.dll.config file to the bin directory on the website, but that did not help.

Is there a way to centralize the WCF client configuration?

+7
wcf service-reference wcf-configuration
source share
6 answers

I limited only the WCF experience, all with bindings to BasicHTTP. But I am allergic to WCF xml files and still managed to avoid them. I do not recommend this at all, but I put the configuration details into my existing application configuration store and then applied them programmatically. For example. With the web service proxy, I use the constructor for the Client, which accepts the "bindings" and the "endpoint" and programmatically applies the settings to the bindings and the endpoint.

A more elegant solution seems to be described here: Reading the WCF configuration from a user location , but I haven't tried it yet.

+4
source share

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

That way you can really delete the file because it is not in use. Instead, the library host configuration is read, so this is the only place where the endpoint and binding configuration should be.

+4
source share

You can abandon the xml configuration and create the binding and endpoint classes associated with the service in the constructor or the custom Factory Service. IDesign has some good information about this: http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11 (See In Proc Factory)

In your approach, you set the attributes of your services to determine at a high level how they should work (for example, [Internet], [Intranet], [BusinessTobusiness]), and the factory service sets up the service according to best practices for each scenario. Their book describes the creation of these types of services: http://www.amazon.com/Programming-WCF-Services-Juval-Lowy/dp/0596526997

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

+3
source share

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

If you want to centralize your web configurations, I would suggest you explore them in IIS using virtual directories. This will allow you to use configuration inheritance to centralize everything you need.

+3
source share

There are 2 options.

Option 1. Work with channels.

If you work directly with channels, .NET 4.0 and .NET 4.5 have a ConfigurationChannelFactory . An example MSDN is as follows:

 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 Langdon noted, you can use the endpoint address from the configuration file by simply passing zero, for example:

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

This is discussed in the MSDN documentation.

Option 2. Work with a proxy.

If you are working with proxy-generated code, you can read the configuration file and download ServiceModelSectionGroup . There is a bit more work than just using ConfigurationChannelFactory , but at least you can continue to use the generated proxy (which under the hood uses ChannelFactory and manages IChannelFactory for you.

Pablo Cibraro shows a good example of this here: Getting WCF bindings and behavior from any configuration source

+1
source share

First of all, class libraries (DLLs) do not have their own configuration, however they can read their host configuration (Web / Executable, etc.). However, I still support the app.config file in library projects as a template and a simple link.

Regarding the configuration of the service itself, the WCF configuration can make someone easily pull their hair out. This is too complex a design. The goal of your applications should be to be least dependent on configuration, while maintaining the flexibility of deployment scenarios that your product will encounter.

0
source share

All Articles