Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Duplex Contract

Tags:

c#

wcf

duplex

Say I have WCF service contract like this

[ServiceContract(CallbackContract = typeof(ICallback1),
SessionMode = SessionMode.Required)]
public interface IService1
{
  // some methods
}

The service implementation has InstanceContextMode.Single set for InstanceContextMode and ICallback1 is something like

public interface ICallback1
{
    [OperationContract]
    void Report(int someValue);
}

Now on client side, I can have a class the implements ICallback1 like

class Callback1 : ICallback1
{
    public void Report(int someValue)
    {
        // alert client
    }
}

and I create client service reference like this

Service1Client serviceClient = new Service1Client(new InstanceContext(new CallBack1())); 

which works fine. Now the problem is that I have some clients that are not interested in the callback so I figured I do not require to implement the callback interface for such clients so I tried this

 Service1Client serviceClient = new Service1Client(null);

and

 Service1Client serviceClient = new Service1Client(new InstanceContext(null));

both reported that the parameter cannot be null. My question is, how can i create a service reference without passing a callback object if the client is not interested in the callback. The only requirement is that all clients should be talking to the same service but otherwise I can restructure the service however. Any thoughts ?

EDIT:

I have also tried SessionMode = SessionMode.Allowed for ServiceContract instead of SessionMode.Required but that didn't help either.

like image 570
Bala R Avatar asked Mar 16 '11 04:03

Bala R


People also ask

Which HTTP binding is used for duplex contracts?

This is the WsDualHttpBinding. This binding is designed for use with Duplex Service contracts, which allows both the Services and clients to send and receive the messages.

What is a duplex service?

A duplex service contract is a message exchange pattern in which both endpoints can send messages to the other independently. A duplex service, therefore, can send messages back to the client endpoint, providing event-like behavior.

What is callback contract in WCF?

Abstract: In WCF, callback is used to implement PUSH mechanism, so that delayed or long running operation results can be automatically pushed back to the client application. WCF actively supports callback to its client, over the instance context established. In this article, we will explore the same.

What is callback contract?

Callback contracts that have one-way operations represent calls from the service that the client can handle. Note. The ServiceContractAttribute attribute is ignored on callback contracts. To configure runtime behavior of callback objects, use the System. ServiceModel.


1 Answers

Workaround: Remove the CallbackContract from IService1. Create IDuplexService1 which inherits IService1 and contains the CallbackContract. Have Service1Client implement IDuplexService1. When instantiating the host, call ServiceHost.AddServiceEndpoint for both IService1 and IDuplexService1.

like image 52
Edward Brey Avatar answered Oct 31 '22 21:10

Edward Brey