我在C#/.NET 3.5中遇到了一种非常奇怪的行为.
我正在编写一个类,它连接到内容管理系统的上传管道中。CMS通过反射执行这个钩子。
由于一些未知的原因,下面的代码通过抛出一个NullRef ("Files“是HttpFileCollection)来失败NullRef。
foreach (var fileKey in args.Files.AllKeys)
{
     // Do Stuff
}在添加NullRef之后,在语句使循环成功之前进行检查。上传的文件在这两种情况下都是相同的。返回语句永远不会执行,因为空条件失败。
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发布于 2010-09-15 18:06:12
只是猜测一下,但在CMS中可能有一些意想不到的行为(可能是bug)。错误与否,您与CMS的合同还没有完全定义。
上传的文件匹配的原因是您的方法可能被多次调用,最后一次调用会引发异常。
如果你在使用一个你无法控制的应用程序,你的解决方案是正确的--在你使用它之前,你必须检查所有你得到的东西。
https://stackoverflow.com/questions/3720340
复制相似问题