如何将自定义服务行为添加到WCF配置

时间:2016-03-07 13:24:47

标签: wcf

我在Uri模板参数中有一个ComplexType服务,我已经将CanConvert()和ConvertStringToValue()方法覆盖到MyQueryStringConverter类。

我需要将该行为添加到web.config文件中。

这是行为

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

这是配置文件:

<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>

请帮助您添加该行为。

1 个答案:

答案 0 :(得分:1)

首先,您必须使用配置文件中的behaviorExtensions元素注册您的行为:

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

其中:

  • “FullNameSpace.MyWebHttpBehavior”是您的具有完整命名空间的类,
  • “MyWebHttpBehavior.AssemblyName”是具有扩展名的程序集名称 您的类编译的位置。
  • “1.0.0.0”是程序集的版本
  • “中立”是你的文化。如果需要任何特殊文化,请更改

Secound,声明你的行为:

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

接下来,添加到绑定:

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

        </bindings>

最后,将行为设置为服务端点:

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

我已经复制了我开发的服务的这个配置,并更改了名称以适合您的类/配置,但检查我是否写错了。

我已将此链接用作设置配置的基础:Custom Behavior won't register in my web.config

希望它有所帮助。