Creating a WCF Client Part 3: Adding additional services to your WCF Client App
The previous articles (Article 1, Article 2) describe how to create a WCF client for one service. In this article I will show you how to add multiple services by way of the app.config file.
Notes:
- app.config file
- WCF Endpoints: Addresses, Bindings, and Contracts - Microsoft
- Configuring Services Using Configuration Files - Microsoft
In order to get web service availability check working in my diagnostic program, I had to be able to monitor multiple web services. The simplest way to do this is by altering your existing WCF Client project to add:
- The new Communication Class (generated by svcutil)
- New configuration sections to the app.config file (pulled from new auto-generated file)
Here are the steps as I implemented them in my application:
- Use svcutil.exe to generate the class & config file for the additional service(s) you wish to use
- Open your WCF Client project in Visual Studio
- Right-click on the Project Name and select Add -> Existing Files…
- Select the .cs and .config files (be sure that the new .config file is named something other than app.config)
- In Program.cs (or equivalent if you have a non-command line project) add a reference to the new WebServices namespace so the top of the file looks something like this:
- Open the app.config file of the WCF project. It should be chock full of XML goodness.
- In the app.config file you will need to add the appropriate sections to configure the other service. Specifically, you will need to do 2 things:
- Now that you have made the changes to app.config file and the using section of the Program.cs (or equivalent) file, you are ready to instantiate an instance of your new Client object.
For example, you could have something similar to this:
// Instantiate a client object
SomeOtherServiceClient client2 = new SomeOtherServiceClient();
// Open it
client2.Open();
// Perform some operations
client2.SomeMethodToRetrieveData();
// Close the client object
client2.Close();At this point I would recommend setting a break point on the line where you Open the connection and step through the code to ensure you understand how the service works
Note: If you run into problems, see the "Additional Notes" section from Article #2 in this series
// Hypothetical pre-existing service reference
using SomeService.ServiceClientType1;
// New service reference
using SomeOtherService.ServiceClientTypeDifferent;
- Add the ServiceBinding to the appropriate binding section (in this example, both services are of type basicHttpBinding)
- Add the Endpoint Address to the client section of the XML
Here is an example of what the configuration file would look like before modification (when it houses config info for a single service):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISomeServices"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://someservices.mydomain.com:809/Services/SomeServices.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ISomeServices"
contract="ISomeServices"
name="BasicHttpBinding_ISomeServices" />
</client>
</system.serviceModel>
</configuration>
And here is an example of how that same configuration file would appear after appending the appropriate sections to the file (The new sections are Bolded for easy discernment):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<!-- SomeService Binding -->
<binding name="BasicHttpBinding_ISomeServices"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
<!-- SomeOtherService Binding -->
<binding name="BasicHttpBinding_ISomeOtherServices"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding> </basicHttpBinding>
</bindings>
<client>
<!-- This is the Endpoint for SomeService -->
<endpoint address="https://someservices.mydomain.com:809/Services/SomeServices.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ISomeServices"
contract="ISomeServices"
name="BasicHttpBinding_ISomeServices" />
<!-- This is the Endpoint for SomeOtherService -->
<endpoint address="https://someservices.mydomain.com:809//Services/SomeOtherServices.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ISomeOtherServices"
contract="IDeepsightServices"
name="BasicHttpBinding_ISomeOtherServices" />
</client>
</system.serviceModel>
</configuration>