简体   繁体   中英

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. 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)

<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

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. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> 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

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 http add urlacl url=http://+:8080/ user=\everyone

Also try testing it with your PC's firewall disabled

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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