事件通知类的WCF应用
1.可使用的绑定协议
wsDualHttpBinding,netTcpBinding…
2.不能使用的协议
basicHttpBinding…
Iservice.cs
[ServiceContract(CallbackContract =typeof(IServiceCallBack), SessionMode = SessionMode.Required)]
public interface INoticeService
{
[OperationContract]
void Register();
[OperationContract(IsOneWay = true)]
void BroadCast(byte[] buffer, intlength);
[OperationContract]
void UnRegister();
}
[ServiceContract]
public interface IServiceCallBack
{
[OperationContract]
void GetNotice(byte[] buffer, intlength);
}
2.实现INoticeService接口
[ServiceBehavior(IncludeExceptionDetailInFaults= true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode =ConcurrencyMode.Multiple)]
public class NoticeService : INoticeService
{
static Dictionary
public void Register()
{
allOperation.Add(OperationContext.Current.SessionId,OperationContext.Current);
}
public void BroadCast(byte[] buffer,int length)
{
//广播进行回调函数
foreach (var item in allOperation)
{
item.Value.GetCallbackChannel
}
}
public void UnRegister()
{
allOperation.Remove(OperationContext.Current.SessionId);
}
}
2.服务客户端(宿主)调用
public voidstatic main()
{
string bind = "http://" +System.Configuration.ConfigurationManager.AppSettings["bind"].ToString()+ "/NoticeService";
//http://127.0.0.1:9000/NoticeService
//Service = new NoticeService();
//string bind =System.Configuration.ConfigurationManager.AppSettings["bind"].ToString();
Uri uri = new Uri(bind);
ServiceHost Host = newServiceHost(typeof(EventNotice.NoticeService), uri);
Host.Opened += Opened;
Host.Closed += Host_Closed;
Host.Faulted += Faulted;
Host.AddServiceEndpoint(typeof(IService.INoticeService), newWSDualHttpBinding(), "");
ServiceMetadataBehavior behavior =new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
Host.Description.Behaviors.Add(behavior);
Host.Open();
}
3.客户端调用
public delegate void NoticeInfo(byte[] buffer,int length);
public class WcfChannel :IService.IServiceCallBack
{
public void GetNotice(byte[] buffer,int length)
{
if (Notice != null)
{
Notice(buffer, length);
}
}
public static event NoticeInfo Notice;
static WcfChannel()
{
INoticeService Service =DuplexChannelFactory
new WSDualHttpBinding(), newEndpointAddress("http://127.0.0.1:9000/NoticeService"));
//订阅消息
Service.Register();
//service.UnregisterListener();
}
private static WcfChannel Instance()
{
return new WcfChannel();
}
}