部署使用netTcpBinding的WCF服务(不是IIS托管)

时间:2012-10-22 01:49:46

标签: wcf wcf-binding wcf-endpoint

我正在通过netTcpBinding使用WCF开发客户端 - 服务器应用程序。

我的解决方案有2个项目,客户端1和服务器1。

到目前为止,我已经了解到为了使WCF服务正常工作,我必须对app.config文件进行一些配置。我这样做了,事情很顺利。

但是现在我在将服务部署到服务器时找出要做什么的问题,以便客户端连接。我的问题是,当我将服务部署到“localhost”以外的位置时,我不知道我在app.config(或任何其他位置)中需要修改的内容。

这是我的服务器应用程序的app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MMServidor3.ServidorCliente">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="MMServidor3.iServicioMM">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:4005/MMServidor3/" />
            <add baseAddress="net.tcp://localhost:4006/MMServidor3/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

“mex”端点和http协议基地址是为了让客户端获取元数据(否则无法获取)。

所以,既然我事先不知道IP地址是什么,我将部署服务器,我希望从配置或ini文件中读取端点(我宁愿不必编译每个端点)。

另外,我在客户端需要修改什么?以下是客户端app.config的相关部分:

<system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_iServicioMM" 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:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:4006/MMServidor3/" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_iServicioMM" contract="MMServicioServidor.iServicioMM"
                name="NetTcpBinding_iServicioMM">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:3)

配置文件特定于环境。通常,您将(如果需要)将服务地址中的localhost位更改为IP地址或名称。

对于客户来说,它是一样的。应在其生产环境中配置生产客户端以使用生产服务的URL。当然,在知道服务的位置*之前,您无法配置客户端。

不幸的是,您必须单独指定每个客户端端点的URL,有关详细信息,请查看this related question。您可以解决的一个替代方法是为客户端“BaseAddress”实现您自己的设置,并以编程方式specify endpoint addresses使用它。


*如果您在查找服务和客户方面有一些更极端的灵活性,可以查看WCF Discovery mechanisms


**另外请注意,如果您希望自动为不同的环境创建配置文件,我建议您使用(例如)SlowCheetah