Search code examples
c#wcfvisual-studio-2015duplex

UserObject does not implement the CallbackContractType 'Client.MyService.IMyServiceCallback'


I am trying to implement a generic duplex connection using WCF in Visual Studio 2015, however, upon running my client application this error appears:

The InstanceContext provided to the ChannelFactory contains a UserObject that does not implement the CallbackContractType 'Client.MyService.IMyServiceCallback'.

I can't seem to find anything wrong with the file referenced:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Client.MyService;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            var callback = new InstanceContext(new ClientCallback());
            var client = new MyServiceClient(callback);
            client.Open();
            client.Register();
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
            client.Close();
        }
    }
}

And the interface seems to be implemented correctly:

namespace Client
{
    public interface IMyServiceCallback
    {
        [OperationContract(IsOneWay = true)]
        void Tick(DateTime dateTime);
    }
}


namespace Client
{
    public class ClientCallback : IMyServiceCallback
    {
        public void Tick(DateTime dateTime)
        {
            Console.WriteLine(dateTime);
        }
    }
}

Any help or advice would be greatly appreciated.


Solution

  • I'm going to presume that the class MyServiceClient was auto generated by add service reference. If that's the case, the MyServiceClient implements an interface which I will presume is called IMyService. That interface will have the callback contract specified via an attribute. It would look something like this:

    [ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
    public interface IMyService
    {
        [OperationContract]
        void Register();
    }
    

    The exact interface type that CallbackContract is set to is what your callback class needs to implement. Declaring a new interface with the same name (with a different namespace otherwise you would have a compile error) is not equivalent to the interface that was specified on the regular service interface. Remove your definition of IMyServiceCallback and change ClientCallback to implement the correct interface type.