简体   繁体   中英

WCF REST Service (self-hosted) accessible only in local network (and must be accessed remotely)

I have a self-hosted REST service (in C#) which works perfectly in my local network, but is unreachable remotely. Here is my hosting code:

static Uri baseAddress = new Uri("http://m.y.i.p:8080");
WebServiceHost serviceHost = new WebServiceHost(typeof(WebService.RestService),
    baseAddress);
serviceHost.Open();

(Which is run in Windows Forms, BTW).

Here is App.Config:

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
</bindings>
<services>
    <service name="WebService.RestService" behaviorConfiguration="Default">
    <host>
      <baseAddresses>
      </baseAddresses>
    </host>
    <endpoint address="" binding="webHttpBinding" 
          behaviorConfiguration="webBehavior" 
          contract="WebService.ICarga">
    </endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="webBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

When, in a browser, I enter 10.1.1.1:8080/test, it shows exactly what I want. But when I type myip:8080/test, it is unreachable. Firewall is off, I'm running as administrator and already tried the netsh command. Port 8080 is correctly forwarded to my PC. How can I make this accessible remotely?

Thanks.

EDIT: For people looking into this in the future, I had a problem with my router (my ip changed, and I didn't change it in the router). Everything should work if you define your App.config right. If you have this problem, double check your code, be sure that your network is working the way you expect it to, if needed turn off firewall and if you're not administrator, use the answer to this question or this MS page to own your desired port.

Change

new Uri("http://m.y.i.p:8080");

to

new Uri("http://0.0.0.0:8080");

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