WCF Service (dll) host application

Go To StackoverFlow.com

1

I have a WFC(4.0) service dll for which I have to create a host application. The dll is pretty simple and has the following interfaces:

    [ServiceContract(CallbackContract=typeof(IChatServiceCallback))]
    public interface IService {
        [OperationContract]
        Guid Subscribe();
    }

    public interface IServiceCallback {
         void NotifyClient(strign message);
    }

My problem comes when I try to create a service. When I create a channel from the client to the host, I get the following exception: "The InstanceContext provided to the ChannelFactory contains a UserObjecct that does not implement the CallbackContractType 'Client.MyServiceReference.IServiceCallback'."

I found that in the ServiceReference object browser the ClientObject does not contain the IServiceCallback interface. Here my correspoding host code:

    ServiceHost host = new ServiceHost(typeof(ChatService));

    try
    {
        host.BeginOpen(new AsyncCallback(OnOpen), host);
        mre.WaitOne();
        if (host.State == CommunicationState.Opened)
        {
            Console.WriteLine("Server is running!\nServer listens on the following endpoints:");
            foreach (var endp in host.Description.Endpoints)
            {
                Console.WriteLine("\t{0}", endp.Address);
            }

            Console.WriteLine("Press <Enter> to stop the server...");
            Console.ReadLine();
            host.Close();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: {0}", ex.Message);
    }

How can I create a host application for a Service dll which contains callback interface that is implemented on the client side?

ClientSide: try { clientID = client.Subscribe(); <-- Throws exception. } catch(Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK); }

2012-04-04 07:31
by Wrath


1

Can you use the service contract assembly directly on the client? If so you can use DuplexChannelFactory and avoid the client-side service reference code altogether.

And example here

I know this doesn't answer your question but it may help you work around it.

2012-04-04 08:17
by tom redfern
Thanks but there the host implements the service functions. I have a ready service which contains the implementations also. Meaning I have the interfaces and the classes also in the dll so I don't want to rewrite those in the host application - Wrath 2012-04-04 08:54
So why are you using ChannelFactory in the service host? ChannelFactory is for clients to connect to a service - tom redfern 2012-04-04 09:16
What ChannelFactory? Where? I think you don't get my problem in its full detail. I have a complete service which I got in a .dll library. I have to create a host application which hosts the service and receives client connections. I don't know where did you get that ChannelFactory just now. All I said that the link you gave me isnot suited for my question. I have the ready Service and I only have to host it corretly with the callback interface. On the link you gave me recently the service is implemented IN the HOST. I can't use it because my Service is implemented in a DLL which I want to use - Wrath 2012-04-04 09:58
OK so help me by clarifying a few things: 1. Does your service run when you start the host process? 2. Is your client application referencing your service via a generated service reference? 3. Do you get the exception when your client application calls your service - tom redfern 2012-04-04 10:29
1.) Yes my service is running when I start the host process. 2.) Yes the client references via generated code. 3.) Exactly I get the exception when the client tries to call the Subscribe method - Wrath 2012-04-04 10:38
So my suggestion is do not use the service reference, which is what seems to be causing the problem. Make the client reference the service assembly directly and then use DuplexChannelFactory to call the service - tom redfern 2012-04-04 12:24
Ads