gpt4 book ai didi

c# - QueryStringConverter 因未知原因不工作

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:11 26 4
gpt4 key购买 nike

我正在编写一个小库,它正在为 WCF 休息服务生成客户端。但是我遇到了可空值的问题:由于未知原因,我的自定义 QueryStringConverter 从未被调用,尽管我在调试器中看到 WCF 调用了它的 CanConvert 方法。这是代码:

public class NullableQueryStringConverter : QueryStringConverter
{
public static NullableQueryStringConverter Instance { get; } = new NullableQueryStringConverter();
public override bool CanConvert(Type type)
{
if (base.CanConvert(type))
return true;
var underlyingType = Nullable.GetUnderlyingType(type);
var canConvert = underlyingType != null && base.CanConvert(underlyingType);
return canConvert;
}

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);
}
}

这是 WCF 服务打开:

public void Start(Uri baseAddress)
{
var serviceInterface = _serviceType.GetInterfaces()
.First(i => i.GetCustomAttribute<ServiceContractAttribute>(true) != null);
var attribute = _serviceType.GetCustomAttribute<ServiceBehaviorAttribute>(true);
if (attribute?.InstanceContextMode == InstanceContextMode.Single)
{
var singleton = Activator.CreateInstance(_serviceType);
ServiceHost = new WebServiceHost(singleton, baseAddress.Concat(_subAddress));
}
else
{
ServiceHost = new WebServiceHost(_serviceType, baseAddress.Concat(_subAddress));
}
ServiceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
var debugBehavior = ServiceHost.Description.Behaviors.OfType<ServiceDebugBehavior>().FirstOrDefault();
if (debugBehavior != null)
{
debugBehavior.IncludeExceptionDetailInFaults = true;
}
else
{
ServiceHost.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
}

var webHttpBinding = new WebHttpBinding
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
MaxBufferPoolSize = int.MaxValue,
ReaderQuotas =
new XmlDictionaryReaderQuotas
{
MaxArrayLength = int.MaxValue,
MaxStringContentLength = int.MaxValue,
MaxDepth = 32
}
};

webHttpBinding.ContentTypeMapper = new NewtonsoftJsonContentTypeMapper();
ServiceHost.AddServiceEndpoint(serviceInterface, webHttpBinding, string.Empty);

foreach (var endpoint in ServiceHost.Description.Endpoints)
{
endpoint.Behaviors.Add(new NullableWebHttpBehavior
{
HelpEnabled = false,
DefaultBodyStyle = WebMessageBodyStyle.Bare,
DefaultOutgoingRequestFormat = WebMessageFormat.Json,
DefaultOutgoingResponseFormat = WebMessageFormat.Json,
FaultExceptionEnabled = true
});

endpoint.Behaviors.Add(new NewtonsoftJsonBehavior());
}

ServiceHost.Open();
}

如您所见,我正在添加正在调用的 NullableWebHttpBehavior(至少是 CanConvert 方法),但此后 WCF 仍要求参数可由基类转换.

这是 github 上项目的链接:link .这很奇怪,因为我在另一个工作正常的项目中有非常相似的代码。

也许我遗漏了什么,我不知道。包含所有要检查的逻辑的文件是:NullableWebHttpBehavior.csServiceManager.cs。您可以轻松地运行一个测试(这是这里唯一的一个)并面对同样的问题。

最佳答案

经过一些研究,我发现这个方法会导致异常:

    protected override IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
if (operationDescription.IsGetOrDeleteOperation())
{
// no change for GET operations
return base.GetRequestDispatchFormatter(operationDescription, endpoint);
}

if (operationDescription.Messages[0].Body.Parts.Count == 0)
{
// nothing in the body, still use the default
return base.GetRequestDispatchFormatter(operationDescription, endpoint);
}

return new NewtonsoftJsonDispatchFormatter(operationDescription, true);
}

出于某种原因,base.GetRequestDispatchFormatter 正在返回原始的 QueryStringConverter,这会引发错误。所以我只是删除了这些 if 并始终使用我的自定义 NewtonsoftJsonDispatchFormatter,它有一些开销但工作正常。

关于c# - QueryStringConverter 因未知原因不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41336356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com