ASP.NET 5 beta 7的共享类库参考

问题描述:

在当前环境中,我们有一些共享的公共库(C#类库.NET 4.5.1 csproj)项目,asp.net和控制台应用程序都引用了这些项目.我们正在考虑将我们的Web项目1升级到ASP.NET 5,以开始测试即将到来的一些新更改.

In our current environment, we have some shared common library (C# Class Library .NET 4.5.1 csproj) projects that are referenced by both asp.net and console applications. We are looking into upgrading 1 of our web projects to ASP.NET 5 to start testing out some of the new changes coming.

由于我无法在ASP.NET 5中对旧的csproj库类型进行项目引用,因此必须通过库的已编译DLL来引用该库,并从project.json中删除dnxcore50(这对我们来说很好).但是,我们拥有共享库的源代码,并且经常在DEV环境中停止更新,以便经常需要对DLL进行修改和更改. ASP.NET 5将DLL放在lib目录中,并且不使用csproj目录的路径.

Since I can't do a project reference in ASP.NET 5 to the old csproj library type, I have to reference the library by it's compiled DLL and remove dnxcore50 from project.json (which is fine for us). However, We own the shared library source and constantly bring down updates in our DEV environment for modifications and changes will be needed often to the DLL. ASP.NET 5 puts the DLL inside of a lib directory and does not use a path to the csproj directory.

如果您拥有asp.net&amp ;,该怎么办?控制台csproj应用程序与asp.net一起5个需要共享公共代码库而无需维护2个代码库的应用程序?您会建议一个编译任务来编译公共库项目并在编译asp.net 5项目之前替换lib中的参考DLL还是为我们的共享库设置本地nuget存储库,因为对csproj来说,现在项目参考已成问题?我们是TFS中所有这些代码的共享团队,因此我所做的一切都需要轻松地复制给其他所有人.

What would you do if you have asp.net & console csproj apps along with asp.net 5 apps needing to share a common code library without having to maintain 2 code bases? Would you recommend a build task to compile the common library project and replace the reference DLL in lib before compiling the asp.net 5 project or instead setup a local nuget repo for our shared library since project references are out of the question now for csproj? We're a shared team with all of this code in TFS so whatever I do needs to be replicated easily for everyone else.

如果共享代码不经常更改,我将使用共享的nuget存储库.这样,您将获得使用nuget软件包的所有好处:版本控制,易于还原等.您的解决方案可以减少项目量并加快编译速度. 但是,如果共享代码不断更改,则执行pack-push-restore过程可能会很痛苦.

If the shared code doesn't change to often, I would go with a shared nuget repository. This way, you get all the benefits of using nuget packages: versioning, easy restoration, etc. Your solution can have less projects and compile faster. But if the shared code is constantly changed, going through pack-push-restore process could be painfull.

还有另一种方法-dnu有一个方便的命令,名为wrap.它将现有的.csproj文件包装为project.json文件,然后可以由aspnet5项目引用.

There is one other way - dnu has a handy command named wrap. It wraps your existing .csproj files into project.json files that can be then referenced by aspnet5 projects.

您可以执行以下操作:

  1. 添加 global.json 文件.在projects部分中,列出包含您的源代码的目录,例如:

  1. Add a global.json file in the some top-level directory that contains your projects. In the projects section, list directories that contain your source-code, for example:

{
    "projects": [ "src", "test" ],
    "sdk": {
        "version": "1.0.0-rc1-final"
    }
}

  • 在包含global.json的同一目录中,对每个现有的.csproj项目执行dnu wrap.

  • In the same directory that contains global.json execute dnu wrap for each existing .csproj project.

    dnu wrap src/my.project/my.project.csproj  
    

    这应该创建一个目录wrap,其中包含包裹.csprojsproject.json个文件.示例文件如下:

    This should create a directory wrap containing project.json files that wrap .csprojs. A sample file looks like this:

    {
        "version": "1.0.0-*",
        "frameworks": {
            "net45": {
                "wrappedProject": "../../src/my.project/my.project.csproj",
                "bin": {
                    "assembly": "../../src/my.project/obj/{configuration}/my.project.dll",
                    "pdb": "../../src/my.project/obj/{configuration}/my.project.pdb"
                }
            }
        }
    }
    

    请注意,wrap目录也已添加到global.jsonprojects部分.

    Note that wrap directory is also added to projects section in global.json.

    在您的解决方案中,添加一个新的aspnet项目并添加对该包装项目的引用.只需添加:

    In your solution, add a new aspnet project and add a reference to the wrapped project. Just add:

    "my.project": ""
    

    dependencies部分. Aspnet应该自动在根目录中拾取global.json文件,并在其中列出的所有目录(包括wrap目录)中查找项目.

    to dependencies section. Aspnet should automatically pick up global.json file in root directory, and will look for projects in all directories listed there, including wrap directory.

    现在您可以使用了-您可以使用my.project中的所有类,在调试时进入它们,转到定义等.请注意,在您的解决方案中,您仍然拥有旧的csproj .

    Now you're good to go - you can use all classes from my.project, step into them while debugging, go to definition, etc. Note that in your solution, you still have the old csproj.

    您可以在此处找到示例代码: https://github.com/heavymetaldev/aspnet5-wrap .

    You can find a sample code here: https://github.com/heavymetaldev/aspnet5-wrap.

    我想如果您的项目中有一些自定义内容(例如条件编译,自定义包含等),这可能会变得有些复杂.

    I suppose this may get a little complicated if you have some custom things in your projects, like conditional compilation, custom includes, etc.