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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With