Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

visual studio 2015 - How do I use a standard class library in MVC6?

Is there a reasonably convenient way to include a standard class library in MVC6? I thought the standard add reference dialog could create a wrapper but I'm not having any luck. Publishing it as a nuget package seems pretty inconvenient for ongoing development debugging.

If there isn't a wrapper is there a built in way to convert the standard class library to the new type?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have seen a couple of different approaches so far (using the dnu utility or adding references using VS). It is also slightly different depending if you want to reference an existing assembly or to reference a non asp 5 project within your solution.

When you want to add a reference to an existing assembly that is not part of your solution:

  • Use the dnu wrap command to add the reference. You can open a developer command prompt and navigate to your solution folder or even easier just open the package manager console inside visual studio.

    Now if you want to add a reference to fullPathToYourDllFoo.dll you will run the following command (where the framework argument is required when wrapping an assembly, and is defined as Target framework of assembly to be wrapped):

    dnu wrap "C:fullPathToYourDllFoo.dll" -f 4.5.1
    

    This will generate/update the wrap folder inside your asp 5 solution folder, including a file mySolutionwrapFooproject.json that looks like:

    {
      "version": "1.0.0-*",
      "frameworks": {
        "net451": {
          "bin": {
            "assembly": "../../../FooSln/Foo/bin/Debug/Foo.dll",
            "pdb": "../../../FooSln/Foo/bin/Debug/Foo.pdb"
          }
        }
      }
    }
    

    Finally, update the project.json file of your asp 5 to include a reference to your wrapped assembly as in:

    "frameworks": {
      "dnx451": {
        "dependencies": {
          "Foo": "1.0.0-*",
        }
      }
    },
    
  • Alternatively, use VS. Right click references in your ASP 5 project, select Add Reference... and then click on Browse. Now navigate to the folder containing the assembly and select the dll file.

    Visual studio will update for you the wrap folder (Creating the mySolutionwrapFooproject.json) and will even add the dependency in your project.json (The one in your asp 5 project).

When you want to add a reference to a non asp 5 project within your solution:

  • Use the dnu wrap command to add the reference.

    The process is quite similar to the previous scenario of adding a reference to an existing assembly. Now if you want to add a reference to a non asp 5 project within your solution, you will run the following command:

    dnu wrap ".myNonAsp5ProjectMyNonAsp5Project.csproj"
    

    As when adding a reference to an existing assembly, this will generate/update the wrap folder inside your asp 5 solution folder, although this time the file mySolutionwrapMyNonAsp5Projectproject.json is slightly different since it is wrapping a project and not an assembly:

    {
      "version": "1.0.0-*",
      "frameworks": {
        "net451": {
          "wrappedProject": "../../MyNonAsp5Project/MyNonAsp5Project.csproj",
          "bin": {
            "assembly": "../../MyNonAsp5Project/obj/{configuration}/MyNonAsp5Project.dll",
            "pdb": "../../MyNonAsp5Project/obj/{configuration}/MyNonAsp5Project.pdb"
          }
        }
      }
    }
    

    Again, you will need to manually update the project.json file of your asp 5 to include a reference to your wrapped assembly as in:

    "frameworks": {
      "dnx451": {
        "dependencies": {
          "MyNonAsp5Project": "1.0.0-*",
        }
      }
    },
    
  • Alternatively, use VS. Right click references in your ASP 5 project, select Add Reference.... Now expand Projects on the left hand side, select the project and click ok.

    Visual studio will update the wrap folder (Creating mySolutionwrapMyNonAsp5Projectproject.json) and will even add the dependency in your project.json (The one in your asp 5 project).

PS. I have been upgrading to Win10 and installing/uninstalling stuff as of lately and somehow I ended up with an environment variable Platform=MCD. This will be taken by MSBuild as the default platform and may give you some pain. Specifically, I was getting the error Failed to resolve references when running the dnu wrap command for wrapping a csproj file. (As internally it uses msbuild to resolve the csproj references).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...