Referencing Library in ASP.NET Core 1.0 (vNext) - c#

I am learning ASP.NET Core 1.0 (vNext). With that in mind, I have a solution that is structured like this:
MySolution
src
MyLibrary
MyClass.cs
project.json
MyWebSite
Startup.cs
project.json
I am successfully compiling MyLibrary from the command-line using dnu build. I ran dnu pack which generated MyLibrary.1.0.0.nupkg. There are also two folders: dnx451 and dnxcore50 which both contain MyLibrary.1.0.0.dll. I want to use MyLibrary in MyWebSite, however, I'm confused.
How do I "include" MyLibrary into MyWebSite? Do I manually copy over the .dll file? If so, which one? Should I use the nupkg file instead? This is a private assembly, I do not want to publish it globally via NuGet.
What do I put in MyWebSite/project.json to reference the MyLibrary assembly?

Are you using Visual Studio 2015 RC? If so then follow these steps:
Expand your web project in the Solution Explorer
Right click on References
Click on Add Reference... from the context menu
Select Projects from the Reference Manager
Tick the checkbox next to MyLibrary
Click OK
Profit
If you are not using visual studio then this can be achieved by updated the project.json file in your web project.
If you created a "vNext class library project" then add this to the dependencies property on the json object:
"dependencies": {
"MyLibrary": "1.0.0-*"
}

You don't have to publish the package to NuGet if you don't want to share it with other people/projects outside of your solution. You can just reference the package directly from source.
However, if that still doesn't work for you then you have three options:
Internalize using Roslyn. Example: project.json and the actual code
You can reference the sources directly. Example project.json
Create a build time dependency. Example project.json and actual code for the package

Several questions marked in bold.
(How do I "include" MyLibrary into MyWebSite?)
In MyWebSite/project.json: Add the reference like this (don't worry about the version number not showing up).
(Do I manually copy over the .dll file?)
No
(This is a private assembly, I do not want to publish it globally via NuGet.)
Then don't do that :)
Referencing your class library from your web application will make sure it is included when building the web application. Then you should copy your web application files to an application server able to run it. I think you should try to publish your web application to an Azure Web app and then use server explorer to look at what the files on the server looks like after publishing. Or did you already know there is no reason to make a package before using it in you web app?

Related

Issue finding and using lib folder in Nuget package C# project

I am working on creating a sample Nuget package to test out the process of creating an internal Nuget package for use in another project of mine. My end goal is to create a simple Nuget package, which can be installed onto another simple C# project, and tested out.
I have been following the Microsoft tutorial to create & publish a package using VS:
https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio-net-framework
I successfully created & published my package on nuget.org, called MyNugetPackage, and attempted to install it onto my other C# project called TestingMyNugetPackage. I received an error in the NuGet package console stating:
Package does not support any target framework
This error makes sense, because I had read about supporting multiple .NET versions and specifying the version under the lib folder, and I definitely did not do that when creating my package:
https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks
This idea of lib folder makes sense to me and I think I understand how to add my target .NET version to it. However, I cannot find this folder anywhere! It's not anywhere in the C# project directory. I assume I may need to create it on my own, but I'm not sure where to put it.
Many tutorials and SO questions I have read about this topic talk about how to use the lib folder, but no one ever says where it is. I'm a complete beginner to this and I know I am missing something obvious here, but I'm not sure what it is.
Edit: I did try to change my .nupkg file to a .zip file and extracting the contents in attempt to view the lib folder. This did work in extracting the contents, but I did not see any lib folder after expanding entire project tree and searching for lib.
Here is a quick layout of my C# solution tree:
Solution titled MyNugetPackage with a MyNugetPackage.sln file, a MyNugetPackage.csproj file, and a simple class Logger.cs that just has a public void Print(string text) { Console.WriteLine(text); } method:
MyNugetPackage
MyNugetPackage.csproj.1.0.0.nupkg
MyNugetPackage.nuspec
MyNugetPackage.sln
MyNugetPackage (folder)
bin (folder)
Debug (folder) -> .dll, .pdb
Release (folder) -> .dll, .pdb
obj (folder)
Debug (folder)
Release (folder)
Properties (folder)
AssemblyInfo.cs
Logger.cs
MyNugetPackage.csproj
Could someone direct me where I need to place my lib folder, so that I can add my supported .NET 4.7 framework reference, and successfully install my package?
A NuGet package (.nupkg) is just a zip file. If you are trying to view the contents of this file, open it like a zip file (using 7zip or something). Alternatively change the extension to zip. In the package you will find the "lib" folder as well as the .nuspec, and package folder (among other contents). But this is the resulting package that is built when you Pack your project, changes here would have no affect on your code.
If you're just trying to target one or more frameworks. In VS, edit your project file (.csproj). This file is an XML with a PropertyGroup that contains either a "TargetFramework" OR a "TargetFrameworks" element. To target a single framework add a TargetFramework element, to target multiple use the TragetFrameworks instead.
To target a single .Net framework:
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
Alternatively, you can target multiple frameworks.
<PropertyGroup>
<TargetFrameworks>net472; netcoreapp3.0; netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
This would target .Net 4.7.2, .Net Core 3.0, and .Net Core 2.1

How to create .dll from Nuget package?

I have a program written in C# and there is a TwitchLib.dll that provides some stuff about Twitch.tv API I guess and I want to update the .dll since there were some changes in API. How can I get .dll from a nuget package (TwitchLib).
I've tried going to /.nuget/packages/twitchlib/3.0.1/ and there is no TwitchLib.dll while for example in /.nuget/packages/twitchlib.client/3.0.3/ there is. I need .dll that is for whole TwitchLib library, not only specific parts of library.
Just build the class library by clicking the right click on solution explorer and you will see the NameOfLibrary.dll file in packages folder of your project directory.

Deploy project with xUnit reference as NuGet package

I have a library that contains some classes, which I need in several Unit Test projects. The library should be deployed as a NuGet package in my private repository. I already deployed some NuGet packages there, so I know what I have to do.
BUT: Inside of this library I need a reference to xUnit. And as soon as I add this reference, there is no more .nupkg file created when execute dotnet pack.
Another interesting effect is, that the project icon turns into a Unit Test icon as soon as I add xUnit:
Steps to reproduce:
Create a Class Library
Add a reference to the xUnit NuGet package
Right click the project and click on pack
Expected Behvior: there should be a .nupkg file in ./bin/Debug
Actual Behevior: there is no such file.
According to https://github.com/dotnet/cli/issues/7539, some projects seem to be "not packable" by default. You have to enable this manually by adding the following lines to your .csproj file:
<PropertyGroup>
<IsPackable>true</IsPackable>
</PropertyGroup>
After that, the .nupkg file is created expected.
Another option is to create Class Library project but don't add full xunit package, just:
xunit.abstractions and xunit.assert this way VS won't detect it as unit test project but you can still put some common code.

Creating and referencing class libraries with dotnet CLI and VSCode just like you can do in Visual Studio

I want to start using vscode to develop .net core apps, but I am a bit confused about how to create class libraries as separate projects and reference them.
For example: In a Visual Studio Solution I would add a Web API project and then several class libraries to that solution. Right click the Web API project and Add Reference as necessary.
Can this same thing be done with VS Code and dotnet CLI even though there is no solution concept?
Create Solution Folder
c:\Projects>mkdir SampleDotNet
c:\Projects>cd SampleDotNet
Create SampleDotNet solution
c:\Projects\SampleDotNet>dotnet new sln
Create src folder (optional)
c:\Projects\SampleDotNet>mkdir src
c:\Projects\SampleDotNet>cd src
Create Web API Project
c:\Projects\SampleDotNet\src>dotnet new webapi -n SampleDotNet.Api
Create Class Library Project
c:\Projects\SampleDotNet\src>dotnet new classlib -n SampleDotNet.Services
Reference Library Project to Web API Project
c:\Projects\SampleDotNet\src>dotnet add SampleDotNet.Api/SampleDotNet.Api.csproj reference SampleDotNet.Services/SampleDotNet.Services.csproj
Finally Add Projects to Solution
c:\Projects\SampleDotNet\src>cd ..
c:\Projects\SampleDotNet>dotnet sln add src/SampleDotNet.Api/SampleDotNet.Api.csproj
c:\Projects\SampleDotNet>dotnet sln add src/SampleDotNet.Services/SampleDotNet.Services.csproj
Commands
Result
Visual Studio Code
Visual Studio

How can i add local dll at the asp.net 5 project

I am trying to make an ASP.NET 5 site use visual studio 2015 preview, and i want to add dll at local file system to the ASP.NET 5 project. But i can't find this option, Is it no longer possible to add local dll? If yes, why?
You cannot add direct reference anymore, you would have to create your own nuget package containing it.
See: http://forums.asp.net/t/2002302.aspx?Adding+a+non+nuget+reference+to+a+vNext+project
As for the why, it is really easier to manage dependencies with nuget, download your sources anywhere, and with a single command (kpm restore) all nuget packages necessary will be downloaded.
If you have project code than you can add Foo.csproj to Bar.xproj as reference but not directly, see instructions below. It can be done without uploading packages in Beta8 but it is not simple as it should be. If you only have Foo.dll there is one hint here: Bin syntax (wrapping a dll)
Go too Foo.csproj folder, type: dnv wrap Foo.csproj.
You should now have some files generated, for me it was Foo/wrap/Foo/project.json. Go to your solution in Visual Studio, Add -> Existing project -> project.json.
Now you have some more files, including Foo.xproj which is available in Visual Studio solution, but it does not build.
Open cmd in Foo dir and execute dnv restore.
After 4) completes with no error and Foo.xproj can be built you can now go to Bar.xproj and add Foo.xproj as reference.
Open cmd in Bar directory and execute dnv restore.
You can now build Bar.xproj
I really hope that this will be easier in final version.

Categories