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

标签 wcf app-config

嗨,我有自己的 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 configuration tool

最佳答案

创建一个继承自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 配置工具的扩展部分中注册您的行为(高级->扩展->行为元素扩展)

关于WCF:如何将自定义 ServiceBehavior 添加到 WCF 配置中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6201249/

相关文章:

c# - 使用 MEF 时,为我的插件使用 app.config 的正确方法是什么?

c# - 检测 WCF 双工契约(Contract)中的客户端死亡

wcf - maxItemsInObjectGraph 被忽略

wcf - 生成的元数据 (WSDL) 中的 .net 4 WCF 端点指向节点而不是托管在负载平衡 (NLBS) IIS6 上的虚拟主机

wcf - 多个 SVC 引用每个都暴露相同的实体

c# - 'System.ServiceModel.Diagnostics.TraceUtility' 的类型初始值设定项抛出异常

.net - ConfigurationManager 不会将设置保存到 exe.config

c# - WCF 服务返回 400 错误 : The body of the message cannot be read because it is empty

c# - AppSettings 清除 app.config 中的 Xml 元素

entity-framework - 在 F# 中,什么时候是 "constant string expression"而不是 "constant string expression"