我在C#/。Net 3.5 ...中遇到了非常奇怪的行为。

我正在编写一个课程,该课程将内容管理系统的上传管道挂钩。 CMS通过反射执行此钩子。

由于某些未知原因,以下代码 失败 通过投掷nullref(“文件”是httpfilecollection)。

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

没有正确的解决方案

其他提示

只是一个猜测,但是CMS可能存在一些意外的行为(可能是一个错误)。是否有错误,您的 合同 使用CMS未完全定义。

上载文件匹配的原因是因为您的方法可能被多次调用,而最终调用会引起异常。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top