简体   繁体   English

无法从局域网上的任何其他计算机访问WCF REST服务

[英]Can't Access WCF REST service from any other machine on my local area network

None of any similar questions have helped me. 任何类似的问题都没有帮助我。 I am sure experts here would.. 我相信这里的专家会..

My problem is I am hosting a WCF Rest service and I can only access it locally. 我的问题是我正在托管WCF Rest服务,并且只能在本地访问它。 Any other computer on the LAN won't be able to access it. 局域网上的任何其他计算机将无法访问它。

This is how I host my service (this run in a Console application) 这就是我托管服务的方式(此服务在控制台应用程序中运行)

WebServiceHost host = new WebServiceHost(typeof(MyRestService), new Uri("http://Yaniv-PC:8080/Test"));

Here is the Service Contract: 这是服务合同:

[ServiceContract]
public interface IMyRestService
{
    [OperationContract]
    [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "/{id}")]
    string GetData(string id);
}

And Here is the app.config file (only the serviceModel section) 这是app.config文件(仅serviceModel部分)

<system.serviceModel>
<services>
  <service behaviorConfiguration="MyRestServiceBehavior" name="ServiceHostApp.MyRestService">
    <endpoint address="" binding="webHttpBinding" contract="ServiceHostApp.IMyRestService" behaviorConfiguration="web">
      <identity>
        <dns value="Yaniv-PC" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyRestServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

When I open the browser and navigate to: http://yaniv-pc:8080/test/123 当我打开浏览器并导航到: http://yaniv-pc:8080/test/123

I get this: 我得到这个:

<?xml version="1.0"?>
<GetDataResponse xmlns="http://tempuri.org/">
    <GetDataResult>You sent server 123</GetDataResult>
</GetDataResponse>

When I access the service from other machine in my LAN i get this error: 当我从局域网中的其他计算机访问服务时,出现此错误:

System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://yaniv-pc:8080/Test/123 that could accept the message. System.ServiceModel.EndpointNotFoundException:在http://yaniv-pc:8080/Test/123上没有侦听终结点的端点可以接受该消息。 This is often caused by an incorrect address or SOAP action. 这通常是由不正确的地址或SOAP操作引起的。 See InnerException, if present, for more details. 有关更多详细信息,请参见InnerException(如果存在)。 ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network 10.0.0.3:8080 ---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:尝试了对不可达网络的套接字操作10.0.0.3:8080

Any ideas? 有任何想法吗?

Assuming that you are self-hosting your service make sure that you have registered your port using the netsh command as below: 假设您正在自托管服务,请确保已使用netsh命令注册了端口,如下所示:

netsh http add urlacl url=http://+:8080/ user=\everyone

Also try testing it with your PC's firewall disabled 另外尝试在禁用PC防火墙的情况下对其进行测试

您应该尝试禁用防火墙或尝试向防火墙添加带有端口的规则。

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

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