我使用的是Juval Lowy的"Programming WCF Services“中的ServiceModelEx WCF库。我正在尝试实现一个带有发布者和订阅者的发布-订阅服务。到目前为止,我所做的是发布者和发现-发布服务。
服务合同:
[ServiceContract]
interface IMyEvents
{
[OperationContract(IsOneWay=true)]
void OnEvent1(int number);
}发现-发布服务:
class MyPublishService : DiscoveryPublishService<IMyEvents>, IMyEvents
{
public void OnEvent1(int number)
{
FireEvent(number);
}
}发现-发布服务主机:
ServiceHost host = DiscoveryPublishService<IMyEvents>.
CreateHost<MyPublishService>();
host.Open();
// later..
host.Close();出版商:
IMyEvents proxy = DiscoveryPublishService<IMyEvents>.CreateChannel();
proxy.OnEvent1();
(proxy as ICommunicationObject).Close();我的问题是如何实现订阅者?书上说要执行服务合同。这很简单。
class EventServiceSubscriber : IMyEvents
{
public void OnEvent1(int number)
{
// do something
}
}但是我如何托管订阅者呢?订阅者如何连接到发布-订阅服务?
发布于 2016-07-30 20:51:43
为了让它正常工作,我创建了一个SubcriptionService,如下所示:
using ServiceLibrary.Contracts;
using ServiceModelEx;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Subscriber
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, IncludeExceptionDetailInFaults = DebugHelper.IncludeExceptionDetailInFaults, InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false)]
class SubscriptionService : DiscoveryPublishService<IMyEvents>, IMyEvents
{
public void OnEvent1()
{
Debug.WriteLine("SubscriptionService OnEvent1");
}
public void OnEvent2(int number)
{
Debug.WriteLine("SubscriptionService OnEvent2");
}
public void OnEvent3(int number, string text)
{
Debug.WriteLine("SubscriptionService OnEvent3");
}
}
}然后,我为这个服务设置了一个主机,如下所示:
ServiceHost<SubscriptionService> _SubscriptionHost = DiscoveryPublishService<IMyEvents>.CreateHost<SubscriptionService>();
_SubscriptionHost.Open();一个基本的工作示例可以在我的Github帐户中找到,网址如下。
https://github.com/systemsymbiosis/PublishSubscribeWithDiscovery
发布于 2015-12-04 17:58:02
有很多关于这个主题的文章。首先,this one。您可以通过不同的方式承载订阅服务器,如控制台应用程序或ASP.NET应用程序。每种应用程序类型都有某种启动方法,因此这将是实现订阅/发布逻辑的好地方。
https://stackoverflow.com/questions/34085299
复制相似问题