2

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

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
David Horák
  • 5,535
  • 10
  • 53
  • 77

2 Answers2

3

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)

Johann Blais
  • 9,389
  • 6
  • 45
  • 65
  • @johann-blais can be StructureMapServiceBehaviorElement in same project like me wcf libary ? WCF configuration tool, show error: Can locate assembly – David Horák Jun 01 '11 at 13:08
  • Yes it is possible. Where does this message come from? The configuration tool? Runtime exception? – Johann Blais Jun 01 '11 at 13:10
  • @johann-blais exactly in WCF configuration tool. Build fine, and if I open WCF CT, then a got error: (MyAssembly) could not be found. Do you want relocate it ? – David Horák Jun 01 '11 at 13:16
  • It is probably because the tool running directory is not bin/debug, it cannot find the assembly. Try adding the extension through the tool instead of manually, then it should be able to find it afterwards. – Johann Blais Jun 01 '11 at 13:22
  • @johann-blais on behavior in app.config i have this error: Warning The element 'behavior' has invalid child element 'StructureMapBehavior'. List of possible elements expected: 'serviceAuthenticationManager, clear, serviceAuthorization, serviceCredentials, serviceMetadata, serviceSecurityAudit, serviceThrottling, dataContractSerializer, serviceDebug, serviceTimeouts, remove, useRequestHeadersForMetadataAddress, persistenceProvider, – David Horák Jun 01 '11 at 13:26
  • @johann-blais workflowRuntime, etwTracking, bufferedReceive, workflowIdle, workflowUnhandledException, sendMessageChannelCache, sqlWorkflowInstanceStore, workflowInstanceManagement, serviceDiscovery, routing'. – David Horák Jun 01 '11 at 13:26
1

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.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670