WCF - 在单个APP.Config文件中定义多个服务?

时间:2010-04-06 12:15:22

标签: wcf service

方案

我有一个Windows窗体应用程序。 我想使用两个不同的WCF服务。 但是,我不知道如何在我的APP.CONFIG文件中定义服务。 根据我的阅读,可以做我在下面所做的事情,但我无法确定语法是否正确或标签是否在必要时都存在,我需要澄清。

问题。

以下是在单个APP.CONFIG文件中设置两个服务的正确方法吗? 即:

<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>

修改

BEHAVIORS代表什么? 它们如何与服务定义相关?

编辑2

服务名称是否需要与绑定名称相同?

4 个答案:

答案 0 :(得分:12)

你没有正确的配置:

<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>

服务和服务端点可以分别指定behaviorConfiguration=bindingConfiguration=设置来引用行为配置和绑定配置。

您一定要查看WCF Configuration Editor tool,以便配置您的WCF服务!它应该可以从Visual Studio“工具”菜单中获得:

alt text

它看起来像这样:

alt text

答案 1 :(得分:4)

将它们组合在一起。

      <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>

答案 2 :(得分:2)

  

服务名称是否必须是   与绑定名称相同吗?

服务名称应为合同实施类。如果要使用绑定配置,则绑定配置名称应与端点的bindingConfiguration设置相同。

    <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>

答案 3 :(得分:0)

是的,这两个名称应该是相同的服务名称和绑定名称