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

Goo*_*ber 6 service wcf

脚本

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

题.

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

<configuration>
    <system.serviceModel>
        <services>
            <service>
                <!--SERVICE ONE-->
                <endpoint>
                </endpoint>
                <binding>
                </binding>
            </service>
            <service>
                <!--SERVICE TWO-->
                <endpoint>
                </endpoint>
                <binding>
                </binding>
            </service>
        </services>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

码

<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>
Run Code Online (Sandbox Code Playgroud)

编辑

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

编辑2

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

mar*_*c_s 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>
Run Code Online (Sandbox Code Playgroud)

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

您一定要查看WCF配置编辑器工具,以便配置您的WCF服务!它应该可以从Visual Studio"工具"菜单中获得:

替代文字

它看起来像这样:

替代文字

  • 对不起,投票不够我说谢谢! (4认同)

Nix*_*Nix 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>
Run Code Online (Sandbox Code Playgroud)