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

后端 未结 2 742
天涯浪人
天涯浪人 2020-12-17 19:42

I have an MVC 6 (vNext/ASP.NET 5) project, with one class library for the DAL(Data Access Layer). Now I am getting an exception because NHibernate can\'t find the mapping fi

相关标签:
2条回答
  • 2020-12-17 20:01

    UPDATE

    My previous answer is no longer valid (since RC2), the resource is now marked as deprecated. (Thanks @Yossarian)

    Proper way how to do this is now to use buildOptions/embed:

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

    You must use the section resource in project.json, like this

    {
      "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.

    List of include/exclude properties

    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              
    

    More info: http://docs.asp.net/en/latest/dnx/projects.html#including-excluding-files


    You can see a sample below:

    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"
          }
        }
      }
    }
    

    Output

    List of Manifest Resource Names
    ======================
    ConsoleApp1.9NLiZmx.png
    
    0 讨论(0)
  • 2020-12-17 20:11

    Alberto's answer is no longer valid (since RC2), the resource is now marked as deprecated.

    Proper way how to do this is now to use buildOptions/embed:

    ...
    "buildOptions": {
      "emitEntryPoint": true,
      "embed": [ "9NLiZmx.png" ]
    },
    ...
    
    0 讨论(0)
提交回复
热议问题