2017-12-03 365 views
1

我最近开始使用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 

我认真地穿上”不知道该怎么做,任何帮助都会很棒。

非常感谢!

+0

去这个网站一个简单的教程一步一步在WCF - https://www.tutorialspoint.com/wcf/wcf_creating_service.htm为输入 – MethodMan

+0

谢谢,但它没有太大的帮助,我一直在做的东西大部分是那个教程 – gqmartins

回答

0

您已经公开了元数据,但没有将其绑定到服务。以下是您需要的。

<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

谢谢你的输入!但是,这仍然没有这样做,我一直使用你的答案得到相同的错误 – gqmartins

+0

你能浏览wsdl吗? –

+0

不,没什么,当我把?wsdl放在基本URI之前,或者甚至使用基本URI时,它仍然会给出一个错误。你是否设法使它在你的机器上工作? – gqmartins

0

所以我想通了什么是错的,这是开始服务的代码。而不是我有什么它应该是:

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(); 
}