这个问题有一个不需要任何技巧的解决方案。它可能看起来需要做很多工作,但实际上并非如此,如果你通读它,它就会很有意义。问题的核心是确实有一个unresolved bug(从 .NET 4 开始),这意味着 WebServiceHost 不使用自定义 QueryStringConverters。因此,您需要做一些额外的工作并了解 WebHttpEndpoints 的 WCF 配置是如何工作的。下面为您列出解决方案。
首先,一个自定义的 QueryStringConverter 允许通过省略或提供空白字符串在查询字符串中提供空值:
public class NullableQueryStringConverter : QueryStringConverter
{
public override bool CanConvert(Type type)
{
var underlyingType = Nullable.GetUnderlyingType(type);
return (underlyingType != null && base.CanConvert(underlyingType)) || base.CanConvert(type);
}
public override object ConvertStringToValue(string parameter, Type parameterType)
{
var underlyingType = Nullable.GetUnderlyingType(parameterType);
// Handle nullable types
if (underlyingType != null)
{
// Define a null value as being an empty or missing (null) string passed as the query parameter value
return String.IsNullOrEmpty(parameter) ? null : base.ConvertStringToValue(parameter, underlyingType);
}
return base.ConvertStringToValue(parameter, parameterType);
}
}
现在是一个自定义 WebHttpBehavior,它将设置自定义 QueryStringConverter 以代替标准使用。请注意,此行为源自 WebHttpBehavior,这很重要,因此我们继承了 REST 端点所需的行为:
public class NullableWebHttpBehavior : WebHttpBehavior
{
protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new NullableQueryStringConverter();
}
}
现在是一个自定义 ServiceHost,它将自定义行为添加到 WebHttpEndpoint,以便它使用自定义 QueryStringConverter。在这段代码中需要注意的重要一点是,它派生自 ServiceHost 而不是 WebServiceHost。这很重要,否则上面提到的错误将阻止自定义 QueryStringConverter 被使用:
public sealed class NullableWebServiceHost : ServiceHost
{
public NullableWebServiceHost()
{
}
public NullableWebServiceHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses)
{
}
public NullableWebServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)
{
}
protected override void OnOpening()
{
if (this.Description != null)
{
foreach (var endpoint in this.Description.Endpoints)
{
if (endpoint.Binding != null)
{
var webHttpBinding = endpoint.Binding as WebHttpBinding;
if (webHttpBinding != null)
{
endpoint.Behaviors.Add(new NullableWebHttpBehavior());
}
}
}
}
base.OnOpening();
}
}
因为我们不是从 WebServiceHost 派生的,所以我们需要完成它的工作并确保我们的配置正确,以确保 REST 服务能够正常工作。您只需要以下内容。在此配置中,我还设置了 WS HTTP 端点,因为我需要从 C#(使用 WS HTTP 作为其更好的方式)和移动设备(使用 REST)访问此服务。如果不需要,可以省略此端点的配置。需要注意的一件重要事情是,您不再需要自定义端点行为。这是因为我们现在添加了绑定自定义 QueryStringConverter 的自定义端点行为。它派生自配置添加的 WebHttpBehavior,使其现在变得多余。
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyNamespace.Service1">
<endpoint binding="webHttpBinding" bindingConfiguration="WebHttpBinding" contract="MyNamespace.IService1" />
<endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="WsHttpBinding" contract="MyNamespace.IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
<wsHttpBinding>
<binding name="WsHttpBinding">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="false" httpsHelpPageEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
最后要做的是创建一个自定义ServiceHostFactory并告诉svc文件使用它,这将导致所有自定义代码都被使用。当然,您也可以创建一个自定义元素,允许您在配置中添加行为,但我认为对于这种行为,基于代码的方法更好,因为您不太可能想要删除处理可空类型的能力,因为它会破坏你的服务:
public sealed class NullableWebServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new NullableWebServiceHost(serviceType, baseAddresses);
}
}
将 Service.svc 文件的标记更改为以下内容:
<%@ ServiceHost Service="MyNamespace..Service1" CodeBehind="Service1.svc.cs" Factory="MyNamespace.NullableWebServiceHostFactory" %>
现在您可以在服务接口中使用可空类型而不会出现任何问题,只需省略参数或将其设置为空字符串即可。以下资源可能对您有更多帮助:
希望这会有所帮助!