如何使 XML 文件成为 vNext (ASP.NET 5) class 库中的嵌入式资源?

How do I make an XML file an embedded resource in a vNext (ASP.NET 5) class library?

我有一个 MVC 6 (vNext/ASP.NET 5) 项目,其中有一个 class 库用于 DAL(数据访问层)。现在我得到一个异常,因为 NHibernate 找不到我试图保留的实体的映射文件。我看到严格的指示将此 XML 映射文件标记为 嵌入式资源 而不是复制到输出,但在三个 属性 中的 none ] 我为这个文件设法打开的页面,有没有地方规定这个。

我只想转向基于代码的流畅映射,但这个问题并不是我的一个 NHibernate 映射文件独有的。项目项的旧 属性 页面(可通过在解决方案资源管理器中单击鼠标右键访问)已消失。我希望如果嵌入式资源这样的东西仍然存在,它在其他地方,比如 project.json,我们必须指定它。

更新

我之前的回答不再有效(自 RC2 起),resource 现在被标记为已弃用。 (感谢

现在正确的方法是使用 buildOptions/embed:

...
"buildOptions": {
  "emitEntryPoint": true,
  "embed": [ "9NLiZmx.png" ]
},
...

您必须使用 project.json 中的 资源 部分,像这样

{
  "compile": "*.cs",
  "resource": [
    "mapping.xml"
  ]
}

By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.

Most sections of the project.json file that deal with files allow glob patterns, which are often called wildcards.

include/exclude 个属性列表

name                  default value
===============================================
compile                   
compileExclude            
content               **/*   
contentExclude            
preprocess            compiler/preprocess/**/*.cs   
preprocessExclude         
resource              compiler/preprocess/resources/**/*   
resourceExclude           
shared                compiler/shared/**/*.cs   
sharedExclude             
publishExclude        bin/**;obj/**;**/.*/**   
exclude              

更多信息:http://docs.asp.net/en/latest/dnx/projects.html#including-excluding-files


您可以在下面查看示例:

Program.cs

using System;
using System.Reflection;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var assemblyName = new AssemblyName("ConsoleApp1");
            var resources = string.Join(Environment.NewLine, Assembly.Load(assemblyName).GetManifestResourceNames());
            Console.WriteLine("List of Manifest Resource Names");
            Console.WriteLine("======================");
            Console.WriteLine(resources);
        }
    }
}

project.json

{
  "version": "1.0.0-*",
  "description": "ConsoleApp1 Console Application",
  "authors": [ "Alberto Monteiro" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
    "emitEntryPoint": true
  },
  "resource": "9NLiZmx.png",
  "dependencies": {
  },

  "commands": {
    "ConsoleApp1": "ConsoleApp1"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Console": "4.0.0-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516",
        "System.IO": "4.0.11-beta-23516",
        "System.IO.FileSystem": "4.0.1-beta-23225",
        "System.Reflection": "4.1.0-beta-23516"
      }
    }
  }
}

输出

List of Manifest Resource Names
======================
ConsoleApp1.9NLiZmx.png

Alberto 的回答不再有效(自 RC2 起),resource 现在被标记为已弃用。

现在正确的方法是使用 buildOptions/embed:

...
"buildOptions": {
  "emitEntryPoint": true,
  "embed": [ "9NLiZmx.png" ]
},
...