I'm using ServiceModelEx WCF library from Juval Lowy's "Programming WCF Services". I'm trying to implement a Publish-Subscribe Service with publisher and subscriber. What I have done so far is the publisher and the discover-publish service.
Service Contract:
[ServiceContract]
interface IMyEvents
{
[OperationContract(IsOneWay=true)]
void OnEvent1(int number);
}
Discover - publish Service:
class MyPublishService : DiscoveryPublishService<IMyEvents>, IMyEvents
{
public void OnEvent1(int number)
{
FireEvent(number);
}
}
Discover - publish service host:
ServiceHost host = DiscoveryPublishService<IMyEvents>.
CreateHost<MyPublishService>();
host.Open();
// later..
host.Close();
Publisher:
IMyEvents proxy = DiscoveryPublishService<IMyEvents>.CreateChannel();
proxy.OnEvent1();
(proxy as ICommunicationObject).Close();
My question is how can I implement the subscriber? The book says to implement the service contract. That's simple.
class EventServiceSubscriber : IMyEvents
{
public void OnEvent1(int number)
{
// do something
}
}
but how can i host the subscriber? How subscriber can connect to the Publish-Subscribe service?
There are a bunch of articles around that cover this subject. For starters, this one. You can host your subscriber in different ways like a console application or a ASP.NET application. Every application type has some kind of startup method so that'd be a good place to implement your subscription/publishing logic.