This sample covers the usage of the ConfigurationChannelFactory<TChannel>. The ConfigurationChannelFactory<TChannel> allows central management of WCF client configuration. This can also be useful in scenarios in which configuration is selected or changed after the application domain load time.
This sample shows how to use ConfigurationChannelFactory<TChannel> to add a particular configuration file to a client application, without having to use the default application configuration file.
The sample consists of two projects. The first project is a simple service that runs to reply to messages coming from the clients. The second project is a client application that builds two ConfigurationChannelFactory<TChannel> objects using a ExeConfigurationFileMap for the Test.config configuration file and uses them to communicate with the service. Both clients communicate with the service using the configuration specified in Test.config.
The following code adds a custom configuration file to a client application.
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();
To set up, build, and run the sample
Open Visual Studio 2012 with administrator privileges.
Right-click the ConfigurationChannelFactory solution (2 projects) and then select Properties.
In Common Properties, select Startup Project, and then click Multiple startup projects.
Move the Service project to the beginning of the list, with the Action ‘Start’, and then move the Client project after the Service project, also with the Action ‘Start’, so the Client project is executed after the Service project.
Click OK, and then press F5 (or CTRL+F5) to run the sample.
The samples may already be installed on your machine. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.