WCF:如何将自定义 ServiceBehavior 添加到 WCF 配置中

发布于 2024-11-10 17:30:27 字数 1238 浏览 3 评论 0原文

您好,我有自己的 ServiceBehavior:

public class StructureMapServiceBehavior : IServiceBehavior
    {
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher cd = cdb as ChannelDispatcher;
                if (cd != null)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        ed.DispatchRuntime.InstanceProvider =
                            new StructureMapInstanceProvider(serviceDescription.ServiceType);
                    }
                }
            }
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }

    }

如何使用 WCF 配置工具将其添加到 App.config 中?

: WCF 配置工具

Hi I have my own ServiceBehavior:

public class StructureMapServiceBehavior : IServiceBehavior
    {
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher cd = cdb as ChannelDispatcher;
                if (cd != null)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        ed.DispatchRuntime.InstanceProvider =
                            new StructureMapInstanceProvider(serviceDescription.ServiceType);
                    }
                }
            }
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }

    }

How i can add it in App.config with WCF configuration tool ?

:
WCF configuration tool

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2024-11-17 17:30:28

创建一个继承自 BehaviorExtensionElement 的类:

public class StructureMapServiceBehaviorElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(StructureMapServiceBehavior ); }
    }

    protected override object CreateBehavior()
    {
        return new StructureMapServiceBehavior ();
    }
}

然后在配置文件中注册您的扩展:

<behaviorExtensions>
    <add name="timeService" type="YourAssembly.StructureMapServiceBehaviorElement ,
YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>

完成后,您可以像使用其他扩展一样使用您的扩展。

编辑:使用配置工具来完成它,它是类似的。创建上述类后,在 WCF 配置工具的扩展部分中注册您的行为(高级 -> 扩展 -> 行为元素扩展)

Create a class that inherit from BehaviorExtensionElement:

public class StructureMapServiceBehaviorElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(StructureMapServiceBehavior ); }
    }

    protected override object CreateBehavior()
    {
        return new StructureMapServiceBehavior ();
    }
}

Then register your extension in the config file:

<behaviorExtensions>
    <add name="timeService" type="YourAssembly.StructureMapServiceBehaviorElement ,
YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>

When that is done you can use your extension like any other.

EDIT: To do it using the configuration tool, it is similar. Once the class above is created, register your behavior in the extensions section of the WCF configuration tool (advanced->extensions->behavior element extensions)

白衬杉格子梦 2024-11-17 17:30:28

您必须创建从 BehaviorExtensionElement 派生的自定义类,它将负责创建您的自定义行为。 这是示例 以及在配置文件中添加此类行为所需的步骤(必须首先在 behaviorsExtensions 中注册扩展 部分)。

在配置工具中,我想您首先必须在“高级”>“中”注册扩展。扩展,之后您可能将能够使用该服务行为。

You have to create custom class derived from BehaviorExtensionElement which will be responsible for creating your custom behavior. Here is the example with steps needed to add such behavior in configuration file (extension must be registered first in behaviorsExtensions section).

In configuration tool I guess you will first have to register the extension in Advanced > Extensions and after that you will probably will be able to use that service behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文