使用VS“添加服务引用”的方式,来使用WCF服务,对于服务器端的配置需要添加mex endpoint
<endpoint address="mex" binding="mexTcpBinding" name="MEX" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8123/GetSimService"/>
</baseAddresses>
</host>
如果移除,使用VS添加服务引用时会出现“元数据包含无法解析的引用”的异常,导致添加服务失败。
对于启用WCF服务时出现端口被占用的情况,除了排除有其他的程序占用端口的情况,还要注意是否是因为WCF配置的问题,
另外对于不同版本的windows系统,服务器端的配置有所不同:
对于xp sp3 x86,listenBacklog="10"和maxConnections="10"属性不能被更改,如果能改之后会出现:
未处理 System.ServiceModel.AddressAlreadyInUseException
Message=已有针对 IP 终结点 0.0.0.0:8123 的侦听器。
请确保没有在应用程序中多次尝试使用此终结点,也没有其他应用程序在侦听此终结点。
如下图所示,让listenBacklog,maxConnections element保持默认值则不会出现异常。
<bindings>
<netTcpBinding>
<!--<binding name="tcpbinding" transactionFlow="true"/>-->
<binding name="tcpbinding" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false"
transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<bindings>
对于windows 2008 R2 x64,要移除listenBacklog、maxConnections这两个属性,否则同样会出现System.ServiceModel.AddressAlreadyInUseException异常,启用服务时,出现端口被占用的情况。
<bindings>
<netTcpBinding>
<!--<binding name="tcpbinding" transactionFlow="true"/>-->
<binding name="tcpbinding" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false"
transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
从这些天学习WCF经验,由于接触时间不长,出现了各种各样的错误,到最后都是因为WCF的配置错误导致的,应该引以为鉴。