3

I have a service with ComplexType in the Uri Template Parameter, I have Override the CanConvert() and ConvertStringToValue() methods to the class MyQueryStringConverter.

I need to add that behavior to the web.config file.

Here is the Behavior

public class MyWebHttpBehavior : WebHttpBehavior
    {
        protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
        {
            return new MyQueryStringConverter();
        }
    }

Here Is the Configuration File :

<system.serviceModel>
        <bindings>
            <webHttpBinding>
                <binding name="webHttp"
                 maxReceivedMessageSize="20000000" >
                    <security mode="None">
                        <transport clientCredentialType = "None"/>
                    </security>
                </binding>
            </webHttpBinding>

        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehaviour">
                    <serviceMetadata httpGetEnabled="true"/>
                    <useRequestHeadersForMetadataAddress>
                        <defaultPorts>
                            <add scheme="http" port="80" />
                        </defaultPorts>
                    </useRequestHeadersForMetadataAddress>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="web">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service name="Service.Service1" behaviorConfiguration="mexBehaviour">
                <endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="web"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">

                </endpoint>

            </service>
        </services>
    </system.serviceModel>

Please help how to add that behavior.

KousiK
  • 825
  • 7
  • 17
  • 39

1 Answers1

2

First, you must register your behavior, using behaviorExtensions element in your configuration file:

  <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="MyWebHttpBehavior"
             type="FullNameSpace.MyWebHttpBehavior, MyWebHttpBehavior.AssemblyName, Version=1.0.0.0, Culture=neutral" />
      </behaviorExtensions>
    </extensions>

Where:

  • "FullNameSpace.MyWebHttpBehavior" is your class with full namespace,
  • "MyWebHttpBehavior.AssemblyName" is your assembly name with extension where your class is compiled.
  • "1.0.0.0" is the version of your assembly
  • "neutral" is your Culture. Change if any special Culture is required

Secound, declare your behavior:

<behaviors>
  <endpointBehaviors>
    <behavior name="customMyWebHttpBehavior">
      <webHttp/>
      <MyWebHttpBehavior/>
    </behavior>
  </endpointBehaviors>
<behaviors>

Next, add to the binding:

<bindings>
            <webHttpBinding>
                <binding name="webHttp"
                 maxReceivedMessageSize="20000000" >
                    <security mode="None">
                        <transport clientCredentialType = "None"/>
                    </security>
                </binding>
                <MyWebHttpBehavior />
            </webHttpBinding>

        </bindings>

And finally, set the behavior to your service endpoint:

<endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="customMyWebHttpBehavior"/>

I've copied this configuration of a service I've developed and changed the names to fit your class/configuration, but check if I did not wrote anything wrong.

I've used this link as base to setup my configuration: Custom Behavior won't register in my web.config

Hope it helps.

Community
  • 1
  • 1
Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
  • Getting a error The type 'Service.MyWebHttpBehavior, Service, Version=1.0.0.0, Culture=neutral' registered for extension 'MyWebHttpBehavior' could not be loaded. @Ricadro – KousiK Mar 08 '16 at 05:09
  • You must ensure that namespace and assembly name is correct and also that assembly is in the same folder of your service binaries (bin folder for instance). – Ricardo Pontual Mar 08 '16 at 13:10
  • Yes all are correct. The Namespace and Assembly name are same. @Ricardo Pontual – KousiK Mar 08 '16 at 13:32
  • 1
    Just check out if your assembly is in the same folder of your service binaries, probably the bin folder. – Ricardo Pontual Mar 08 '16 at 13:37
  • The class MyWebHttpBehavior isn't extending of BehaviorExtensionElement. This occurs an error – Oswald Feb 20 '18 at 08:40
  • https://stackoverflow.com/questions/15636874/how-to-add-a-custom-endpointbehavior-to-the-web-config-of-the-service. Looks like there's a need for the BehaviorExtensionElement – Oswald Feb 20 '18 at 09:41