English 中文(简体)
任务——如何处理例外
原标题:Task.Status.Faulted - How to Handle Exceptions

我在伙伴关系中承担以下任务。 NET 欲免以文件形式提出新请求的网播。 似乎在我的当地,但在主机上,档案没有保存,我的记录显示现状。

我的理解是,这必须涉及未经处理的例外情况。 我怎么能够发现这些例外是什么,以便我能够解决这些问题?

    // Save file
    MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Files"));
    Task<IEnumerable<HttpContent>> task = Request.Content.ReadAsMultipartAsync(provider);

    return task.ContinueWith<string>(contents =>
    {
        string filename = provider.BodyPartFileNames.First().Value;

        FileInfo file = new FileInfo(filename);

        Log.LogMessage("Status: " + task.Status.ToString() + " File Exists: " + file.Exists);

        // Delete excel file when complete.
        File.Delete(filename);

        // Return message to user.
        return "Complete";

    }, TaskScheduler.FromCurrentSynchronizationContext());
最佳回答

You need to have another continuation to look at the exception:

Task task1 = () => ....;
task1.ContinueWith((t) => if(t.IsFaulted) Trace.WriteLine(t.Exception.ToString()));
return task1;

因此,在继续确定初步任务时,trick计要继续。

问题回答

暂无回答




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签