gpt4 book ai didi

.NET SOAP 扩展在 MethodInfo 中抛出 NullReferenceException?

转载 作者:行者123 更新时间:2023-12-04 07:11:45 24 4
gpt4 key购买 nike

注意:使用 .NET 2.0 和 VS2005 作为 IDE

大家好,

我正在将 web 服务调用记录到我们的数据库中,最后使用从另一个项目移植过来的非常精简的实现来配置和运行 SoapExtension。我已经在配置文件中设置了它,因此它将适用于所有方法。当我调用我的 web 服务,soap 扩展触发时,当 SoapServerMessage 尝试调用其 MethodInfo 属性时会抛出 NullPointerException:

System.Web.Services.Protocols.SoapException: There was an exception running the extensions specified in the config file. 
---> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Services.Protocols.SoapServerProtocol.get_MethodInfo()
at System.Web.Services.Protocols.SoapServerMessage.get_MethodInfo()
at MyService.SOAPLoggingExtension.LogInput(SoapMessage message)
at MyService.SOAPLoggingExtension.ProcessMessage(SoapMessage message)
at System.Web.Services.Protocols.SoapMessage.RunExtensions(SoapExtension[] extensions, Boolean throwOnException)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

在 ProcessMessage(SoapMessage) 的 BeforeDeserialize 阶段调用 LogInput 方法:
SoapMessageStage.BeforeDeserialize:
CopyStream(_oldStream, _newStream);
_newStream.Position = 0;

if(_enabled)
LogInput(message);
break;

当尝试访问它尝试记录的消息对象的 MethodInfo 属性时,LogInput 方法失败。这是调用该属性的代码块:
entry = new MyServerLogEntry();
entry.ServerURL = message.Url;
entry.Method = (message.MethodInfo == null) ? null : message.MethodInfo.Name;

当 message.MethodInfo 被调用时,它会冒泡到 SoapServerProtocol.get_MethodInfo(),并且在那里抛出空引用异常。我用谷歌搜索,并在 Stack Overflow 上进行了检查,但无法找出 MethodInfo 属性会引发异常的原因。

有谁知道如何确保在 Web 服务调用期间正确初始化此 MethodInfo 属性?

更多详情:如果我不尝试访问 MethodInfo 属性,则扩展程序可以正常工作并记录到我的数据库。

最佳答案

经过一些试验和错误,我已经能够解决这个问题。虽然我不完全理解为什么,SoapMessage 对象在 BeforeDeserialize 阶段没有完全初始化。在此阶段,Action 和 MethodInfo 属性都会引发错误。

但是,在 AfterSerialize 阶段,这些对象似乎已正确初始化。通过将读取消息名称的行移到后面的阶段,可以正确填充日志条目对象而不会引发异常。

看来正确的顺序是:

  • 反序列化之前

    一种。读取服务器 URL

    湾。检索请求信息(证书、用户主机地址等)

    C。从流
  • 中读取请求内容
  • 序列化后

    一种。读取异常

    湾。读取 MethodInfo 信息(如有必要,还可以读取 Action 信息)

    C。从流
  • 中读取响应内容

    关于.NET SOAP 扩展在 MethodInfo 中抛出 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/226587/

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