如何解决:值不能为null。参数名称:路径

时间:2019-01-13 22:16:18

标签: c# asp.net-core-webapi

我正在设置IIS上托管的asp.net核心Web-api。 api具有一些用于创建XML文档的逻辑,这些逻辑应存储在服务器C:驱动器正下方的文件夹中。流程如下:

客户端发送一个请求,该请求已处理并存储在数据库中。数据库事务完成后,将生成包含一些数据的XML文档。我将应在appsettings.json文件中生成此文档的路径存储如下:

"ProtocolPath": {
"PathToFolder": "C:\\some_folder"
}

在我的代码中,我有一个存储Path变量并通过构造函数对其进行初始化的类:

public string Path { get; set;}

public ProtocolService(object entry, IConfiguration config)
{
    _config = config;
    Path = GetProtocolPath();
    _entry = entry;
}

private string GetProtocolPath()
{
  try
  {

    if(!Directory.Exists(_config.GetSection("ProtocolPath")
    .GetValue<string>("PathToFolder")))
    {            
       Directory.CreateDirectory(_config.GetSection("ProtocolPath")
      .GetValue<string>("PathToFolder"));
    }

    StringBuilder pathBuilder = new 
    StringBuilder(_config.GetSection("ProtocolPath").GetValue<string> 
    ("PathToFolder"));
    pathBuilder.Append(DateTime.Now.ToString("yyyyMMdd"));
    pathBuilder.Append(".xml");

    return pathBuilder.ToString();

   }
   catch (Exception ex)
   {
      var msg = $"{ex.Message} : {ex.InnerException}";
      Console.WriteLine(msg);
      throw;
    }
  }

我有一些逻辑可以生成需要序列化为XML的对象。为此,我使用扩展类。

public static class XmlGenerator
{
    private static FileStream _xmlFileStream;
    private static XmlWriter _xmlWritter;

public static void ConvertToXml(this Protocol obj, string pathToProtocol)
    {
        try
        { 
            XmlSerializer serializer = new XmlSerializer(typeof(Protocol));
            _xmlFileStream = new FileStream(pathToProtocol, FileMode.Append, FileAccess.Write);
            _xmlWritter = XmlWriter.Create(_xmlFileStream, new XmlWriterSettings() { OmitXmlDeclaration = true, Async = true, Encoding = Encoding.UTF8 });
            serializer.Serialize(_xmlWritter, obj, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));

        }
        catch (Exception ex)
        {
            var msg = $"{ex.Message} : {ex.InnerException}";
            Console.WriteLine(msg);
        }
        finally
        {
             _xmlWritter.Flush();
             _xmlWritter.Close();
            _xmlFileStream.Flush();
            _xmlFileStream.Close();
        }
    }
}

在本地使用C:正确创建了文件夹,并且还正确生成了XML文件。但是,在托管API的服务器上,这些都不会发生,并且日志仅显示:

Value cannot be null.
Parameter name: path : 

1 个答案:

答案 0 :(得分:0)

您的GetProtocolPath()好像有问题。 pathBuilder不返回路径格式。

private string GetProtocolPath()
    {
        try
        {
            string path = _config.GetSection("ProtocolPath").GetValue<string>("PathToFolder");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            StringBuilder pathBuilder = new StringBuilder()
                .Append(path)
                .Append("\\")
                .Append(DateTime.Now.ToString("yyyyMMdd"))
                .Append(".xml");

            return pathBuilder.ToString();

        }
        catch (Exception ex)
        {
            var msg = $"{ex.Message} : {ex.InnerException}";
            Console.WriteLine(msg);
            throw;
        }
    }