• [WCF编程]10.操作:事件


    一、事件概述

        基础的WCF回调机制并不能阐明客户端与服务之间交互的本质。双向回调的规范使用可以通过事件来完成。客户端发生的相关事项都可以通过事件通知客户端或者多个客户端。事件可能源于直接的客户端调用,也可能来源于服务监听器。激活事件的服务称为发布者,而接受事件答得客户端则称为订阅者。如下图所示:

    image

        与回调操作相比,WCF更重视对事件的运作。从本质讲,事件代表了发布者与订阅者之间更加松散的关系,他优于客户端和服务之间的关系。处理事件时,服务通常会为多个订阅客户端发布同样的事件。发布者一般不会考虑订阅者的回调数顺序,也不会考虑订阅者在处理事件时可能出现的错误。所有的发布者都直到它应该将事件传递给订阅者。如果事件出现问题,服务对此也束手无策。此外,服务并不关心订阅者返回的结果。因此,事件处理操作的返回值应该为void,而不需要返回值,因而应该被标记为单向操作。建议将事件分解为单独的回调契约,而不要在相同的契约中将事件和常规的回调混在一起。

    public interface IMyEvent
        {
            [OperationContract(IsOneWay=true)]
            void OnEvent1();
    
        [OperationContract(IsOneWay</span>=<span style="color: #0000ff">true</span><span style="color: #000000">)]
        </span><span style="color: #0000ff">void</span> OnEvent2(<span style="color: #0000ff">int</span><span style="color: #000000"> number);
    
        [OperationContract(IsOneWay</span>=<span style="color: #0000ff">true</span><span style="color: #000000">)]
        </span><span style="color: #0000ff">void</span> OnEvent3(<span style="color: #0000ff">int</span> number,<span style="color: #0000ff">string</span><span style="color: #000000"> text);
    }</span></pre></div>
    

        在订阅端,即使使用了单向回调操作,事件处理方法的实现也应该是执行周期较短的操作。其原因有二:第一,如果需要发布大量的事件,以至于超过了订阅者的能力,且由于队列正在处理前一个事件,无法将回调放入队列中,就会导致发布者被阻塞。阻塞了发布者就会阻止事件到达其它的订阅者;第二,如果存在大量的事件订阅者,每个订阅者所累加起来的处理事件就会操作发布者的超时值。

        发布者可以为它的契约添加专门的操作,允许客户端显式地订阅事件或取消对事件的订阅。如果发布者支持多个事件类型,也可以允许订阅者选择它们希望订阅或取消订阅的事件。

        服务如何从内部管理订阅者列表及选择它们的参数,完全属于服务端的实现细节,不会影响到客户端。发布者可以使用.NET委托管理订阅者列表和发布者自身的行为,也可以使用泛型进行管理。

    [Flags]
        public enum EvenType
        {
            Event1 = 1,
            Event2 = 2,
            Event3 = 4,
            AllEvents = Event1 | Event2 | Event3
        }
    
    [ServiceContract(CallbackContract </span>= <span style="color: #0000ff">typeof</span><span style="color: #000000">(IMyEventsCallback))]
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span><span style="color: #000000"> IMyContract
    {
        [OperationContract]
        </span><span style="color: #0000ff">void</span><span style="color: #000000"> DoSomething();
    
        [OperationContract]
        </span><span style="color: #0000ff">void</span><span style="color: #000000"> Subscribe(EvenType mask);
    
        [OperationContract]
        </span><span style="color: #0000ff">void</span><span style="color: #000000"> UnSubscribe(EvenType mask);
    }
    
    [ServiceBehavior(InstanceContextMode</span>=<span style="color: #000000">InstanceContextMode.PerCall)]
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MyPublisher : IMyContract
    {
        </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Action m_Event1 = <span style="color: #0000ff">delegate</span><span style="color: #000000"> { };
        </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Action&lt;<span style="color: #0000ff">int</span>&gt; m_Event2 = <span style="color: #0000ff">delegate</span><span style="color: #000000"> { };
        </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Action&lt;<span style="color: #0000ff">int</span>, <span style="color: #0000ff">string</span>&gt; m_Event3 = <span style="color: #0000ff">delegate</span><span style="color: #000000"> { };
    
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> Subscribe(EvenType mask)
        {
            IMyEventsCallback subscriber </span>= OperationContext.Current.GetCallbackChannel&lt;IMyEventsCallback&gt;<span style="color: #000000">();
            </span><span style="color: #0000ff">if</span> ((mask &amp; EvenType.Event1) ==<span style="color: #000000"> EvenType.Event1)
            {
                m_Event1 </span>+=<span style="color: #000000"> subscriber.OnEvent1;
            }
            </span><span style="color: #0000ff">if</span> ((mask &amp; EvenType.Event2) ==<span style="color: #000000"> EvenType.Event2)
            {
                m_Event2 </span>+=<span style="color: #000000"> subscriber.OnEvent2;
            }
            </span><span style="color: #0000ff">if</span> ((mask &amp; EvenType.Event3) ==<span style="color: #000000"> EvenType.Event3)
            {
                m_Event3 </span>+=<span style="color: #000000"> subscriber.OnEvent3;
            }
        }
    
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> UnSubscribe(EvenType mask)
        {
            IMyEventsCallback subscriber </span>= OperationContext.Current.GetCallbackChannel&lt;IMyEventsCallback&gt;<span style="color: #000000">();
            </span><span style="color: #0000ff">if</span> ((mask &amp; EvenType.Event1) ==<span style="color: #000000"> EvenType.Event1)
            {
                m_Event1 </span>-=<span style="color: #000000"> subscriber.OnEvent1;
            }
            </span><span style="color: #0000ff">if</span> ((mask &amp; EvenType.Event2) ==<span style="color: #000000"> EvenType.Event2)
            {
                m_Event2 </span>-=<span style="color: #000000"> subscriber.OnEvent2;
            }
            </span><span style="color: #0000ff">if</span> ((mask &amp; EvenType.Event3) ==<span style="color: #000000"> EvenType.Event3)
            {
                m_Event3 </span>-=<span style="color: #000000"> subscriber.OnEvent3;
            }
        }
    
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> FireEvent(EvenType eventType)
        {
            </span><span style="color: #0000ff">switch</span><span style="color: #000000"> (eventType)
            {
                </span><span style="color: #0000ff">case</span><span style="color: #000000"> EvenType.Event1:
                    {
                        m_Event1();
                        </span><span style="color: #0000ff">return</span><span style="color: #000000">;
                    }
                </span><span style="color: #0000ff">case</span><span style="color: #000000"> EvenType.Event2:
                    {
                        m_Event2(</span><span style="color: #800080">42</span><span style="color: #000000">);
                        </span><span style="color: #0000ff">return</span><span style="color: #000000">;
                    }
                </span><span style="color: #0000ff">case</span><span style="color: #000000"> EvenType.Event3:
                    {
                        m_Event3(</span><span style="color: #800080">42</span>, <span style="color: #800000">"</span><span style="color: #800000">Hello</span><span style="color: #800000">"</span><span style="color: #000000">);
                        </span><span style="color: #0000ff">return</span><span style="color: #000000">;
                    }
                </span><span style="color: #0000ff">default</span><span style="color: #000000">:
                    {
                        </span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #800000">"</span><span style="color: #800000">Unkown event type</span><span style="color: #800000">"</span><span style="color: #000000">);
                    }
            }
        }
    
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> DoSomething()
        {
            </span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span><span style="color: #000000"> NotImplementedException();
        }
    }</span></pre></div>
    

        服务契约IMyContract定义了Subscribe()和UnSubscribe()方法。这些方法接受一个枚举类型EventType,枚举类型的字段均被设置为2的指数。这就能使订阅客户端通过枚举值掩码标识事件类型是订阅还是取消订阅。例如,订阅Event1和Event3,但不订阅Event2订阅者会调用如下的Subsctibe()方法:

    class Program
        {
            static void Main(string[] args)
            {
                IMyEventCallback subscriber = new MySubscriber();
                InstanceContext context = new InstanceContext(subscriber);
                MyContractClient proxy = new MyContractClient(context);
                proxy.Subscribe(Service.EvenType.Event1 | Service.EvenType.Event3);
                proxy.DoSomething();
                proxy.Close();
                Console.Read();
    
        }
    }
    
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MySubscriber : IMyEventCallback
    {
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> OnEvent1()
        {
            Console.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">OnEvent1 Action</span><span style="color: #800000">"</span><span style="color: #000000">);
        }
    
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> OnEvent2(<span style="color: #0000ff">int</span><span style="color: #000000"> number)
        {
            Console.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">OnEvent2 Action,number={0}</span><span style="color: #800000">"</span><span style="color: #000000">,number);
        }
    
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> OnEvent3(<span style="color: #0000ff">int</span> number, <span style="color: #0000ff">string</span><span style="color: #000000"> text)
        {
            Console.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">OnEvent3 Action,number={0},text={1}</span><span style="color: #800000">"</span><span style="color: #000000">, number,text);
        }
    }</span></pre></div>
    

        MyPublisher内部维持了三个静态委托,每一个委托对应一个事件类型。

        Subscribe()与UnSubscribe()方法都检查;额传入参数EventType值,从对应的委托中添加或移除订阅者的回调。为了触发事件,MyPublisher提供了静态方法FireEvent()。他根据EventType值判断应该触发哪一个事件,然后调用对应的委托。

    示例代码:下载

  • 相关阅读:
    mysql时间日期的加、减
    IDEA 下的svn检出maven代码
    IDEA中如何显示和关闭工具栏、目录栏
    Idea集成使用SVN教程
    Python PEP8 代码规范常见问题及解决方法
    Word文档中手写签名操作说明
    19.名称空间和作用域
    18.函数的参数
    17.文件处理
    16.字符编码
  • 原文地址:https://www.cnblogs.com/zxj159/p/3993090.html
Copyright © 2020-2023  润新知