0

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.

1 Answers1

0

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.

MattC
  • 373
  • 1
  • 6
  • I followed your advice, and after a bit of fiddling I got the client to successfully start without throwing any exceptions. However, now I'm a tad confused. ClientCallback now implements an interface called IMyServiceCallback, but at a different namespace(MyService.IMyServiceCallback). Is this the solution you had in mind, or is this something completely different? IMyServiceCallback is not even defined in my file structure, but it magically causes the client to function properly. – user5389897 Oct 02 '15 at 15:07
  • MyServiceClient was auto generated by a tool (presumably "Add Service Reference" in visual studio). That downloaded the wsdl from the service and created an interface (IMyService) for that service in the namespace MyService. The namespace was specified in the dialog box of the "Add Service Reference" window and you left the default value. As it generated the service interface IMyService and that interface needs the callback interface defined, IMyServiceCallback also needs to be generated at the same time and will live in the same namespace. Highlight the interface in VS and press F12. – MattC Oct 02 '15 at 18:32