【问题标题】:Error message when writing stream in file c#在文件 c# 中写入流时出现错误消息
【发布时间】:2021-08-24 08:15:37
【问题描述】:

我创建了一个在特定文件夹中写入 XML 文件的函数。

if (!Utils.WriteFile((Constants.PRED_STORAGE_PATH + predictionResponse.EmailId + ".xml"), prediction))
    throw new Exception("File already exists");

writeFile方法如下:

public static bool WriteFile(string path, Prediction prediction)
{
    if (File.Exists(path))
        return false;

    if (prediction == null)
        throw new Exception("No prediction was returned");

    XmlSerializer serializer = new XmlSerializer(typeof(Prediction));
    // Create an XmlTextWriter using a FileStream.
    Stream fs = new FileStream(path, FileMode.Create);
    XmlWriter writer = 
         XmlWriter.Create(fs, new XmlWriterSettings { Indent = true, Encoding = Encoding.Unicode });
    // Serialize using the XmlTextWriter.
    serializer.Serialize(writer, prediction);
    writer.Flush();
    // writer = null;
    writer.Close();
        
    fs.Close();
    fs.Dispose();
    // fs = null;
    fs.Flush();
        
    return true;
}

我关闭并 我有两个关于此代码的问题:

  1. 文件写入正确的文件夹,但会生成错误消息
  2. 此代码在 dll 中,但错误消息的路径与项目的路径有关,而不是 dll 的路径

这是我的错误信息:

[2021-08-24 07:53:15+00:00] [Error] An unhandled exception has occurred while executing the request.    at System.IO.FileStream.Flush(Boolean flushToDisk)
   at System.IO.FileStream.Flush()
   at myproject.utils.Utils.WriteFile(String path, Prediction prediction) in C:\Users\me\Documents\projects\myproject\utils\Utils.cs:line 71
   at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse) in CC:\Users\me\Documents\projects\myproject\Controllers\WebhookController.cs:line 99
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
[2021-08-24 07:53:15+00:00] [Error] Connection ID "16861477006485751766", Request ID "800007d7-0000-ea00-b63f-84710c7967bb": An unhandled exception was thrown by the application.    at System.IO.FileStream.Flush(Boolean flushToDisk)
   at System.IO.FileStream.Flush()
   at myproject.utils.Utils.WriteFile(String path, Prediction prediction) in C:\Users\me\Documents\projects\myproject\utils\Utils.cs:line 71
   at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse) in C:\Users\me\Documents\projects\myproject\Controllers\WebhookController.cs:line 99
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

【问题讨论】:

  • fs.Flush() 需要在fs.Close()fs.Dipose() 之前调用
  • 谢谢!我现在试图理解为什么错误消息返回项目中我的类的路径 (c:\user\me\...\myclass") ...而不是类和行。

标签: c# stream


【解决方案1】:

我只是注释掉了一行,它解决了我的错误消息。

// fs.Flush();

我仍然想知道为什么我的堆栈消息给了我项目的路径,而不是 dll 的路径。例如:

C:\Users\me\Documents\projects\myproject\utils\Utils.cs:line 71
   at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    相关资源
    最近更新 更多