【问题标题】:C# - WCF works on local machine, but not over networkC# - WCF 在本地机器上工作,但不在网络上
【发布时间】:2018-01-04 09:30:59
【问题描述】:

我的 WCF 在 C# 应用程序中存在问题。当服务器和客户端在同一台 PC 上时,我可以调用服务,但如果服务器和客户端在不同的 PC 上,我的请求会超时。 我已经打开了防火墙中的端口(我使用端口 9998),我什至尝试关闭防火墙。 服务器的 app.config 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SMS_ServiceBehavior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="SMS_ServiceBehavior" name="SMSapplication.SMS_Service">
        <endpoint address="http://10.20.5.100:9998/SMS_Service" binding="basicHttpBinding"
          bindingConfiguration="" name="SMS_ServiceEndpoint" contract="SMSapplication.ISMS_Service" />
        <endpoint address="http://10.20.5.100:9998/mex" binding="mexHttpBinding"
          bindingConfiguration="" name="mex" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

有没有人知道我还能尝试什么或错误可能出在哪里?

【问题讨论】:

  • 这真的是您服务器机器的 public IP 地址吗? (你应该重新考虑在这里发布它......)
  • 尝试增加超时值。也许您的操作在网络上花费的时间比在一台机器上工作时要长。
  • 你能从客户端机器上ping那个IP吗?可以分享一下客户端应用的配置文件吗?
  • ip地址有效,可以ping通机器,也可以用telnet调用端口。

标签: c# visual-studio web-services wcf


【解决方案1】:

客户端的配置文件如下所示:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="SMS_ServiceEndpoint" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:9998/SMS_Service" binding="basicHttpBinding"
                bindingConfiguration="SMS_ServiceEndpoint" contract="SMS_Service.ISMS_Service"
                name="SMS_ServiceEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

【讨论】:

  • 您的客户端引用本地主机 => 它应该引用您的服务器 ip
【解决方案2】:

我通过以下方式处理代码:

SMS_Service.SMS_ServiceClient client = new SMS_Service.SMS_ServiceClient();
client.Endpoint.Address = new EndpointAddress(new Uri("http://10.20.5.100:9998/SMS_Service"), client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
client.Open();
client.DoWork("test");
client.Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-16
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多