1

I add behavior extension in web.config:

<extensions>
        <behaviorExtensions>
            <add name="WebAppBeaviourElement" type="WebApp.Extensions.CustomBehaviorExtensionElement, WebApp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </behaviorExtensions>
    </extensions>

    <behaviors>
        <endpointBehaviors>
            <behavior name="WebAppBeaviour">
                <WebAppBeaviourElement />
            </behavior>
        </endpointBehaviors>
    </behaviors>

But in Visual Studio 2010 is bug - WebAppBehaviourElement is not visible by visual studio and get error:/

So I think that it is possible to add this extension to endpointBehaviours in code ? And when in code I should do it?

2
  • What error did you get? Why do you think it is a bug? Extensions are only for configuration file. If you want to add behavior in code you will do it directly on endpoint. Commented Sep 17, 2011 at 19:49
  • I dont't remember it , but error said something like WebAppBeaviourElement can not be set as element of behaviour
    – netmajor
    Commented Sep 17, 2011 at 20:27

2 Answers 2

3

You can extend the host factory

public class ExtendedHostFactory : WebServiceHostFactory

Then, add behavior to the host

protected override ServiceHost CreateServiceHost(System.Type serviceType, System.Uri[] baseAddresses)
  {
    ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses) as WebServiceHost;
    host.Description.Behaviors.Add(new ValidateApiKey()); // ValidateApiKey is an IServiceBehavior
  }

You can add the host in config. If you choose code for that too, add it in global.asax as below

ExtendedHostFactory factory = new ExtendedHostFactory();
RouteTable.Routes.Add(new ServiceRoute(@"myservice/path", factory, typeof(MyService)));
1
  • you forgot to add return section into overrided method, but it was obviously) helped me a lot, thanks!
    – krabcore
    Commented Sep 23, 2021 at 14:16
0

Visual Studio 2010 validate configuration file against XSD schemas. One is defined for here system.serviceModel There is no yours custom behavior, but programms work.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.