我有一个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中使用此扩展类。
它似乎是一个端点行为,但不知道如何使用它。
感谢任何帮助
答案 0 :(得分:2)
要添加自定义行为,您需要将派生行为添加为配置文件中的行为扩展,并需要添加新的行为扩展类型。请参阅此帖子 - Custom Behavior won't register in my 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>