枚举反射程序集中的集合需要空引用检查吗?
我在 C#/.NET 3.5 中遇到了一种非常奇怪的行为...
我正在编写一个挂钩到内容管理系统的上传管道的类。 CMS 通过反射执行该钩子。
由于某些未知原因,以下代码失败并抛出 NullRef(“Files”是一个 HttpFileCollection)。
foreach (var fileKey in args.Files.AllKeys)
{
// Do Stuff
}
在语句之前添加 NullRef 检查后,循环将成功。两种情况下上传的文件都是相同的。 return 语句永远不会被执行,因为 null 条件失败。
if (args.Files == null ) return;
foreach (var fileKey in args.Files.AllKeys)
{
// Do Stuff
}
我完全被这个难住了。有什么想法吗?
完整堆栈跟踪
** Exception: System.Web.HttpUnhandledException **
Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
Source: System.Web
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.sitecore_shell_applications_flashupload_advanced_uploadtarget_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
** Nested Exception **
Exception: System.Reflection.TargetInvocationException
Message: Exception has been thrown by the target of an invocation.
Source: mscorlib
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
at Sitecore.Pipelines.Processor.Invoke(PipelineArgs args)
at Sitecore.Nexus.Pipelines.NexusPipelineApi.Resume(PipelineArgs args, Pipeline pipeline)
at Sitecore.Pipelines.Pipeline.Resume()
at Sitecore.Pipelines.Pipeline.DoStart(PipelineArgs args)
at Sitecore.Pipelines.Pipeline.Start(PipelineArgs args, Boolean atomic)
at Sitecore.Pipelines.Pipeline.Start(PipelineArgs args)
at Sitecore.Shell.Applications.FlashUpload.Advanced.UploadTarget.HandleUpload()
at Sitecore.Shell.Applications.FlashUpload.Advanced.UploadTarget.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
** Nested Exception **
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: Sitecore.CustomExtensions
at Sitecore.CustomExtensions.StreamingMediaUploader.Process(UploadArgs args) in C:\...\Sitecore.CustomExtensions\StreamingMediaUploader.cs:line 33
I'm running into a very strange behavior in C#/.NET 3.5...
I am writing a class which hooks into upload pipeline of a content management system. The CMS executes this hook via reflection.
For some unknown reason the following code fails by throws a NullRef ("Files" is an HttpFileCollection).
foreach (var fileKey in args.Files.AllKeys)
{
// Do Stuff
}
After adding a NullRef check before the statement makes the loop succeed. The uploaded files are identical in both cases. The return statement never gets executed, since the null condition fails.
if (args.Files == null ) return;
foreach (var fileKey in args.Files.AllKeys)
{
// Do Stuff
}
I am totally stumped by this. Any ideas?
Full Stack Trace
** Exception: System.Web.HttpUnhandledException **
Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
Source: System.Web
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.sitecore_shell_applications_flashupload_advanced_uploadtarget_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
** Nested Exception **
Exception: System.Reflection.TargetInvocationException
Message: Exception has been thrown by the target of an invocation.
Source: mscorlib
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
at Sitecore.Pipelines.Processor.Invoke(PipelineArgs args)
at Sitecore.Nexus.Pipelines.NexusPipelineApi.Resume(PipelineArgs args, Pipeline pipeline)
at Sitecore.Pipelines.Pipeline.Resume()
at Sitecore.Pipelines.Pipeline.DoStart(PipelineArgs args)
at Sitecore.Pipelines.Pipeline.Start(PipelineArgs args, Boolean atomic)
at Sitecore.Pipelines.Pipeline.Start(PipelineArgs args)
at Sitecore.Shell.Applications.FlashUpload.Advanced.UploadTarget.HandleUpload()
at Sitecore.Shell.Applications.FlashUpload.Advanced.UploadTarget.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
** Nested Exception **
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: Sitecore.CustomExtensions
at Sitecore.CustomExtensions.StreamingMediaUploader.Process(UploadArgs args) in C:\...\Sitecore.CustomExtensions\StreamingMediaUploader.cs:line 33
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是猜测,但 CMS 中可能存在一些意外行为(可能是错误)。无论是否有错误,您与 CMS 的合同尚未完全定义。
上传的文件匹配的原因是因为您的方法可能被多次调用,最后一次调用引发异常。
鉴于您正在使用一个不受您控制的应用程序,您的解决方案是正确的 - 您必须在使用它之前检查所提供的所有内容。
Just a guess, but there is probably some unexpected behavior (possibly a bug) in the CMS. Bug or not, your contract with the CMS is not fully defined.
The reason the uploaded files match is because your method was likely called multiple times with the final call raising the exception.
Given you're working with an app you don't control, your solution is correct -- you must check everything you're given before you use it.