WCF编程系列(六)以编程方式配置终结点

    示例一中我们的宿主程序非常简单:只是简单的实例化了一个ServiceHost对象,然后调用open方法来启动服务。而关于终结点的配置我们都是通过配置文件来完成的。在本讲中,我们将直接使用编程方式来配置终结点。

ServiceHost类

    即服务宿主类,它为我们的服务提供一个运行环境。ServiceHost的构造函数有三个重载:

    ServiceHost()  使用无参数的构造必须要有相应的配置文件设置。

    ServiceHost(Object,Uri[]) Object参数为一个服务类的实例(如示例一中的FirstService类),Uri[]参数指定服务终结点的默认基地址,对应于我们配置文件中的<host><baseAddress>配置节点下设置。此构造函数使用于”单实例”的服务。

    ServiceHost(Type,Uri[]) Type参数指定服务的类型(使用typeof获取),对应于配置文件中<service>配置节点的name属性,如示例一中的<service name="Xfrog.Study.WCF.FirstService"…>,Uri[]参数指定服务终结点的默认基地址。

ServiceHost.AddServiceEndPoint方法

    顾名思义,此方法用于向宿主添加一个服务终结点,该方法有8个重载,但实际上可区分为两个版本:三参数版本和四参数版本,三参数版本中,三个参数依次传入终结点的契约类型、绑定及地址,其中契约类型可以使用String类型传入服务契约的全局限定名(如示例一中的"Xfrog.Study.WCF.IFirstService",也可以使用typeof操作符来传入契约的类型(Type);绑定参数传入一个Binding对象,该对象可以通过预定义绑定类型来实例化;地址参数可以使用String类型传入也可以使用Uri类型实例传入,与使用配置文件相似,地址可使用绝对地址或相对地址。由于第一、三参数分别有两个重载,所以三参数版本对应有6个重载版本。

    四参数的版本,除了前述三个参数外,第四个参数为一个Uri类型,该地址表示一个服务的侦听地址,即服务通过该地址来侦听客户端的请求。所以四参数的版本实际上是提供了“多个终结点在同一个地址上侦听”的功能。

示例三

通过编程方式为示例一添加终结点:

1.复制实例一文件夹,将其改名为XfrogWCFStudy003

2.打开Host项目下的App.config文件,屏蔽掉配置内容:

隐藏行号 复制代码 App.config
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <!--
  4.   <system.serviceModel>
  5.     <services>
  6.       <service name="Xfrog.Study.WCF.FirstService" behaviorConfiguration="behaviorConfiguration">
  7.         <host>
  8.           <baseAddresses>
  9.             <add baseAddress="http://localhost:8000/"/>
  10.           </baseAddresses>
  11.         </host>
  12.         <endpoint address="" binding="basicHttpBinding" contract="Xfrog.Study.WCF.IFirstService"></endpoint>
  13.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
  14.       </service>
  15.     </services>
  16.     <behaviors>
  17.       <serviceBehaviors>
  18.         <behavior name="behaviorConfiguration">
  19.           <serviceMetadata httpGetEnabled="falses"/>
  20.         </behavior>
  21.       </serviceBehaviors>
  22.     </behaviors>
  23.   </system.serviceModel>
  24.   -->
  25. </configuration>

3.修改Host项目Program.cs代码如下:

隐藏行号 复制代码 Program.cs
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Text;
    
  4. using System.ServiceModel;
    
  5. using Xfrog.Study.WCF;
    
  6. 
    
  7. namespace Host
    
  8. {
    
  9.     class Program
    
  10.     {
    
  11.         static void Main(string[] args)
    
  12.         {
    
  13.             using (ServiceHost host = new ServiceHost(typeof(FirstService)))
    
  14.             {
    
  15.                 host.AddServiceEndpoint("Xfrog.Study.WCF.IFirstService", new BasicHttpBinding(), "http://localhost:8000/");
    
  16.                 host.AddServiceEndpoint(typeof(IFirstService), new NetTcpBinding(), new Uri("net.tcp://localhost:8001"));
    
  17.                 host.AddServiceEndpoint(typeof(IFirstService), new NetNamedPipeBinding(), "net.pipe://localhost/");
    
  18. 
    
  19.                 host.Open();
    
  20.                 Console.WriteLine("服务已启动,按任意键中止...");
    
  21.                 Console.ReadKey(true);
    
  22.                 host.Close();
    
  23.             }
    
  24.         }
    
  25.     }
    
  26. }
    
  27. 
    

示例四

1.通过编程方式启用元数据

2.获取终结点信息

Program.cs代码如下,请参照MSDN了解类的使用方法

隐藏行号 复制代码 Program.cs
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Text;
    
  4. using System.ServiceModel;
    
  5. using Xfrog.Study.WCF;
    
  6. using System.ServiceModel.Description;
    
  7. 
    
  8. namespace Host
    
  9. {
    
  10.     class Program
    
  11.     {
    
  12.         static void Main(string[] args)
    
  13.         {
    
  14.             using (ServiceHost host = new ServiceHost(typeof(FirstService)))
    
  15.             {
    
  16.                 //启用元数据
    
  17.                 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    
  18.                 smb.HttpGetUrl = new Uri("http://localhost:8000/mex");
    
  19.                 smb.HttpGetEnabled = true;
    
  20.                 host.Description.Behaviors.Add(smb);
    
  21. 
    
  22.                 host.AddServiceEndpoint("Xfrog.Study.WCF.IFirstService", new BasicHttpBinding(), "http://localhost:8000/");
    
  23.                 host.AddServiceEndpoint(typeof(IFirstService), new NetTcpBinding(), new Uri("net.tcp://localhost:8001"));
    
  24.                 host.AddServiceEndpoint(typeof(IFirstService), new NetNamedPipeBinding(), "net.pipe://localhost/");
    
  25. 
    
  26.                 host.Open();
    
  27. 
    
  28.                 //获取终结点信息
    
  29.                 foreach (ServiceEndpoint point in host.Description.Endpoints)
    
  30.                 {
    
  31.                     Console.WriteLine("终结点:");
    
  32.                     Console.WriteLine("\t地址: {0}", point.Address.ToString());
    
  33.                     Console.WriteLine("\t绑定: {0}", point.Binding.ToString());
    
  34.                     Console.WriteLine("\t契约: {0}", point.Contract.ToString());
    
  35.                     KeyedByTypeCollection<IEndpointBehavior> behaviors = point.Behaviors;
    
  36.                     foreach (IEndpointBehavior behavior in behaviors)
    
  37.                     {
    
  38.                         Console.WriteLine("Behavior: {0}", behavior.ToString());
    
  39.                     }
    
  40.                 }
    
  41.                 Console.WriteLine("服务已启动,按任意键中止...");
    
  42.                 Console.ReadKey(true);
    
  43.                 host.Close();
    
  44.             }
    
  45.         }
    
  46.     }
    
  47. }
    
  48. 
    
原文地址:https://www.cnblogs.com/xfrog/p/1737501.html