简体   繁体   中英

Is there a way to pass parameters to a custom Service Behavior through configuration

I have multiple WCF services hosted in IIS to which I'm applying the same custom service behavior. I'm looking for a way to pass several parameters to the behavior through configuration, such as in behaviorExtensions or behavior elements.

If it helps, I'm also adding custom message inspector in ApplyDispatchBehavior, so I will need to pass parameters to the inspector:

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
    foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
    {
        foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
                    new ValidatingMessageInspector(<custom parameters>));
        }
    }
}

Would just creating a configuration section in web.config with the parameters be valid? If so you can just read the config there and apply it, or even just use appSettings if the parameters are basic.

For example, if you have few parameters of basic types, maybe a simple approach could be:

<appSettings>
    <add key="Shared.CommonParameter" value="A value" />
    <add key="Service1.OneParameter" value="False" />
    <add key="Service1.AnotherParameter" value="Some Value" />
    <add key="Service2.ADifferentParameter" value="42" />
</appSettings>

That way it would be easy to differentiate what setting belongs to which service (notice the service name prefix in the key), and also have some shared parameters if needed.

If you need something more complex in structure you might want to look into defining custom configuration sections for each service, as is shown here: http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.140%29.aspx

But that might be overkill, depending on your needs and expected flexibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM