• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • WCF服務的批量寄宿(2)

    發表于:2012-02-09來源:博客園作者:Artech點擊數: 標簽:WCF
    6: foreach (ServiceTypeElement element in settings.ServiceTypes) 7: { 8: this.Add(element.ServiceType); 9: } 10: 11: if (null != serviceTypes) 12: { 13: Array.ForEach(serviceTypes, serviceType= this.A

      6: foreach (ServiceTypeElement element in settings.ServiceTypes)

      7: {

      8: this.Add(element.ServiceType);

      9: }

      10:

      11: if (null != serviceTypes)

      12: {

      13: Array.ForEach(serviceTypes, serviceType=> this.Add(new ServiceHost(serviceType)));

      14: }

      15: }

      16: public void Add(params Type[] serviceTypes)

      17: {

      18: if (null != serviceTypes)

      19: {

      20: Array.ForEach(serviceTypes, serviceType => this.Add(new ServiceHost(serviceType)));

      21: }

      22: }

      23: public void Open()

      24: {

      25: foreach (ServiceHost host in this)

      26: {

      27: host.Open();

      28: }

      29: }

      30: public void Dispose()

      31: {

      32: foreach (IDisposable host in this)

      33: {

      34: host.Dispose();

      35: }

      36: }

      37: }

      定義在ServiceHostCollection中的Open方法實現了對所有ServiceHost對象的批量開啟。ServiceHostCollection還實現了IDisposable接口,并在Dispose方法中實現了對ServiceHost的批量關閉。

      現在我們定義了FooService、BarService和BazService三個服務類型,它們分別實現了契約接口IFoo、IBar和IBar。三個服務以及包含的終結點定義在如下的配置中,而三個服務類型同時被定義在了我們自定義的配置節下。

      1:

      2:

      3:

      4: type="Artech.BatchingHosting.Configuration.BatchingHostingSettings,

      5: Artech.BatchingHosting"/>

      6:

      7:

      8:

      9:

      10:

      11: binding="ws2007HttpBinding"

      12: contract="Artech.BatchingHosting.IFoo"/>

      13:

      14:

      15:

      16: binding="ws2007HttpBinding"

      17: contract="Artech.BatchingHosting.IBar"/>

      18:

      19:

      20:

      21: binding="ws2007HttpBinding"

      22: contract="Artech.BatchingHosting.IBaz"/>

      23:

      24:

      25:

      26:

      27:

      28:

      29:

      30:

      31:

      要實現針對三個服務的批量寄宿,我們只需要創建ServiceHostCollection對象并開啟它即可。為了確認三個服務對應的ServiceHost確實被創建并被開啟,我通過如下的代碼注冊了ServiceHostCollection中每個ServiceHost的Opened事件。當該事件觸發時,會在控制臺上打印一段文字。

      1: using (ServiceHostCollection hosts = new ServiceHostCollection())

      2: {

      3: foreach (ServiceHost host in hosts)

      4: {

      5: host.Opened += (sender, arg) => Console.WriteLine("服務{0}開始監聽",

      6: (sender as ServiceHost).Description.ServiceType);

      7: }

      8: hosts.Open();

      9: Console.Read();

      10: }

      上面這段代碼執行之后,控制臺上將會具有如下一段輸出文字,這充分證明了我們對三個服務成功地進行了批量寄宿。

      1: 服務Artech.BatchingHosting.FooService開始監聽

      2: 服務Artech.BatchingHosting.BarService開始監聽

      3: 服務Artech.BatchingHosting.BazService開始監聽

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>