![](/img/trans.png)
[英]Can't connect to NetTcpBinding WCF service due to “There was no endpoint listening” error
[英]WCF error - Can't get service endpoint up
我最近開始與WCF合作,但遇到一個問題,就是我不知道如何解決。 我使用服務主機啟動WCF服務,但是當我在瀏覽器中使用URI時,它不顯示服務協定,當我嘗試使用ChannelFactory連接到它時,它會給出異常。
我在Visual Studio 2017中創建了項目,但對配置文件不做任何事情,只好更改了基址。 服務接口和實現都在根項目“文件夾”中,並且我嘗試禁用防火牆,甚至禁用殺毒軟件,但似乎沒有任何效果。
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TaskExecutor.Exec">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
服務界面:
namespace TaskExecutor
{
[ServiceContract]
public interface IExec
{
[OperationContract]
void DoWork();
}
}
服務實施:
namespace TaskExecutor
{
public class Exec : IExec
{
public void DoWork()
{
Console.WriteLine("Doing work.");
}
}
}
程序啟動服務:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
}
Console.WriteLine("Press any key to end...");
Console.ReadLine();
啟動程序后,顯示以下消息:
exec service started at http://localhost:8001/TaskExecutor/Exec/
Press any key to end...
服務客戶端代碼如下:
EndpointAddress endpoint = new EndpointAddress("http://localhost:8001/TaskExecutor/Exec/");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IExec> channelFactory = new ChannelFactory<IExec>(binding, endpoint);
IExec proxy = channelFactory.CreateChannel();
proxy.DoWork();
它給出了例外:
System.ServiceModel.EndpointNotFoundException occurred
HResult=0x80131501
Message=There was no endpoint listening at http://localhost:8001/TaskExecutor/Exec/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Inner Exception 1:
WebException: Unable to connect to the remote server
Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it
我真的不知道該怎么辦,任何幫助都將是驚人的。
非常感謝你!
您已公開元數據但未將其綁定到服務。這是必須的方法。
<behaviors>
<serviceBehaviors>
<behavior name="metadadiscovery">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
現在將行為綁定到服務。
<services>
<service name="TaskExecutor.Exec" behaviorConfiguration="metadadiscovery">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
現在,當您在瀏覽器中鍵入地址時,您應該可以看到wsdl。 http:// localhost:8001 / TaskExecutor / Exec /
所以我找出了問題所在,這是啟動服務的代碼。 而不是我所擁有的應該是:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
Console.WriteLine("Press any key to end...");
Console.ReadLine();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.