0

I am trying to make a connection between two services. Service1 will send event notifications to service2.

I want to use callbacks, between these two services. service1 will send event to service2 and service2 will pass the event to a client.

Is it possible to use callbacks between two services?

I have already looked into the samples of wcf callbacks, but cant find anything between service to service communication.

Any help is appreaciated. see below the code of 1 of the services. my question: how is the code of the other service is going to look like with the callback in it?

Or is it in this case one of the services acting like a client? And how should that be looking like?

Service1 code:(Interface class)

public interface IMyEvents
{
    [OperationContract(IsOneWay = true)]
    void Event1();
}

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyEvents))]
public interface SampleInterface
{
    [OperationContract]
    void DoSomethingAndFireEvent();

    [OperationContract]
    void SubscribeEvent(); 
}

This is the service class

public class SampleHandlerSimulator : SampleInterface
{
    static Action m_Event1 = delegate { Console.WriteLine("Catch");};

    public void SubscribeEvent()
    {
        IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
        m_Event1 += subscriber.Event1;
    }

    public static void FireEvent()
    {
        m_Event1();
    }

    public void DoSomethingAndFireEvent()
    {
        CCStateHandlerSimulator.FireEvent(); 
    }
CAbbott
  • 8,078
  • 4
  • 31
  • 38
Annonymuze
  • 53
  • 1
  • 1
  • 4

1 Answers1

0

Configure your services by adding a service reference to one of them (refering to the other service). To do this make sure the service to be referenced is up and running, then right click on the child service project and select add service reference. Now you can make calls to the methods exposed by the parent service.

You will need an endpoint configured with duplex binding for call backs. See here for information on duplex binding. Update your service reference when you add new endpoint configurations to the parent service. You will need to host your services.

A similar question was raised here.

Your enpoint will look like:

 <endpoint address="http://localhost:61775/DUPLEXFUN/" binding="wsDualHttpBinding"
      bindingConfiguration="" name="DUPEND" contract="IDuplexCallback" />

Note the wsDualHttpBinding is the important bit to note.

Your service contract will look like:

    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDuplexCallback))]
public interface IDuplexService
{

    [OperationContract(IsOneWay = true)]
    void FormatString(string institution);
}

and the service implementation will be like:

   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class DuplexService : IDuplexService
{
    IDuplexCallback _callback;
    string _requestString = String.Empty;

    public DuplexService()
    {
        _callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
    }

    public void FormatString(string text)
    {
        //Make the thread to sleep for 10 seconds
        Thread.Sleep(4000);
        //Format the input data
        _requestString = string.Format("Welcome to {0}", text);
        //Pass the string to the client through the call back function
        _callback.DuplexCallbackFunction(_requestString);
    }
}

and finally IDuplexCallback is declared as:

 public interface IDuplexCallback
    {
        [OperationContract(IsOneWay = true)]
        void DuplexCallbackFunction(string requestString);
    }
Community
  • 1
  • 1
ldgorman
  • 1,553
  • 1
  • 14
  • 39
  • Thanks for your answer, but I cant get it running yet. I am using netnamedpipebinding which is also duplex. Do I have to implement the netnamedpipebinding in both services or just one? Is your code above an example of how the second service should be impelemented? Because as you see I have provided the code from the first service. And your code doesnt really fit into my service1 code. – Annonymuze Mar 16 '12 at 09:17
  • I don't know much about pipes, but you should only need to set up the end point in the parent service and then once you have added the service reference to the child, the child service will reflect the parents service in it's configuration. The code i posted isn't specific to you, but it does highlight what tags and things that you need. Perhaps you could post your parent service app.config? – ldgorman Mar 16 '12 at 14:40
  • A PubSub might work better, depends on what you are trying to accomplish. I've had all kinds of issues with duplex bindings, and they aren't all that efficient. Good example of Pub-Sub and a framework for doing so are available here: http://www.idesign.net/Downloads – PTiddy Mar 16 '12 at 14:59