Ainullin Damir
Ainullin Damir

Reputation: 315

WCF duplex: calling callback outside class of WCF-service

I develop WCF-service with duplex mode and use nettcpbinding. I use .NET 4.5. I have only one client and only one server and there is a lot of logic between its. The client always the first begins communication with the server. I want that server can call client functions outside WCF-service class. I try to use duplex mode for this purpose.

This is my WCF-service:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{

...

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

I have no problem when I call my callback within OperationContract functions of my service:

Callback.foo();

But when I try to call it outside class Service for example:

service = new Service(); 
service.Callback.foo();

I got NullReferenceException of OperationContext.Current. I found a lot of information about the similar problems on the SO and other resources but it can't help to decide my problem. Are there any solutions of my issue or may be workarounds for calling of callback? At the moment I plan to create one more WCF-service for my purpose but I feel that this is bad solution. Thanks for attention.

Upvotes: 2

Views: 388

Answers (1)

Yaron Naveh
Yaron Naveh

Reputation: 24436

OperationContext.Current has a meaning only when it runs from the same thread that handles the current client request. You did not specify where exactly you calls this from.

Upvotes: 1

Related Questions