.NET SOAP 扩展在 MethodInfo 中抛出 NullReferenceException?
注意: 使用 .NET 2.0 和 VS2005 作为 IDE
大家好,
我正在记录对数据库的 Web 服务调用,最后使用一个非常精简的实现配置并运行了 SoapExtension,该实现是从另一个项目移植过来的。 我已在配置文件中对其进行了设置,因此它将针对所有方法运行。 当我调用 Web 服务并且肥皂扩展触发时,当 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;
并且 LogInput 方法在尝试执行以下操作时失败:访问它尝试记录的消息对象的 MethodInfo 属性。 下面是调用该属性的代码块:
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 属性,则扩展将正常工作并记录到我的数据库。
NOTE: Using .NET 2.0, and VS2005 as IDE
Hello all,
I'm working on logging webservice calls to our database, and finally got the SoapExtension configured and running using a very stripped-down implementation that was ported over from another project. I've set it up in the configuration file so it will run for all methods. When I call my webservice, and the soap extension fires, a NullPointerException is thrown when the SoapServerMessage attempts to call its MethodInfo property:
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)
The LogInput method is called during the BeforeDeserialize stage of ProcessMessage(SoapMessage):
SoapMessageStage.BeforeDeserialize:
CopyStream(_oldStream, _newStream);
_newStream.Position = 0;
if(_enabled)
LogInput(message);
break;
And the LogInput method is failing when attempting to access the MethodInfo property of the message object it is trying to log. Here is the block of code where the property is called:
entry = new MyServerLogEntry();
entry.ServerURL = message.Url;
entry.Method = (message.MethodInfo == null) ? null : message.MethodInfo.Name;
When message.MethodInfo is called, it bubbles over to SoapServerProtocol.get_MethodInfo(), and the null reference exception gets thrown in there. I've googled, and checked around here on Stack Overflow, but haven't been able to find out why the MethodInfo property would be throwing an exception.
Does anyone know how to ensure this MethodInfo property is properly initialized during the web service call?
ADDITIONAL DETAILS: If I do not attempt to access the MethodInfo property, the extension works correctly and logs to my database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一番尝试和错误,我已经能够解决这个问题。 虽然我不完全理解为什么,SoapMessage 对象在 BeforeDeserialize 阶段没有完全初始化。 Action 和 MethodInfo 属性都会在此阶段引发错误。
然而,在 AfterSerialize 阶段,这些对象似乎已正确初始化。 通过将读取消息名称的行移到后面的阶段,可以正确填充日志条目对象而不会引发异常。
看来正确的顺序是:
BeforeDeserialize
a. 读取服务器 URL
b. 检索请求信息(证书、用户主机地址等)
c. 流中读取请求内容
AfterSerialize
a. 阅读异常
b. 读取 MethodInfo 信息(如果需要,还可以读取 Action 信息)
c. 从流中读取响应内容
After some trial and error, I have been able to solve this issue. While I do not entirely understand why, the SoapMessage object is not completely initialized at the BeforeDeserialize stage. Both the Action and MethodInfo properties throw errors at this stage.
However, during the AfterSerialize stage, these objects seem to be properly initialized. By moving the line which reads the message name to a later stage, the log entry object can be properly filled without throwing an exception.
It appears the correct order is:
BeforeDeserialize
a. Read Server URL
b. Retrieve request information (certificates, user host address, etc.)
c. Read request contents from stream
AfterSerialize
a. Read Exceptions
b. Read MethodInfo information (and Action information if necessary)
c. Read response contents from stream
根据 MSDN,方法信息仅在 AfterDeserialization 和 BeforeSerialization 期间可用。 所以这就是问题的一部分。
According to MSDN the method information is only available during AfterDeserialization and BeforeSerialization. So that would be part of the problem.