【问题标题】:How do I make an XML file an embedded resource in a vNext (ASP.NET 5) class library?如何使 XML 文件成为 vNext (ASP.NET 5) 类库中的嵌入式资源?
【发布时间】:2015-12-27 15:52:42
【问题描述】:

我有一个 MVC 6 (vNext/ASP.NET 5) 项目,其中有一个用于 DAL(数据访问层)的类库。现在我遇到了一个异常,因为 NHibernate 找不到我试图持久化的实体的映射文件。我已经看到了将这个 XML 映射文件标记为 嵌入式资源 并且不复制到输出的严格说明,但是在我设法为这个文件打开的三个属性页中,没有任何地方可以规定这个。

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

【问题讨论】:

    标签: c# asp.net embedded-resource asp.net-core


    【解决方案1】:

    更新

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

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

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

    你必须使用project.json中的resource部分,像这样

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

    默认情况下,包含 project.json 的目录中的所有代码文件都包含在项目中。您可以使用 project.json 的包含/排除部分来控制它。

    project.json 文件中处理文件的大部分部分都允许使用 glob 模式,这通常称为通配符。

    包含/排除属性列表

    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


    您可以在下面看到一个示例:

    程序.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
    

    【讨论】:

    • 谢谢你,我可以吻你。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多