Arsen Zahray
Arsen Zahray

Reputation: 25297

EndpointNotFoundException when creating callback WCF service

I'm trying to do self-hosting for a WCF callback service, based on this example.

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

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

    [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

Upvotes: 0

Views: 3422

Answers (2)

Jontatas
Jontatas

Reputation: 964

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

Upvotes: 1

tom redfern
tom redfern

Reputation: 31760

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")

UPDATE

Your client config contract is set to CallbackService.IMessage but nowhere in your code is there a service implementation of the IMessage contract.

Upvotes: 2

Related Questions