<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      WCF進(jìn)階:擴(kuò)展bindingElementExtensions支持對(duì)稱加密傳輸

          前面兩篇文章WCF進(jìn)階:將編碼后的字節(jié)流壓縮傳輸WCF 進(jìn)階: 對(duì)稱加密傳輸都是實(shí)現(xiàn)了自定義編碼,那兩個(gè)例子中托管服務(wù)或者客戶端調(diào)用都采用的代碼實(shí)現(xiàn),WCF更友好的方式是在app.config或web.config來配置服務(wù)的運(yùn)行和調(diào)用,本文是介紹如何在配置文件中配置自定義的BindingElement。

          上文WCF 進(jìn)階: 對(duì)稱加密傳輸中我們實(shí)現(xiàn)了一個(gè)自定義的BindingElement:CryptEncodingBindingElement,它是一個(gè)MessageEncodingBindingElement,在宿主程序中,我們通過代碼的方式將其添加到CustomBinding中去,方法為:

      ICollection<BindingElement> bindingElements = new List<BindingElement>();
      HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement();
      string key = "JggkieIw7JM=";
      string iv = "XdTkT85fZ0U=";
      CryptEncodingBindingElement textBindingElement = new CryptEncodingBindingElement(new BinaryMessageEncodingBindingElement(), key, iv);
      bindingElements.Add(textBindingElement);
      bindingElements.Add(httpBindingElement); 
      CustomBinding bind = new CustomBinding(bindingElements);  

      如果是缺省的BindingElement,如TextMessageEncodingElement,HttpTransportElement,ReliableSessionElement,SecurityElement都可以像如下這樣的方式進(jìn)行配置:

      <bindings>
        <customBinding>
          <binding name="myBinding">
            <textMessageEncoding/>
            <reliableSession/>
            <security/>
            <httpTransport/>
          </binding>
        </customBinding>
      </bindings>

      如果要想讓我們自定義的BindingElement和上面的一樣享受同等待遇的話,我們需要再額外做一些事情,那就是重載BindingElementExtensionElement,在.Net中擴(kuò)展配置的基類為:ConfigurationElement,它定義聲明了一些擴(kuò)展配置的屬性和方法,為了方便WCF重載了ConfigurationElement形成了ServiceModelExtensionElement,而為了進(jìn)一步的方便擴(kuò)展BindingElement,又提供了BindingElementExtensionElement的基類,創(chuàng)建自定義的BindingElementExtension,只需要重載BindingElementExtensionElement三個(gè)方法:

      using System;
      using System.ServiceModel.Channels;
      
      namespace System.ServiceModel.Configuration
      {
          // 摘要:
          //     為使用計(jì)算機(jī)或應(yīng)用程序配置文件中的自定義 System.ServiceModel.Channels.BindingElement 實(shí)現(xiàn)提供支持。
          public abstract class BindingElementExtensionElement : ServiceModelExtensionElement
          {
              // 摘要:
              //     初始化 System.ServiceModel.Configuration.BindingElementExtensionElement 類的新實(shí)例。
              protected BindingElementExtensionElement();
      
              // 摘要:
              //     在派生類中重寫時(shí),獲取表示自定義綁定元素的 System.Type 對(duì)象。
              //
              // 返回結(jié)果:
              //     一個(gè)表示自定義綁定類型的 System.Type 對(duì)象。
              public abstract Type BindingElementType { get; }
      
              // 摘要:
              //     將指定綁定元素的內(nèi)容應(yīng)用到此綁定配置元素。
              //
              // 參數(shù):
              //   bindingElement:
              //     一個(gè)綁定元素。
              //
              // 異常:
              //   System.ArgumentNullException:
              //     bindingElement 為 null。
              public virtual void ApplyConfiguration(BindingElement bindingElement);
              //
              // 摘要:
              //     在派生類中重寫時(shí),返回一個(gè)自定義綁定元素對(duì)象。
              //
              // 返回結(jié)果:
              //     一個(gè)自定義 System.ServiceModel.Channels.BindingElement 對(duì)象。
              protected internal abstract BindingElement CreateBindingElement();
              //
              // 摘要:
              //     使用指定綁定元素的內(nèi)容來初始化此綁定配置節(jié)。
              //
              // 參數(shù):
              //   bindingElement:
              //     一個(gè)綁定元素。
              protected internal virtual void InitializeFrom(BindingElement bindingElement);
          }
      }

      public abstract Type BindingElementType { get; } 這個(gè)屬性中只需要返回自定義類的類型,protected internal abstract BindingElement CreateBindingElement()的重載就是在這創(chuàng)建需要的自定義BindingElement,如果自定義的BindingElement有額外的屬性或者參數(shù),我們可以還可以通過創(chuàng)建帶有ConfigurationPropertyAttribute的屬性來指定。這個(gè)是.Net配置中的通性。在我們的需求中,需要一個(gè)CryptEncodingBindingElement實(shí)例,而CryptEncodingBindingElement帶有三個(gè)參數(shù):InnerMessageEncodingBindingElement,Key,IV。分析到這,我們的自定義BindingElement配置擴(kuò)展類就成形了,代碼如下:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.ServiceModel.Configuration;
      using System.Configuration;
      
      namespace RobinLib
      {
          public class CryptEncodingBindingElementConfiguration : BindingElementExtensionElement
          {
              public override void ApplyConfiguration(System.ServiceModel.Channels.BindingElement bindingElement)
              {
                  CryptEncodingBindingElement bind = bindingElement as CryptEncodingBindingElement;
                  if (InnerMessageEncoding.ToLower() == "text")
                  {
                      bind.InnerMessageEncodingBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
                  }
                  else if (InnerMessageEncoding.ToLower() == "binary")
                  { 
                      bind.InnerMessageEncodingBindingElement = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement();
                  }
                  else if (InnerMessageEncoding.ToLower() == "mtom")
                  {
                      bind.InnerMessageEncodingBindingElement = new System.ServiceModel.Channels.MtomMessageEncodingBindingElement();
                      
                  }
                  bind.Key = Key;
                  bind.IV = IV;
                  base.ApplyConfiguration(bindingElement);
              }
      
      
              [ConfigurationProperty("innerMessageEncoding",DefaultValue="text")]
              public string InnerMessageEncoding
              {
                  get
                  {
                      return base["innerMessageEncoding"] as string;
                  }
                  set
                  {
                      base["innerMessageEncoding"] = value;
                  }
              }
      
      
              [ConfigurationProperty("key", DefaultValue = "")]
              public string Key
              {
                  get
                  {
                      return base["key"] as string;
                  }
                  set
                  {
                      base["key"] = value;
                  }
              }
      
              [ConfigurationProperty("iv", DefaultValue = "")]
              public string IV
              {
                  get
                  {
                      return base["iv"] as string;
                  }
                  set
                  {
                      base["iv"] = value;
                  }
              }
      
              public override Type BindingElementType
              {
                  get { return typeof(CryptEncodingBindingElementConfiguration); }
              }
      
              protected override System.ServiceModel.Channels.BindingElement CreateBindingElement()
              {
                  if (InnerMessageEncoding.ToLower() == "text")
                  {
                      CryptEncodingBindingElement bindElement = new CryptEncodingBindingElement(new System.ServiceModel.Channels.TextMessageEncodingBindingElement(), Key, IV);
                      return bindElement;
                  }
                  else if (InnerMessageEncoding.ToLower() == "binary")
                  {
                      CryptEncodingBindingElement bindElement = new CryptEncodingBindingElement(new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement(), Key, IV);
                      return bindElement;
                  }
                  else if (InnerMessageEncoding.ToLower() == "mtom")
                  {
                      CryptEncodingBindingElement bindElement = new CryptEncodingBindingElement(new System.ServiceModel.Channels.MtomMessageEncodingBindingElement(), Key, IV);
                      return bindElement;
                  }
                  throw new Exception("只支持Text,Binary,MTOM三種內(nèi)置編碼器!");
              }
          }
      }

      重載實(shí)現(xiàn)之后,我們就可以使用它在App.Config或者Web.Config來配置BindingElement,使用的方法為:

      1. 在<system.serviceModel><extensions> <bindingElementExtensions>中添加新的配置節(jié)點(diǎn)的綁定。

      2. 創(chuàng)建新的注冊(cè)后的節(jié)點(diǎn)到自定義綁定中。

      來看一下服務(wù)的配置文件吧:

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <system.serviceModel>
          <services>
            <clear/>
            <service name="Robin_Wcf_CustomMessageEncoder_SvcLib.Service1">
              <host>
                <baseAddresses>
                  <add baseAddress="http://127.0.0.1:8081/"/>
                </baseAddresses>
              </host>
              <endpoint address="Robin_Wcf_Formatter" name="ep" contract="Robin_Wcf_CustomMessageEncoder_SvcLib.IService1"  binding="customBinding" bindingConfiguration="myBinding"></endpoint>
            </service>
          </services>
          <bindings>
            <customBinding>
              <binding name="myBinding">
                <cryptMessageEncoding key="JggkieIw7JM=" iv="XdTkT85fZ0U=" innerMessageEncoding="Binary"/>
                <httpTransport/>
              </binding>
            </customBinding>
          </bindings>
          <extensions> 
            <bindingElementExtensions>
              <add name="cryptMessageEncoding" type="RobinLib.CryptEncodingBindingElementConfiguration,RobinLib,Version=1.0.0.0, Culture=neutral,PublicKeyToken=null"/>
            </bindingElementExtensions>
          </extensions>
        </system.serviceModel>
      </configuration>

      托管服務(wù)的代碼就輕便了好多,如下:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.ServiceModel;
      using Robin_Wcf_CustomMessageEncoder_SvcLib;
      using System.ServiceModel.Channels;
      using RobinLib;
      
      namespace Robin_Wcf_CustomMessageEncoder_Host
      {
          class Program
          {
              static void Main(string[] args)
              { 
                  ServiceHost host = new ServiceHost(typeof(Service1)); 
                  if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)
                  {
                      System.ServiceModel.Description.ServiceMetadataBehavior svcMetaBehavior = new System.ServiceModel.Description.ServiceMetadataBehavior();
                      svcMetaBehavior.HttpGetEnabled = true;
                      svcMetaBehavior.HttpGetUrl = new Uri("http://127.0.0.1:8001/Mex");
                      host.Description.Behaviors.Add(svcMetaBehavior);
                  }
                  host.Opened += new EventHandler(delegate(object obj, EventArgs e)
                  {
                      Console.WriteLine("服務(wù)已經(jīng)啟動(dòng)!");
                  }); 
                  host.Open();
                  Console.Read();
              }
          }
      }

      運(yùn)行項(xiàng)目,能得到和WCF 進(jìn)階: 對(duì)稱加密傳輸中一致的輸出!

      posted @ 2010-04-17 23:45  Robin Zhang  閱讀(4801)  評(píng)論(6)    收藏  舉報(bào)
      主站蜘蛛池模板: 日韩精品人妻av一区二区三区| 激情五月开心婷婷深爱| 丽水市| 亚洲av在线观看| 国产亚洲欧洲av综合一区二区三区| 久久久久人妻一区精品| 成人午夜大片免费看爽爽爽| 国产尤物精品自在拍视频首页| 久久99日韩国产精品久久99| 国产午夜福利视频合集| 在线播放亚洲成人av| 毛片av在线尤物一区二区| 精品国产一区av天美传媒| 江门市| 最新亚洲人成网站在线观看| 1000部精品久久久久久久久| 韩国青草无码自慰直播专区| 国产午夜福利片在线观看| 亚洲第一狼人成人综合网| 国产日产亚洲系列av| 高中女无套中出17p| 三人成全免费观看电视剧高清| 宁南县| 精品国产女同疯狂摩擦2| 丰满人妻无码∧v区视频 | 美女一区二区三区在线观看视频| 视频一区二区三区在线视频| 国精品午夜福利视频不卡| 国产成人剧情AV麻豆果冻| 国产精品久久久福利| 日韩精品成人一区二区三| 亚洲免费视频一区二区三区| 亚洲中文字幕一区二区| 91高清免费国产自产拍| 亚洲国产精品毛片在线看| 亚洲国产成人片在线观看无码| 中文天堂资源| 亚洲日韩AV秘 无码一区二区| 我要看亚洲黄色太黄一级黄| 欧美 亚洲 国产 制服 中文| 成人午夜在线播放|