zoukankan      html  css  js  c++  java
  • WCF 第五章 行为 通过配置文件暴露一个服务行为

    列表5.25 实现了对在服务端配置中安装的软件授权码的一个验证行为。如果它不存在或者它不合法,服务将不会启动。它显示了服务运行时创建的用来验证配置信息的一个终 结点行为。它也显示了当服务运行时创建时调用的一个行为扩展和扩展是如何将行为加入到服务运行时的。结果就是一个自定义行为在配置文件中 (app.config 或者web.config)使用并被添加到服务运行时中以便于配置信息可以在服务启动时被验证。

      类myServiceBehavior实现了IServiceBehavior接口。类有两个属性,_EvaluationKey和_EvaluationType.myEndpointBehavior将这些值与预定义值比较。

      类myBehaviorExtensionElement 实现了IBehaviorExtensionElement接口。它定义了两个可以再配置文件中表现的 [ConfigurationProperties]。它重载了BehaviorType和CreateBehavior方法以便于它可以返回同时在运行 时启动时创建自定义行为myServiceBehavior。myServiceBehavior的构造函数有两个参数,每个属性一个,所以它可以执行验 证操作。

    列表5.25 在配置文件中暴露的终结点行为

    01public class myServiceBehavior : IServiceBehavior
    02   {
    03       string _evaluationKey;
    04       string _evaluationType;
    05       public myServiceBehavior(string evaluationKey, string evaluationType)
    06       {
    07           _evaluationKey = evaluationKey;
    08           _evaluationType = evaluationType;
    09       }
    10 
    11       public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    12       {
    13       }
    14 
    15       public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    16       {
    17       }
    18 
    19       public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    20       {
    21           if ((_evaluationType == "Enterprise") & (_evaluationKey != "SuperSecretEvaluationKey"))
    22           {
    23               throw new Exception(string.Format("Invalid evaludation key.Type:{0}", _evaluationType));
    24           }
    25       }
    26   }
    01public class myBehaviorExtensionElement : BehaviorExtensionElement
    02{
    03    [ConfigurationProperty("EvaluationKey", DefaultValue = "", IsRequired = true)]
    04    public string EvaluationKey
    05    {
    06        get { return (string)base["evaluationKey"]; }
    07        set { base["evaluationKey"] = value; }
    08    }
    09    [ConfigurationProperty("EvaluationType", DefaultValue = "Enterprise", IsRequired = false)]
    10    public string EvaluationType
    11    {
    12        get { return (string)base["evaluationType"]; }
    13        set { base["evaluationType"] = value; }
    14    }
    15 
    16    public override Type BehaviorType
    17    {
    18        get
    19        {
    20            return typeof(myServiceBehavior);
    21        }
    22    }
    23 
    24    protected override object CreateBehavior()
    25    {
    26        return new myServiceBehavior(EvaluationKey, EvaluationType);
    27    }
    28}
    01[ServiceContract]
    02public interface IStockService
    03{
    04    [OperationContract]
    05    double GetPrice(string ticker);
    06}
    07 
    08public class StockService : IStockService
    09{
    10    public double GetPrice(string ticker)
    11    {
    12        if (ticker == "MSFT")
    13        {
    14            return 94.85;
    15        }
    16        else
    17        {
    18            return 0.0;
    19        }
    20    }
    21}

      列表5.26 显示了服务端的配置文件。在配置文件中添加了<behaviorExtension>,指向扩展实现。注意实现是强命名的,包括它的类型名和程序集信息(名称,版本,文化和公共密钥)。在这个例子中,程序集名字和存储扩展的DLL就是服务。

    列表5.26 暴露一个终结点行为的配置文件

    01<?xml version="1.0" encoding="utf-8" ?>
    02<configuration>
    03  <system.serviceModel>
    04    <behaviors />
    05    <extensions>
    06      <behaviorExtensions>
    07        <add name="FreeTrial" type="Services.myBehaviorExtensionElement, Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2c0e9c165e34d972" />
    08      </behaviorExtensions>
    09    </extensions>
    10    <services>
    11      <service name="Services.StockService" behaviorConfiguration="customBehavior">
    12        <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
    13            contract="Services.IStockService" />
    14        <host>
    15          <baseAddresses>
    16            <add baseAddress="http://localhost:8000/EssentialWCF" />
    17          </baseAddresses>
    18        </host>
    19      </service>
    20    </services>
    21    <behaviors>
    22      <serviceBehaviors>
    23        <behavior name="customBehavior">
    24          <FreeTrial EvaluationKey="SuperSecretEvaluationKey" EvaluationType="Enterprise"/>
    25        </behavior>
    26      </serviceBehaviors>
    27    </behaviors>
    28  </system.serviceModel>
    29</configuration>


    =======

    转载自

     

  • 相关阅读:
    简单的远程控制软件
    VS集成环境中的JavaScript脚本语法检查
    vs2022安装
    有关httpContext.Current.Session[值] 取值的问题
    【python3.7】文件操作
    148. 排序链表
    11. 盛最多水的容器
    23. 合并K个升序链表
    147. 对链表进行插入排序
    146. LRU 缓存机制
  • 原文地址:https://www.cnblogs.com/llbofchina/p/2094107.html
Copyright © 2011-2022 走看看