簡體   English   中英

創建回調WCF服務時EndpointNotFoundException

[英]EndpointNotFoundException when creating callback WCF service

根據此示例 ,我正在嘗試為WCF回調服務進行自我托管。

這是托管代碼:

服務:

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

客戶:

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#代碼:

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

服務器啟動正常。 在運行客戶端時,嘗試調用任何服務器功能時會崩潰,但以下情況除外:

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.

內部例外是:無法連接到遠程服務器。

這不是因為防火牆,因為我成功測試了沒有回調功能的類似應用

這可能是什么原因?

更新

完整的源代碼可以在這里下載: http : //iathao.com/tmp/fullSource.zip

您似乎正在將端點托管在http:// localhost:8000 / HelloWCF,但是客戶端配置指向http:// localhost:8000 / HelloWCF / HelloWCF (注意:額外的“ / HelloWCF”)

更新

您的客戶端配置協定設置為CallbackService.IMessage但是代碼中沒有地方提供IMessage協定的服務實現。

通過這些小的更改,您的代碼就可以在我的機器上正常工作。

客戶端配置

<?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>

入門代碼

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