• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

WCF错误 – 无法获取服务端点

c/c++ 来源:gqmartins 4次浏览

我最近开始使用WCF,我遇到了一个问题,我只是没有线索如何解决。 我使用服务主机启动WCF服务,但是当我在浏览器中使用URI时,它不显示服务的合同,当我尝试使用ChannelFactory连接到它时,它会发出异常。WCF错误 – 无法获取服务端点

我在Visual Studio 2017中创建了项目,并没有对配置文件做任何事情,excpet改变了基地址。服务接口和实现都在根项目“文件夹”中,我试过禁用防火墙甚至是防病毒,但似乎没有任何效果。

的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/


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)