在代码中向端点行为添加行为扩展 [英] add behavior extension to endpoint behaviour in code

查看:48
本文介绍了在代码中向端点行为添加行为扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.config中添加了行为扩展名:

I add behavior extension in web.config:

<extensions>
        <behaviorExtensions>
            <add name="WebAppBeaviourElement" type="WebApp.Extensions.CustomBehaviorExtensionElement, WebApp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </behaviorExtensions>
    </extensions>

    <behaviors>
        <endpointBehaviors>
            <behavior name="WebAppBeaviour">
                <WebAppBeaviourElement />
            </behavior>
        </endpointBehaviors>
    </behaviors>

但是在Visual Studio 2010中是错误-Visual Studio不可见WebAppBehaviourElement并得到错误:/

But in Visual Studio 2010 is bug - WebAppBehaviourElement is not visible by visual studio and get error:/

所以我认为可以在代码中将此扩展添加到endpointBehaviours吗?何时在代码中应该执行此操作?

So I think that it is possible to add this extension to endpointBehaviours in code ? And when in code I should do it?

推荐答案

您可以扩展主机工厂

公共类ExtendedHostFactory:WebServiceHostFactory

然后,将行为添加到主机

Then, add behavior to the host

protected override ServiceHost CreateServiceHost(System.Type serviceType, System.Uri[] baseAddresses)
  {
    ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses) as WebServiceHost;
    host.Description.Behaviors.Add(new ValidateApiKey()); // ValidateApiKey is an IServiceBehavior
  }

您可以在配置中添加主机。如果您还为此选择了代码,则将其添加到global.asax中,如下所示:

You can add the host in config. If you choose code for that too, add it in global.asax as below

ExtendedHostFactory factory = new ExtendedHostFactory();
RouteTable.Routes.Add(new ServiceRoute(@"myservice/path", factory, typeof(MyService)));

这篇关于在代码中向端点行为添加行为扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆