2016-01-13 77 views
3

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

Additional information: There was no endpoint listening at >http://localhost:9001/ValueDataService that could accept the message. This is >often caused by an incorrect address or SOAP action. See InnerException, if >present, for more details.

内部异常:

The remote server returned an error: (404) Not Found.

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

只是为了得到简化事情: 要获得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"/> 

和至少客户端应用程序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> 

谁能帮我得到这个工作?

+1

您无法通过'localhost'远程访问它,这就是“本地”机器。将客户端的app.config更改为使用托管服务器应用程序的计算机的IP。 – DrewJordan

+0

就像你在你的机器上托管WCF服务一样,你必须将'localhost'改为你的内部IP地址。你确实需要保留端口号(:9001)。 –

+0

哦,我的坏,我没有写,但是,当然我使用主机的IP地址,如http://192.168.0.16/ValueDataService – smq93

回答

0

UPDATE:

所以IIS具有不转换IP地址的请求到本地主机绑定一个恼人的约束制度。它的和不幸的事情,但这就是它的请求系统如何工作。当您在调试中运行WCF服务时,它将使用本地主机绑定运行IIS Express实例。因此,当请求到达时,它不会被转换为实例正在运行的绑定地址。

如何解决这个问题:

1)启动IIS 2)用合适的IP地址绑定

旧主机在IIS你开发的应用程序: 在链接:http://localhost:9001/ValueDataService与服务器的IP地址替换localhost以便该链接看起来像:http://192.168.1.5:9001/ValueDataService转到网络和共享中心/使用ipconfig获取服务器计算机的IP地址。

本地主机链接不起作用的原因是因为当浏览器读取本地主机时,它解决了它看起来在计算机自​​己的网络接口,而不是向外看。

我希望它有帮助。

+0

我刚刚更新了我的问题,当然我将本地主机更改为IP地址主机也是如此。我还写道,我有权访问其他机器,但只能通过浏览器访问。 – smq93

+0

很久以前有人在尝试错误的IP。我认为你必须改变默认绑定,这可能是一些愚蠢的IIS中间件,它无法识别WCF中的本地主机绑定。 – rkrishnasanka

相关问题