zoukankan      html  css  js  c++  java
  • Use Custom Events from your WCF ServiceHost http://www.codeproject.com/Tips/150702/UseCustomEventsfromyourWCFServiceHost 武胜

    While working on an article for CodeProject, I happened on a requirement to create a WCF service using NamedPipe binding. The client application (a Windows Service) would be sending periodic messages, and if the sever (a SysTray application) was listening, it would insert those messages into a ListBox (I have no need to write the messages to disk anywhere - I just want to show them in a ListBox). This tip involves allowing the ServiceHost instance communicating to the host application that it had received a message from the client by way of a custom event.
     
    First, you need your custom event:
     

    //////////////////////////////////////////////////////////////////////////////////////
    public class MyHostEventArgs
    {
        public string   Message { get; set; }
        public DateTime Date    { get; set; }
        public MyHostEventArgs(string msg, DateTime datetime)
        {
            this.Message = msg;
            this.Date    = datetime;
        }
    }
     
    public delegate void MyHostEventHandler(object sender, MyHostEventArgs e);
    

     
    Next, you need to define your service (and this is where most of the tip lies):
     

    //////////////////////////////////////////////////////////////////////////////////////
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        void SendStatusMessageEx(string msg, DateTime datetime);
    }
     
    //////////////////////////////////////////////////////////////////////////////////////
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, IncludeExceptionDetailInFaults=true)]
    public class MyService : IMyService
    {
        public event MyHostEventHandler MyHostEvent = delegate{};
     
        public MyService()
        {
        }
     
        //--------------------------------------------------------------------------------
        public void SendStatusMessageEx(string msg, DateTime datetime)
        {
            MyHostEvent(this, new MyHostEventArgs(msg, datetime));
        }
    }
    


     
    Notice the ServiceBehavior attribute. When you set the InstanceContextMode to Single, you can then do the following in your application:
     

    // create and open an instance of the host in our form object
    Uri                 BaseAddress      = new Uri("net.pipe://localhost/MyService");
    NetNamedPipeBinding NamedPipeBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
    MyService           svc              = new MyService();
    SvcHost                              = new ServiceHost(svc, BaseAddress);
    SvcHost.AddServiceEndpoint(typeof(IMyService), NamedPipeBinding, "");
    SvcHost.Open();
     
    // now we can add an event handler
    (SvcHost.SingletonInstance as MyService).MyHostEvent += new MyHostEventHandler(Form1_MyHostEvent);
    


     
    An alternative to doing this is to set up delegates inside the ServiceHost object, but that requires a tighter coupling between the object that contains the ServiceHost object, and the ServiceHost object itself.
     
    Note: Special thanks goes out to Nish for showing me the appropriate ServiceBehavior attribute

  • 相关阅读:
    [Flash开发笔记] ActionScript 生成伪 Guid
    [Flash开发笔记] 如何在as2.0中使用自定义类事件
    vs2005 智能感知不正常的解决办法
    [Flash开发笔记] 自定义ActionScript中的trim函数,取回车函数,字节换算函数
    爱你不容易——ExternalInterface
    软件测试中开发团队和测试团队的职责
    [Flash开发笔记] 正确理解MovieClipLoader的onLoadComplete事件
    ActionScript 中的字符串替换函数
    “hello world”PHP
    使用"类型文件"(typed File),创建自己的"数据库"
  • 原文地址:https://www.cnblogs.com/zeroone/p/2543783.html
Copyright © 2011-2022 走看看