![](/img/trans.png)
[英]WCF REST Service (self-hosted) accessible only in local network (and must be accessed remotely)
[英]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/ValueDataService
將localhost
替換為服務器的IP地址,以便鏈接如下所示: http://192.168.1.5:9001/ValueDataService
: http://192.168.1.5:9001/ValueDataService
http://localhost:9001/ValueDataService
轉到網絡和共享中心/使用ipconfig獲取服務器機器的IP地址。
localhost鏈接不起作用的原因是因為當瀏覽器讀取localhost時它會解析它查看計算機自己的網絡接口而不是向外看。
我希望它有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.