Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending messages to WCF host process

Tags:

c#

wcf

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?

like image 966
dotnetengineer Avatar asked Sep 26 '08 14:09

dotnetengineer


People also ask

Which classes are responsible for message serialization in WCF?

WCF uses the DataContractSerializer class as its default serializer.

What is WCF host?

Windows Communication Foundation (WCF) Service Host (WcfSvcHost.exe) allows you to launch the Visual Studio debugger (F5) to automatically host and test a service you have implemented. You can then test the service using WCF Test Client (WcfTestClient.exe), or your own client, to find and fix any potential errors.

What is WCF message?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.


1 Answers

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.

like image 106
Franci Penov Avatar answered Sep 19 '22 04:09

Franci Penov