简体   繁体   English

创建回调WCF服务时EndpointNotFoundException

[英]EndpointNotFoundException when creating callback WCF service

I'm trying to do self-hosting for a WCF callback service, based on this example . 根据此示例 ,我正在尝试为WCF回调服务进行自我托管。

Here's the hosting code: 这是托管代码:

service: 服务:

static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(typeof(Message), new Uri("http://localhost:8000/HelloWCF")))
        {
            // Set up a service endpoint [Contract, Binding, Address]
            host.AddServiceEndpoint(typeof(IMessage), new WSDualHttpBinding() { ClientBaseAddress = new Uri("http://locahost:8001/HelloWCF") }, "HelloWCF");

            // Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior() {HttpGetEnabled =true };

            host.Description.Behaviors.Add(smb);
            host.Open();

            Console.WriteLine("Ready...");
            Console.ReadLine();
        }
    }

client: 客户:

app.config app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding >
                <binding name="WSDualHttpBinding_IMessage" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" clientBaseAddress="http://locahost:8001/HelloWCF">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/HelloWCF/HelloWCF" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IMessage" contract="CallbackService.IMessage"
                name="WSDualHttpBinding_IMessage" >
                <identity>
                    <userPrincipalName value="badasscomputing\menkaur" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

c# code: C#代码:

    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    class Sender : IMessageCallback, IDisposable
    {
        private MessageClient messageClient;

        public void Go()
        {
            InstanceContext context = new InstanceContext(this);
            messageClient = new MessageClient(context, "WSDualHttpBinding_IMessage");

            for (int i = 0; i < 5; i++)
            {
                string message = string.Format("message #{0}", i);
                Console.WriteLine(">>> Sending " + message);
                messageClient.AddMessage(message);
            }

        }

        public void OnMessageAdded(string message, DateTime timestamp)
        {
        }

        public void Dispose()
        {
            messageClient.Close();
        }
    }

    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    class Listener : IMessageCallback, IDisposable
    {
        private MessageClient messageClient;

        public void Open()
        {
            InstanceContext context = new InstanceContext(this);
            messageClient = new MessageClient(context, "WSDualHttpBinding_IMessage");

            messageClient.Subscribe();
        }

        public void OnMessageAdded(string message, DateTime timestamp)
        {
            Console.WriteLine("<<< Recieved {0} with a timestamp of {1}", message, timestamp);
        }

        public void Dispose()
        {
            messageClient.Unsubscribe();
            messageClient.Close();
        }
    }

    static void Main(string[] args)
    {
        Listener l = new Listener();
        l.Open();

        Sender s = new Sender();
        s.Go();
    }

the server starts alright. 服务器启动正常。 when running the client, it crashes when trying to invoke any of the server functions with following exception: 在运行客户端时,尝试调用任何服务器功能时会崩溃,但以下情况除外:

EndpointNotFoundException:There was no endpoint listening at http://localhost:8000/HelloWCF/HelloWCF that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

The inner exception is:Unable to connect to a remote server. 内部例外是:无法连接到远程服务器。

This is not because of the firewall, as I successfully tested a similar app without a callback function 这不是因为防火墙,因为我成功测试了没有回调功能的类似应用

What could be the cause of this? 这可能是什么原因?

UPDATED 更新

full source can be downloaded here: http://iathao.com/tmp/fullSource.zip 完整的源代码可以在这里下载: http : //iathao.com/tmp/fullSource.zip

You appear to be hosting your endpoint at http://localhost:8000/HelloWCF but your client config is pointing to http://localhost:8000/HelloWCF/HelloWCF (note: extra "/HelloWCF") 您似乎正在将端点托管在http:// localhost:8000 / HelloWCF,但是客户端配置指向http:// localhost:8000 / HelloWCF / HelloWCF (注意:额外的“ / HelloWCF”)

UPDATE 更新

Your client config contract is set to CallbackService.IMessage but nowhere in your code is there a service implementation of the IMessage contract. 您的客户端配置协定设置为CallbackService.IMessage但是代码中没有地方提供IMessage协定的服务实现。

With these small changes your code works fine on my machine. 通过这些小的更改,您的代码就可以在我的机器上正常工作。

Client config 客户端配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_IMessage" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/HelloWCF" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IMessage" contract="CallbackService.IMessage"
                name="WSDualHttpBinding_IMessage">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

Starter code 入门代码

namespace wcfStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Message), new Uri("http://localhost:8002/HelloWCF")))
            {
                // Set up a service endpoint [Contract, Binding, Address]
                host.AddServiceEndpoint(typeof(IMessage), new WSDualHttpBinding() { ClientBaseAddress = new Uri("http://locahost:8001") }, "HelloWCF");

                // Enable metadata exchange
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior() {HttpGetEnabled =true };

                host.Description.Behaviors.Add(smb);
                host.Open();

                Console.WriteLine("Ready...");
                Console.ReadLine();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM