Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
Kelly Robins Avatar asked Aug 09 '15 03:08

Kelly Robins


People also ask

How do you run a class library?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.

How do I use .NET standard library in .NET core?

Open Visual Studio 2015 Go to menu click File> New> Project then choose . net Core from left menu by selecting ASP.NET Core Web Application like below image. I have chosen an ASP.NET Core sample Template, like the below image. Let's create another project as a simple class library.


1 Answers

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 fullPathToYourDll\Foo.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:\fullPathToYourDll\Foo.dll" -f 4.5.1
    

    This will generate/update the wrap folder inside your asp 5 solution folder, including a file mySolution\wrap\Foo\project.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 mySolution\wrap\Foo\project.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 ".\myNonAsp5Project\MyNonAsp5Project.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 mySolution\wrap\MyNonAsp5Project\project.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 mySolution\wrap\MyNonAsp5Project\project.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).

like image 181
Daniel J.G. Avatar answered Jan 03 '23 19:01

Daniel J.G.