繁体   English   中英

无法在本地网络中远程访问WCF自托管服务

[英]WCF self-hosted service is not accessible remotely in local network

我目前正在创建WCF自托管服务(托管在ConsoleApplication中)。 服务合同很简单,它只包含两种方法。 当我在本地机器上本地托管服务时,一切都很好。 当我尝试托管它以从本地网络中的其他机器访问时,事情变得复杂。 我可以通过所有机器上的浏览器访问 - 没问题就在这里。 但是,当我只尝试创建客户端应用程序并调用某些方法时,我得到以下异常:

附加信息:没有端点侦听> http:// localhost:9001 / ValueDataService可以接受该消息。 这通常是由不正确的地址或SOAP操作引起的。 如果> present,请参阅InnerException以获取更多详细信息。

内部异常:

远程服务器返回错误:(404)Not Found。

我感到困惑,因为我可以通过浏览器输入http:// localhost:9001 / ValueDataService地址远程访问它。 客户端应用程序始终抛出上述异常。

只是为了简化:通过浏览器(而不是单个localhost机器)在本地网络中的其他机器上看到WCF服务,我唯一需要改变的是添加

hostNameComparisonMode="Exact"

属性到端点绑定。

更新:当然将localhost更改为主机的IP地址。

服务合同如下:

[ServiceContract]
public interface IValuesDataService
{
  [OperationContract]
  int[] GetValues();

  [OperationContract]
  void SetValues(int[] values);
}

控制台应用程序如下所示:

    static void Main(string[] args)
    {
        ServiceHost svcHost = null;
        try
        {
            svcHost = new ServiceHost(typeof(ValueDataService.ValueDataService));
            svcHost.Open();
            Console.WriteLine("Value Data Service has been hosted.");
        }
        catch (Exception e)
        {
            svcHost = null;
            Console.WriteLine("Service can not be started \n\nError Message [" + e.Message + "]");
        }
        if (svcHost != null)
        {
            Console.WriteLine("\nPress any key to close the Service");
            Console.ReadLine();
            svcHost.Close();
            svcHost = null;
        }
    }

主机应用程序App.config文件:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ValueDataService.ValueDataService"     behaviorConfiguration="mathServiceBehave">
        <host>
          <baseAddresses>
        <add baseAddress="http://localhost:9001/ValueDataService"/>
      </baseAddresses>
    </host>
    <endpoint address="" binding="basicHttpBinding"  contract="ValueDataService.IValueDataService"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="mathServiceBehave">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <basicHttpBinding>
    <binding name="bindingName" hostNameComparisonMode="Exact">
      <security mode="None"/>
    </binding>
  </basicHttpBinding>
</bindings>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>

至少Client Application App.config文件:

<?xml version="1.0"?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<system.serviceModel>
    <bindings>
      <basicHttpBinding>
          <binding name="myBinding">
            <security mode="None"/>
          </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:9001/ValueDataService"  binding="basicHttpBinding" contract="ValueDataServiceReference.IValueDataService"/>
    </client>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webEndpoint">
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>
</configuration>

任何人都可以帮助我完成这项工作吗?

更新:

所以IIS有一个烦人的绑定系统,它不会将IP地址请求转换为localhost绑定。 它和不幸的事情,但这就是它的请求系统如何工作。 在调试中运行WCF服务时,它会运行带有localhost绑定的IIS express实例。 因此,当请求通过时,它不会转换为运行实例的绑定地址。

如何解决这个问题:

1)启动IIS 2)使用合适的IP地址绑定在IIS中托管您的已开发应用程序

OLD:在链接中: http://localhost:9001/ValueDataServicelocalhost替换为服务器的IP地址,以便链接如下所示: http://192.168.1.5:9001/ValueDataServicehttp://192.168.1.5:9001/ValueDataService http://localhost:9001/ValueDataService转到网络和共享中心/使用ipconfig获取服务器机器的IP地址。

localhost链接不起作用的原因是因为当浏览器读取localhost时它会解析它查看计算机自己的网络接口而不是向外看。

我希望它有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM