我正在创建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