我有一个客户端和服务器应用程序,通过Visual Studio 2010在同一台机器上本地运行时可以正常通信。如果我编译服务器应用程序并将其移动到托管在同一台机器上的Windows XP VM并将客户端发布到它,我可以毫无问题地进行方法调用。当我将相同的服务器应用程序移动到同一网络上的单独物理机器时,我无法调用该服务。
我通过此调用在服务器中托管服务:
host = new ServiceHost(typeof(ClientHealth));
host.Open();
并使用以下代码在客户端访问它:
try
{
if (client.InnerChannel.State != System.ServiceModel.CommunicationState.Faulted)
{
client.ReportHealth(DataMinder.clientIpAddress, DataMinder.clientName, DateTime.Now);
}
else
{
client = new ClientHealth.ClientHealthClient();
client.Endpoint.Address = new EndpointAddress("net.tcp://" + DataMinder.serverIpAddress + ":" + DataMinder.serverPort + "/");
}
}
catch (Exception ex)
{
Debug.WriteLine("Failed to connect to host");
Debug.WriteLine(ex.Message);
}
我收到错误:“套接字连接已中止。这可能是由于处理消息时出错或远程主机超出接收超时或基础网络资源问题引起的。本地套接字超时为'00:04: 59.9532000' 。”
这是我的客户端app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="GenericForms.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="TournamentTracker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="connectionString" value="Server=192.168.1.7;database=tournamenttracker;uid=root;pwd=12345"/>
</appSettings>
<system.diagnostics>
<trace autoflush ="true" />
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messagelistener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\myMessages.svclog"></add>
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
logMalformedMessages="true"
maxMessagesToLog="5000"
maxSizeOfMessageToLog="2000">
</messageLogging>
</diagnostics>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IClientHealth" closeTimeout="00:01:00"
openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
maxBufferSize="1000000" maxConnections="10" maxReceivedMessageSize="1000000">
<readerQuotas maxDepth="32" maxStringContentLength="1000000" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="1000000" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<!--<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>-->
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:10001/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IClientHealth" contract="ClientHealth.IClientHealth"
name="NetTcpBinding_IClientHealth">
<identity>
<userPrincipalName value="Maelstrom\Josh" />
</identity>
</endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="DeviceHealthService.DeviceHealth" behaviorConfiguration="MetadataBehavior">
<endpoint address=""
binding="netTcpBinding"
contract="DeviceHealthService.IDeviceHealth"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:10001"/>
<add baseAddress="http://localhost:10002"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<applicationSettings>
<GenericForms.Properties.Settings>
<setting name="SqlDirectory" serializeAs="String">
<value>C:\TournamentTracker\</value>
</setting>
</GenericForms.Properties.Settings>
</applicationSettings>
</configuration>
服务器app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connectionString" value="Server=localhost;database=tournamenttracker;uid=root;pwd=12345"/>
</appSettings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ClientHealthService.ClientHealth" behaviorConfiguration="MetadataBehavior">
<endpoint address=""
binding="netTcpBinding"
contract="ClientHealthService.IClientHealth"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:10001"/>
<add baseAddress="http://localhost:10002"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
赔率是没有超时问题,但你没有回答时达到超时,不是由于时间,而是由于一个问题导致它永远不会回答(5分钟已经很大)。诊断它的最佳方法是更改配置(在客户端和服务器端)以记录WCF事件
http://msdn.microsoft.com/en-us/library/ms730064.aspx
然后在服务器上查看它们,如果服务器端是空的,那么它没有接收到任何连接并且移动到客户端的日志可能会告诉您更多关于错误的信息,如果您仍然没有得到任何信息,请联系网络管理员,它最有可能是网络问题。
在您的客户端app.config中,您仍然连接到localhost:
<client>
<endpoint address="net.tcp://localhost:10001/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IClientHealth" contract="ClientHealth.IClientHealth"
name="NetTcpBinding_IClientHealth">
<identity>
<userPrincipalName value="Maelstrom\Josh" />
</identity>
</endpoint>
</client>
在端点的地址中,您必须放置服务器的IP地址或主机名,包括端口。
我进一步挖掘net.tcp绑定,结果发现我使用的是安全模式的传输,我应该使用无安全模式。我将安全模式none标记添加到客户端和服务器,并且能够正常连接。它在VM上工作的原因是我使用的凭据是实现/转移到VM,因为它托管在同一台机器上。
谢谢大家的帮助。