2010-09-15 37 views
2

我在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 
+0

你能提供异常的完整堆栈跟踪吗? – qbeuek 2010-09-15 17:58:28

+0

您的NullRef检查之前语句几乎没有让循环成功,对吧?它使你从方法返回... – 2010-09-15 18:02:26

+0

所以在检查之后,它总是不为NULL,它永远不会返回,即这种额外的检查使它始终具有价值? – Aliostad 2010-09-15 18:07:50

回答

0

只是一个猜测,但有可能是在CMS一些意想不到的行为(可能是一个bug)。错误与否,您的合同与CMS没有完全定义。

上传文件匹配的原因是因为您的方法可能会在最后一次调用引发异常时调用多次。

鉴于您正在使用您无法控制的应用程序,因此您的解决方案是正确的 - 您必须在使用之前检查您获得的所有内容。

+0

我对此持怀疑态度,因为args只是HttpFileCollection的包装,它是一个包装通用的.NET构造。我不完全相信,这是由于CMS – 2010-09-15 18:31:10

+0

@Greg R:你应该在每次调用方法时都进行一些登录。我敢打赌它不止一次(最后一个需要空检查)。 – 2010-09-15 19:32:41

+0

我有一个断点集,我附加了一个调试器,它只被调用一次 – 2010-09-15 21:23:22