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

时间:2017-12-03 01:45:10

标签: c# web-services wcf

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

我真的不知道该怎么做,任何帮助都会很棒。

非常感谢!

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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();
}