Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The InstanceContext provide to the ChannelFactory contains a UserObject that does not implement the CallbackContractType

Tags:

c#

wcf

I'm trying to get WCF duplex communication working an I'm struggling as I keep getting the error:

The InstanceContext provide to the ChannelFactory contains a UserObject that does not implement the CallbackContractType"

I know there are other posts on the subject but couldnt relate them to my exact problem so thought I'd post myself.

Here's my code, I've only included the bits I think are relevant but please let me know if you require anything else:

Host interface definitions

[ServiceContract(CallbackContract = typeof(IDataCollectorCallback), SessionMode = SessionMode.Required)]
  public interface IDataCollector
  {
    [OperationContract(IsOneWay = true)]
    void GetData();
  }

  public interface IDataCollectorCallback
  {
    [OperationContract(IsOneWay = true)]
    void returnData();
  }

Implementation of service

public class DataCollector : IDataCollector 
  { 
    public void GetData() 
    {

      Console.WriteLine("Getting data"); 
      Console.WriteLine("Waiting");
      System.Threading.Thread.Sleep(10000);
      Console.WriteLine("Sending Data back");
      Callback.returnData();


    }

    IDataCollectorCallback Callback
    {
      get
      {
        return OperationContext.Current.GetCallbackChannel<IDataCollectorCallback>();
      }
    }

  }

Client code

class Program
  {
    static void Main(string[] args) 
    { 
      // while (true) 
      //{ 
        Console.WriteLine("Press enter to trigger data collection");
        Console.ReadLine();
        InstanceContext context = new InstanceContext(new MyCallback());


        AshService.DataCollectorClient svc = new AshService.DataCollectorClient(context);

        svc.GetData();
        Console.WriteLine("awaiting data coming back");
        Console.ReadLine();
      //} 
    }

  }

  class MyCallback : IDataCollectorCallback
  {
    public MyCallback()
    {
    }

    public void returnData()
    {
      Console.WriteLine("Got Data back from the server");
    }
  }

To get a reference to the IDataCollector interface I have included a ref to the dll in the host project. I'm wondering if this is where my issue lies. Do I need to redeclare the callback interface in the client application?

Please let me know if you require anything else.

Kind Regards

Ash

like image 959
user589195 Avatar asked Feb 11 '11 10:02

user589195


2 Answers

Apologies.

I have worked out the answer.

My problem was that I was referencing the dll containing the callback interface.

What I should have done is this....

    class MyCallback : AshService.IDataCollectorCallback

Thanks

Ash

like image 189
user589195 Avatar answered Nov 09 '22 02:11

user589195


It would have took forever to figure this one out. I left out the interface in my callback class declaration when I copied it from the sample code.

like image 44
Dan Randolph Avatar answered Nov 09 '22 03:11

Dan Randolph