views:

67

answers:

3

Can WCF sevice and client share the same settings (from the same config file) about bindings, etc.., whatever is in? In other words, can I write a single bindings section and put in anything and be sure that it is good for service and client?

I'll explain better. I have a config file like this:

<services>
  <service name="TestClass1">
    <endpoint binding="basicHttpBinding" address="http://dev00:4322/host1/TestApplication1" contract="myApp.Interface.ITestApplication"/>
    <endpoint binding="netTcpBinding" bindingConfiguration="Binding1" address="net.tcp://dev00:4321/host1/TestApplication1" contract="myApp.Interface.ITestApplication"/>
    <endpoint binding="netNamedPipeBinding" address="net.pipe://localhost/host1/TestApplication1" contract="myApp.Interface.ITestApplication"/>
  </service>

  <service name="ManagementClass1">
    <endpoint binding="netNamedPipeBinding" address="net.pipe://localhost/host1/ManagementApplication1" contract="myApp.Interface.IManagementApplication"/>
    <endpoint binding="netTcpBinding" bindingConfiguration="Binding1" address="net.tcp://dev00:4321/host1/ManagementApplication1" contract="myApp.Interface.IManagementApplication"/>
  </service>
</services>

<client>
  <endpoint name="clientTestClass1Tcp"
   address="net.tcp://dev00:4321/host1/TestApplication1"
   binding="netTcpBinding" 
   bindingConfiguration="Binding1" 
   contract="myApp.Interface.ITestApplication"/>

  <endpoint name="clientManagementClass1Tcp"
   address="net.tcp://dev00:4321/host1/ManagementApplication1"
   binding="netTcpBinding" 
   bindingConfiguration="Binding1" 
   contract="myApp.Interface.IManagementApplication"/>
</client>

<bindings>
  <netTcpBinding>
    <binding name="Binding1" 
         closeTimeout="00:00:10"
         openTimeout="00:00:10" 
         receiveTimeout="00:01:00" 
         sendTimeout="00:01:00"
         transactionFlow="false" 
         transferMode="Buffered" 
         transactionProtocol="OleTransactions"
         hostNameComparisonMode="StrongWildcard" 
         listenBacklog="10"
         maxBufferPoolSize="524288" 
         maxBufferSize="65536" 
         maxConnections="30"
         maxReceivedMessageSize="65536">
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </netTcpBinding>
</bindings>

where not all is under my control. Can I be sure that sharing the bindings (and other sections..) between service and client, whatever gets written, all goes well both in service and in client?

+3  A: 

Yes, you can - to a certain degree:

  • put your binding, behavior, extension information into separate config files
  • reference those from both the client and the server part of your app

I.e. put your bindings in bindings.config:

<?xml version="1.0" encoding="utf-8"?>
<bindings>
  <basicHttpBinding>
    <binding name="Default" useDefaultWebProxy="false">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" 
                   proxyCredentialType="None" realm="" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

and then reference that file from your service's app.config or web.config:

<system.serviceModel>
    <bindings configSource="bindings.config" />
</system.serviceModel>

Visual Studio will complain about the "configSource" - but trust me, it WORKS. It's a flaw in the Visual Studio XML schema used for validation - but the feature works. This actually works for any configuration section (but not for configuration section groups) in your web.config / app.config.

You can do this for any of the "subsections" of the <system.serviceModel> configuration group - client, server, behaviors, extensions, you name it.

Marc

marc_s
A: 

Yes. Here's the (simplified) app.config file I use.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <client>
            <endpoint name="MyServiceClient"
                address="net.pipe://localhost/MyService"
                contract="IMyService"
                binding="netNamedPipeBinding" />
        </client>
        <services>
            <service name="MyService">
                <endpoint name="MyService"
                    address="net.pipe://localhost/MyService"
                    contract="IMyService"
                    binding="netNamedPipeBinding" />
            </service>
        </services>
    </system.serviceModel>
</configuration>
Matt Davis
A: 

Another option if you control the code at both ends is to do the entire configuration in code apart from the name of the server that you read from your own config file.

You can then use the assembly in both the client and the server; this can work well when you are using a shared assembly (rather than generated proxy classes) to define the WCF interfaces already.

Ian Ringrose