<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                終于我又看完了二期愛情保衛戰,太酸爽了,推薦鏈接:[http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd](http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd),不多說,誰看誰入迷,下面言歸正傳, 看看這個很有意思的Behavior。 ## 一: Behavior這個潑婦的厲害   ? 在前面的文章中,我也清楚的說明了整個wcf通信流,而Behavior這個潑婦可以在wcf通信流中的任何地方插上一腳,蠻狠無比,利用的好,讓你上天堂,利用的不 好,讓你下地獄。。。下面讓你看看behavior到底有哪些可以注入的點???先畫個簡圖:![](https://box.kancloud.cn/2015-08-04_55c0b4ecf236b.png) 上面的圖,大概就是wcf的通信簡圖,所有藍色字體都是Behavior注入的點,其中Client和Service端都可以注入,如果按照功能分的話,又可以分為“操作級別”和 ”端點級別“,下面我來簡要的分解下。 ## 二:端點級別Behavior   從圖中你也可以看到,消息檢查器是放在Channel這個級別的,也就是說它可以監視Client和Server的入站請求,也就是說所有的請求都需要通過它轉發,如果 這樣的話,那我是不是可以在這個注入點上自由的修改,變更,攔截入站和出站請求,而且利用這個特性我還可以做很多的事情,比如日志記錄,記錄統計等等,下 面我們來看看這個怎么使用??? 只需要extends?IEndpointBehavior ?和?IDispatchMessageInspector,然后加入EndpointBehaviors即可。。。 ?1.?IDispatchMessageInspector ~~~ public class MyDispatchMessageInspector : IDispatchMessageInspector { public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { Console.WriteLine(request.ToString()); return request; } public void BeforeSendReply(ref Message reply, object correlationState) { Console.WriteLine(reply.ToString()); } } ~~~ 2.?IEndpointBehavior ~~~ public class MyEndpointBehavior : IEndpointBehavior { public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) { } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) { endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyDispatchMessageInspector()); } public void Validate(ServiceEndpoint endpoint) { } } ~~~ 3\. 將MyEndpointBehavior加入到Host中 ~~~ static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://127.0.0.1:1920")); host.AddServiceEndpoint(typeof(IHomeService), new BasicHttpBinding(), "HomeServie"); host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host.Description.Endpoints[0].EndpointBehaviors.Add(new MyEndpointBehavior()); host.Open(); Console.WriteLine("服務已經開啟!!!"); Console.Read(); } ~~~ 4\. 最后我們看一下服務方法 ~~~ public class HomeService : IHomeService { public string Update(string message) { Console.WriteLine("我在Action方法:" + message); return "my reply!!!"; } } ~~~ 下面看看效果。。。在效果圖中,你應該看到了。在我的Action中的方法前后各有一段“入站消息”和“出站消息”,是不是很爽??? ![](https://box.kancloud.cn/2015-08-04_55c0b4eeb97e7.png) ## 三:操作級別Behavior   從文章開頭的簡圖中,你應該看到了,Operation級別的Behavior比較多,有“操作啟動器(IOperationInvoker)","參數檢查(IParameterInspector)“, “消息格式化器(IDispatchMessageFormatter)”等等。。。 為什么說等等這個詞,很簡單啊,,,其實還有很多系統內置的,既然是Operation,那就必 然是針對方法的,還記得OperationContract是怎么套在方法上的嗎??? 是特性,對吧,,,同樣的道理,OperationBehavior也是一樣,那怎么用呢?? 同樣也是很簡單的,繼承幾個接口即可。。。 ??IParameterInspector 的玩法 ? ?其實沒什么好說的,既然是屬于Operation下面的Behavior,那都是通過特性注入的,而這個IParameterInspector,可以做到類似Mvc的Model驗證,下面 我做個簡單的Action參數長度驗證(長度不超過8個字符)。 1.?IParameterInspector ~~~ public class MyIParameterInspector : IParameterInspector { public int MaxLength { get; set; } public MyIParameterInspector(int MaxLength) { this.MaxLength = MaxLength; } /// <summary> /// 出站的操作 /// </summary> /// <param name="operationName"></param> /// <param name="outputs"></param> /// <param name="returnValue"></param> /// <param name="correlationState"></param> public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) { } /// <summary> /// 入站的參數 /// </summary> /// <param name="operationName"></param> /// <param name="inputs"></param> /// <returns></returns> public object BeforeCall(string operationName, object[] inputs) { foreach (var item in inputs) { if (Convert.ToString(item).Length > MaxLength) { throw new Exception("碼單,長度不能超過 " + MaxLength + " 個長度"); } } return null; } } ~~~ 2.?IOperationBehavior ~~~ public class MyOperationBehavior : Attribute, IOperationBehavior { public int MaxLength { get; set; } public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { } public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) { dispatchOperation.ParameterInspectors.Add(new MyIParameterInspector(MaxLength)); } public void Validate(OperationDescription operationDescription) { } } ~~~ 3\. 在Action在加上MyOperationBehavior 這個 Attribute ~~~ public class HomeService : IHomeService { [MyOperationBehavior(MaxLength = 5)] public string Update(string message) { Console.WriteLine("我在Action方法:" + message); return "my reply!!!"; } } ~~~ 4\. 然后我在客戶端故意輸入大于5的字符,看看效果怎么樣??? ~~~ public class Program1 { static void Main(string[] args) { HomeServiceClient client = new HomeServiceClient(); client.Update("我故意輸入了很多的字符,哈哈。。。。。"); Console.Read(); } } ~~~ 5\. 最后看看效果圖,可以看到,最終的入站消息會拋出一個異常。。。 ?![](https://box.kancloud.cn/2015-08-04_55c0b4f128e9e.png) ?MessageFormatter,IOperationInvoker?的玩法    剩下的這兩個玩法都差不多,你只需要extends一下,然后加入到OperationBehavior即可,有了上面的思想,我想下面這些使用起來都不是問題吧。。。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看