dotnetengineer
dotnetengineer

Reputation: 1302

Sending messages to WCF host process

I have a Console application hosting a WCF service. I would like to be able to fire an event from a method in the WCF service and handle the event in the hosting process of the WCF service. Is this possible? How would I do this? Could I derive a custom class from ServiceHost?

Upvotes: 17

Views: 8887

Answers (2)

Pankaj Awasthi
Pankaj Awasthi

Reputation: 39

using ...
using ...

namespace MyWCFNamespace
{
    class Program {

        static void Main(string[] args){
            //instantiate the event receiver
            Consumer c = new Consumer();

            // instantiate the event source
            WCFService svc = new WCFService();
            svc.WCFEvent += new SomeEventHandler(c.ProcessTheRaisedEvent);

            using(ServiceHost host = new ServiceHost(svc))
            {
                host.Open();
                Console.Readline();
            }
        }
    }


    public class Consumer()
    {
        public void ProcessTheRaisedEvent(object sender, MyEventArgs e)
        {
            Console.WriteLine(e.From.toString() + "\t" + e.To.ToString());
        }
    }
}


namespace MyWCFNamespace
{
    public delegate void SomeEventHandler(object sender,MyEventArgs e)

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class WCFService : IWCFService 
    {
        public event SomeEventHandler WCFEvent;

        public void someMethod(Message message)
        {
            MyEventArgs e = new MyEventArgs(message);
            OnWCFEvent(e);
        }

        public void OnWCFEvent(MyEventArgs e)
        {
            SomeEventHandler handler = WCFEvent;
            if(handler!=null)
            {
                handler(this,e);
            }
        }

    // to do 
    // Implement WCFInterface methods here
    }


    public class MyEventArgs:EventArgs
    {
        private Message _message;
        public MyEventArgs(Message message) 
        {
            this._message=message;
        }
    }
    public class Message
    {
        string _from;
        string _to;
        public string From {get{return _from;} set {_from=value;}}
        public string To {get{return _to;} set {_to=value;}}
        public Message(){}
        public Message(string from,string to)
        this._from=from;
        this._to=to;
    }
}

You can define your WCF service with InstanceContextMode = InstanceContextMode.Single.

TestService svc = new TestService();
svc.SomeEvent += new MyEventHandler(receivingObject.OnSomeEvent);
ServiceHost host = new ServiceHost(svc);
host.Open();

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] // so that a single service instance is created
    public class TestService : ITestService
    {
        public event MyEventHandler SomeEvent;
        ...
        ...
    }

Upvotes: 0

Franci Penov
Franci Penov

Reputation: 75982

You don't need to inherit from ServiceHost. There are other approaches to your problem.

You can pass an instance of the service class, instead of a type to ServiceHost. Thus, you can create the instance before you start the ServiceHost, and add your own event handlers to any events it exposes.

Here's some sample code:

MyService svc = new MyService();
svc.SomeEvent += new MyEventDelegate(this.OnSomeEvent);
ServiceHost host = new ServiceHost(svc);
host.Open();

There are some caveats when using this approach, as described in http://msdn.microsoft.com/en-us/library/ms585487.aspx

Or you could have a well-known singleton class, that your service instances know about and explicitly call its methods when events happen.

Upvotes: 15

Related Questions