开发者

WCF: How add me custom ServiceBehavior into WCF configuration

开发者 https://www.devze.com 2023-03-10 03:44 出处:网络
Hi I have my own ServiceBehavior: public class StructureMapServiceBehavior : IServiceBehavior { public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBas

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 service开发者_JAVA技巧Description, ServiceHostBase serviceHostBase)
        {
        }

    }

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

:

WCF: How add me custom ServiceBehavior into WCF configuration


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消