我有一个 WCF 4 RESTful Web 服务,它必须在 GET(它的 JSONP)上使用。我向该服务添加了一个需要复杂类型参数的方法。
[OperationContract]
[WebGet(RequestFormat=WebMessageFormat.Json)]
void InsertEventActionList(string pageViewGUID,
List<EventActionItem> eventActionItemList);
然后我得到了错误:
Operation 'InsertEventActionList' in contract 'ITrackingService'
has a query variable named 'eventActionItemList' of type
'System.Collections.Generic.List`1[EventActionItem]', but type
'System.Collections.Generic.List`1[EventActionItem]' is not convertible by
'QueryStringConverter'. Variables for UriTemplate query values
must have types that can be converted by 'QueryStringConverter'.
好的,所以使用这篇文章 - http://blogs.msdn.com/b/carlosfigueira/archive/2011/08/09/wcf-extensibility-querystringconverter.aspx我创建了一个附加到行为扩展的自定义 QueryStringConverter:
[DataContract]
public class EventActionItem
{
[DataMember]
public string ActionName { get; set; }
[DataMember]
public string ActionValue { get; set; }
[DataMember]
public string ActionCategory { get; set; }
}
public class EventActionItemQueryStringConverter : QueryStringConverter
{
public override bool CanConvert(Type type)
{
return type == typeof(List<EventActionItem>) || base.CanConvert(type);
}
public override object ConvertStringToValue(string parameter, Type parameterType)
{
if (parameterType == typeof(List<EventActionItem>))
{
List<EventActionItem> item = JsonConvert.DeserializeObject<List<EventActionItem>>(parameter);
if (item != default(List<EventActionItem>))
{
return item;
}
}
return base.ConvertStringToValue(parameter, parameterType);
}
}
public class TrackingWebHttpBehavior : WebHttpBehavior
{
protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new EventActionItemQueryStringConverter();
}
}
public class TrackingBehaviorExtension : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(TrackingWebHttpBehavior); }
}
protected override object CreateBehavior()
{
return new TrackingWebHttpBehavior();
}
}
网络配置:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="queryString"
type="namespace.TrackingBehaviorExtension, namespace"/>
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<queryString />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
但是,我仍然遇到同样的错误。我已经调试了服务初始化并且BehaviorExtensionElement
正在创建,但QueryStringConverter
从未调用过。
是因为我使用的是standardEndpoint
配置而不是客户用户定义的端点吗?如果是这样,我如何将自定义行为附加到standardEndpoint
?