【问题标题】:How to fix: Value cannot be null. Parameter name: path如何解决:值不能为空。参数名称:路径
【发布时间】:2019-06-07 23:33:36
【问题描述】:

我正在设置一个托管在 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 : 

【问题讨论】:

  • 请输入var bob = _config.GetSection("ProtocolPath") .GetValue&lt;string&gt;("PathToFolder");,并将bob的值记录在服务器上。它的价值是什么? 请不要猜测。
  • 哪一行产生了这个错误?调试时该行使用的实际运行时值是多少?
  • @mjwills 服务器上的日志显示 _config.GetSection("ProtocolPath").GetValue)("PathToFolder");为空。看起来配置文件没有被正确读取。
  • 这可能与服务器上的应用程序权限有关吗?我对此没有太多经验。
  • GetProtocolPath中逐行添加日志特征,检查是否返回预期路径。要检查它是否与权限相关,请尝试使用您的项目下的值指定您的路径,而不是c:

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


【解决方案1】:

您的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;
        }
    }

【讨论】:

  • 您好,感谢您的回答,但路径格式正确。问题似乎是在服务器上没有正确读取配置文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多