简体   繁体   English

在文件 c# 中写入 stream 时出现错误信息

[英]Error message when writing stream in file c#

I created a function which write an XML file on a specific folder.我创建了一个 function,它在特定文件夹中写入了一个 XML 文件。

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

The writeFile method is the following: 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;
}

I close and I have 2 issues concerning this code:我关闭了,关于这段代码我有 2 个问题:

  1. The file is written in the right folder but it generates an error message该文件写入正确的文件夹中,但会生成一条错误消息
  2. This code is in a dll but the path of the error message is related to the path of the project not of the dll此代码在 dll 但错误消息的路径与项目的路径相关,而不是 dll

Here is my error message:这是我的错误信息:

[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()

I just commented out a single line and it solved my error message.我只是注释掉一行,它解决了我的错误信息。

// fs.Flush();

I still wonder why my stack message is giving me the path of my project, not the path of the dll. For example:我仍然想知道为什么我的堆栈消息给我的是项目的路径,而不是 dll 的路径。例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM