列表5.25 实现了对在服务端配置中安装的软件授权码的一个验证行为。如果它不存在或者它不合法,服务将不会启动。它显示了服务运行时创建的用来验证配置信息的一个终 结点行为。它也显示了当服务运行时创建时调用的一个行为扩展和扩展是如何将行为加入到服务运行时的。结果就是一个自定义行为在配置文件中 (app.config 或者web.config)使用并被添加到服务运行时中以便于配置信息可以在服务启动时被验证。
类myServiceBehavior实现了IServiceBehavior接口。类有两个属性,_EvaluationKey和_EvaluationType.myEndpointBehavior将这些值与预定义值比较。
类myBehaviorExtensionElement 实现了IBehaviorExtensionElement接口。它定义了两个可以再配置文件中表现的 [ConfigurationProperties]。它重载了BehaviorType和CreateBehavior方法以便于它可以返回同时在运行 时启动时创建自定义行为myServiceBehavior。myServiceBehavior的构造函数有两个参数,每个属性一个,所以它可以执行验 证操作。
列表5.25 在配置文件中暴露的终结点行为
01 | public class myServiceBehavior : IServiceBehavior |
03 | string _evaluationKey; |
04 | string _evaluationType; |
05 | public myServiceBehavior( string evaluationKey, string evaluationType) |
07 | _evaluationKey = evaluationKey; |
08 | _evaluationType = evaluationType; |
11 | public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) |
15 | public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase) |
19 | public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase) |
21 | if ((_evaluationType == "Enterprise" ) & (_evaluationKey != "SuperSecretEvaluationKey" )) |
23 | throw new Exception( string .Format( "Invalid evaludation key.Type:{0}" , _evaluationType)); |
01 | public class myBehaviorExtensionElement : BehaviorExtensionElement |
03 | [ConfigurationProperty( "EvaluationKey" , DefaultValue = "" , IsRequired = true )] |
04 | public string EvaluationKey |
06 | get { return ( string ) base [ "evaluationKey" ]; } |
07 | set { base [ "evaluationKey" ] = value; } |
09 | [ConfigurationProperty( "EvaluationType" , DefaultValue = "Enterprise" , IsRequired = false )] |
10 | public string EvaluationType |
12 | get { return ( string ) base [ "evaluationType" ]; } |
13 | set { base [ "evaluationType" ] = value; } |
16 | public override Type BehaviorType |
20 | return typeof (myServiceBehavior); |
24 | protected override object CreateBehavior() |
26 | return new myServiceBehavior(EvaluationKey, EvaluationType); |
02 | public interface IStockService |
05 | double GetPrice( string ticker); |
08 | public class StockService : IStockService |
10 | public double GetPrice( string ticker) |
列表5.26 显示了服务端的配置文件。在配置文件中添加了<behaviorExtension>,指向扩展实现。注意实现是强命名的,包括它的类型名和程序集信息(名称,版本,文化和公共密钥)。在这个例子中,程序集名字和存储扩展的DLL就是服务。
列表5.26 暴露一个终结点行为的配置文件
01 | <?xml version= "1.0" encoding= "utf-8" ?> |
07 | <add name= "FreeTrial" type= "Services.myBehaviorExtensionElement, Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2c0e9c165e34d972" /> |
11 | <service name= "Services.StockService" behaviorConfiguration= "customBehavior" > |
12 | <endpoint address= "" binding= "basicHttpBinding" bindingConfiguration= "" |
13 | contract= "Services.IStockService" /> |
23 | <behavior name= "customBehavior" > |
24 | <FreeTrial EvaluationKey= "SuperSecretEvaluationKey" EvaluationType= "Enterprise" /> |
28 | </system.serviceModel> |
=======
转载自