Goober
Goober

Reputation: 13506

WCF - Define multiple services in a single APP.Config file?

Scenario

I have a windows forms application. I want to use two different WCF Services that are in no way connected. HOWEVER, I'm not sure how to go about defining the services in my APP.CONFIG file. From what I have read, it is possible to do what I have done below, but I cannot be sure that the syntax is correct or the tags are all present where necessary and I needed some clarification.

Question.

So is the below the correct way to setup two services in A SINGLE APP.CONFIG FILE? I.E:

<configuration>
    <system.serviceModel>
        <services>
            <service>
                <!--SERVICE ONE-->
                <endpoint>
                </endpoint>
                <binding>
                </binding>
            </service>
            <service>
                <!--SERVICE TWO-->
                <endpoint>
                </endpoint>
                <binding>
                </binding>
            </service>
        </services>
    </system.serviceModel>
</configuration>

CODE

<configuration>
    <system.serviceModel>
        <services>
        <!--SERVICE ONE-->
            <service>
                <endpoint
                    address=""
                    binding="netTcpBinding"
                    bindingConfiguration="tcpServiceEndPoint"
                    contract="ListenerService.IListenerService"
                    name="tcpServiceEndPoint"
                />
                <binding
                    name="tcpServiceEndPoint"
                    closeTimeout="00:01:00"
                    openTimeout="00:01:00"
                    receiveTimeout="00:10:00"
                    sendTimeout="00:01:00"
                    transactionFlow="false"
                    transferMode="Buffered"
                    transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard"
                    listenBacklog="10"
                    maxBufferPoolSize="524288"
                    maxBufferSize="65536"
                    maxConnections="10"
                    maxReceivedMessageSize="65536"
                >
                    <readerQuotas
                        maxDepth="32"
                        maxStringContentLength="8192"
                        maxArrayLength="16384"
                        maxBytesPerRead="4096"
                        maxNameTableCharCount="16384"
                    />
                    <reliableSession
                        ordered="true"
                        inactivityTimeout="00:05:00"
                        enabled="true"
                    />
                    <security mode="None">
                        <transport
                            clientCredentialType="Windows"
                            protectionLevel="EncryptAndSign"
                        />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </service>
            <!--SERVICE TWO-->
            <service>
                <endpoint
                    address=""
                    binding="netTcpBinding"
                    contract="UploadObjects.IResponseService"
                    bindingConfiguration="TransactedBinding"
                    name="UploadObjects.ResponseService"
                />
                <binding name="TransactedBinding">
                    <security mode="None" />
                </binding>
            </service>
        </services>
    </system.serviceModel>
</configuration>

EDIT

What do the BEHAVIOURS represent? How do they relate to the service definitions?

EDIT 2

Does the Service Name need to be the same as the Binding Name?

Upvotes: 6

Views: 14899

Answers (4)

marc_s
marc_s

Reputation: 755531

You don't have your config quite right:

<configuration>
  <system.serviceModel>
     <behaviors>
        ... here you define sets of behaviors - behavior configurations
     </behaviors>
     <bindings> 
        ... here you define your binding configurations (parameters for bindings)
     </bindings> 
     <services>
        <service name="Service1"> 
          ... here you define the service endpoint which includes the ABC of WCF:
          ... (A)ddress, (B)inding, (C)ontract
        </service>
        <service name="Service2"> 
          ... here you define the service endpoint which includes the ABC of WCF:
          ... (A)ddress, (B)inding, (C)ontract
        </service>
        ....
      </services>    
  </system.serviceModel>
</configuration>

The services and service endpoints can reference behavior configurations, as well as binding configurations, by specifying the behaviorConfiguration= and bindingConfiguration= settings respectively.

You should definitely have a look at the WCF Configuration Editor tool to give you a hand at configuring your WCF services! It should be available from the Visual Studio "Tools" menu:

alt text

and it looks something like this:

alt text

Upvotes: 12

Nix
Nix

Reputation: 58632

Combine them together.

      <system.serviceModel>
          <behaviors>
            <serviceBehaviors>
              <behavior name="Service1Bevhavior">

              </behavior>
              <behavior name="Service2Bevhavior"/>
            </serviceBehaviors>
          </behaviors>

          <services>

            <!-- SERVICE ONE -->
            <service name="Service1">
              <endpoint address=""
                        binding="netTcpBinding"
                        bindingConfiguration="tcpServiceEndPoint"
                        contract="ListenerService.IListenerService"
                        name="tcpServiceEndPoint" />
            </service>     

           <!-- SERVICE TWO -->
            <service name="Service2">
              <endpoint address=""
                        binding="netTcpBinding"
                        contract="UploadObjects.IResponseService"
                        bindingConfiguration="TransactedBinding"
                        name="UploadObjects.ResponseService"/>
            </service>
          </services>
          <bindings>
            <netTcpBinding>
              <binding name="tcpServiceEndPoint" closeTimeout="00:01:00"
                      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                      transactionFlow="false" transferMode="Buffered"  transactionProtocol="OleTransactions"
                      hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
                      maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                 maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:05:00"
                 enabled="true" />
                <security mode="None">
                  <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                  <message clientCredentialType="Windows" />
                </security>
              </binding>
            </netTcpBinding>
            <netTcpBinding>
              <binding name="TransactedBinding">
                <security mode="None" />
              </binding>
            </netTcpBinding>

          </bindings>
        </system.serviceModel>

Upvotes: 4

Ranjita Das
Ranjita Das

Reputation: 443

yes both name should be same service name and binding name

Upvotes: 0

Erup
Erup

Reputation: 532

Does the Service Name need to be the same as the Binding Name?

The Service Name should be the contract implementation class. If you want to use a binding configuration, the binding config name should be the same as the bindingConfiguration setting of the endpoint.

    <configuration>

<system.serviceModel>

<services>

<!--SERVICE ONE-->
<service name="ListenerService.ListenerServiceImplementation" >
<endpoint address=""
          binding="netTcpBinding"
          bindingConfiguration="tcpServiceEndPoint"
          contract="ListenerService.IListenerService" />
<!--SERVICE TWO-->
<service name="UploadObjects.ResponseServiceImplementation" />
<endpoint address=""
          binding="netTcpBinding"
          bindingConfiguration="TransactedBinding"
          contract="UploadObjects.IResponseService" />
</services>
<bindings>

<binding name="tcpServiceEndPoint" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        transactionFlow="false" transferMode="Buffered"  transactionProtocol="OleTransactions"
        hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
        maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
         <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
         <reliableSession ordered="true" inactivityTimeout="00:05:00"
          enabled="true" />
         <security mode="None">
           <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
           <message clientCredentialType="Windows" />
         </security>
       </binding>

      <binding name="TransactedBinding">
        <security mode="None" />
      </binding>
</bindings>

</system.serviceModel>


</configuration>

Upvotes: 2

Related Questions