1

我有一个 WCF 服务

我的 Web.config 如下所示:

<system.serviceModel>

<bindings>
  <webHttpBinding>
    <binding name="crossDomain" crossDomainScriptAccessEnabled="true"  />
  </webHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="EndpBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="ServiceBehavior"  name="MyNameSpace.MyService">
    <endpoint address="" binding="webHttpBinding"  bindingConfiguration="crossDomain" contract="MyNameSpace.IMyService"  behaviorConfiguration="EndpBehavior"/>
  </service>
</services>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

我最近从一个网站上找到了这个数组参数的代码

using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;

namespace ArraysInQueryStrings
{
public class ArrayInQueryStringWebHttpBehavior : WebHttpBehavior
{
    WebMessageFormat defaultOutgoingResponseFormat;
    public ArrayInQueryStringWebHttpBehavior()
    {
        this.defaultOutgoingResponseFormat = WebMessageFormat.Json;
    }

    public override WebMessageFormat DefaultOutgoingResponseFormat
    {
        get
        {
            return this.defaultOutgoingResponseFormat;
        }
        set
        {
            this.defaultOutgoingResponseFormat = value;
        }
    }

    protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
    {
        return new ArrayQueryStringConverter();
    }
}
}

如何在 web.config 中使用这个扩展类。

这似乎是一个端点行为,但不知道如何使用它。

任何帮助表示赞赏

4

1 回答 1

2

要添加自定义行为,您需要将派生行为作为行为扩展添加到配置文件中,并且需要添加新的行为扩展类型。请参阅这篇文章 -自定义行为不会在我的 web.config 中注册

 public class ArrayInQueryStringBehaviorExtension : BehaviorExtensionElement
{
 public override Type BehaviorType
 {
    get { return typeof(ArrayInQueryStringWebHttpBehavior); 
 }
}

protected override object CreateBehavior()
{
    return new ArrayInQueryStringWebHttpBehavior();
}
}

配置文件(您需要在我在下面标出方括号的地方指定您的程序集名称)

<extensions>
  <behaviorExtensions>
    <add name=" ArrayInQueryStringWebHttpBehavior " type="[Namespace]. ArrayInQueryStringBehaviorExtension, [Assembly Name], [Assembly Version], [Assembly Culture], PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>
<behaviors>
  <endpointBehaviors>
    <behavior name="arrayInQueryBehavior">
      <webHttp/>
      < ArrayInQueryStringWebHttpBehavior />
    </behavior>
  </endpointBehaviors>
<behaviors>
于 2013-07-19T11:40:06.100 回答