我正在創建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>
誰能幫我得到這個工作?
您無法通過'localhost'遠程訪問它,這就是「本地」機器。將客戶端的app.config更改爲使用託管服務器應用程序的計算機的IP。 – DrewJordan
就像你在你的機器上託管WCF服務一樣,你必須將'localhost'改爲你的內部IP地址。你確實需要保留端口號(:9001)。 –
哦,我的壞,我沒有寫,但是,當然我使用主機的IP地址,如http://192.168.0.16/ValueDataService – smq93