How to use existing code in asp.net vNext - asp.net-core

Trying to play a bit with asp.net vNext.
Let's say I have MyCode.dll assembly with some code I have and want to use in my be vNext project. How can I reference existing .net 4.5 assembly?
I've packed it into nuget package, and then by using local feed add it to vNext project. Also used kpm restore to actually download the package.
It looks like package added successfully, but no code from MyCode.dll available, it's simply not used by intelliSence and build throw Type or namespace chould not be found
I could move code from MyCode.dll to asp.net 5 class library, but I need to reuse existing dll that also is used by other projects, like old versions of asp.net etc.

My work around was to add a local nuget server and correctly spec, pack and push the assembly to the nuget server by doing that you can target multiples version of .NET like aspnetcore5, aspnet5 and net45. When creating the specification file of the assembly don't forget to include their corresponding dependencies for each version.
To create a local server please see instruction here
Please see example specification.
<dependencies>
<group targetFramework="net45">
<dependency id="Newtonsoft.Json" version="6.0.6" />
</group>
<group targetFramework="aspnet50">
<dependency id="Newtonsoft.Json" version="6.0.6" />
<dependency id="System" version="4.0.0.0" />
<dependency id="System.Core" version="4.0.0.0" />
<dependency id="Microsoft.CSharp" version="4.0.0.0" />
<dependency id="mscorlib" version="4.0.0.0" />
</group>
<group targetFramework="aspnetcore50">
<dependency id="Newtonsoft.Json" version="6.0.6" />
<dependency id="System.Runtime" version="4.0.20-beta-22231" />
<dependency id="System.Collections" version="4.0.10-beta-22516" />
</group>
</dependencies>

If you are using the latest VS 2015 CTP, the easiest way is to add your existing .NET 4.5 project to your ASP.NET 5.0 solution and use the "Add Refernece" dialog to reference your 4.5 library. This will make your project.json file look something like this:
"frameworks": {
"aspnet50": {
"dependencies": {
"MyClassLib": "1.0.0-*"
}
},
"aspnetcore50": { }
},
You can only use 4.5 libraries while running the full ASP.NET 5.0 runtime so be sure project settings aren't set to target the ASP.NET 5.0 Core runtime. If you want your library to be cross-platform and target the Core runtime you'll need to rebuild it as an ASP.NET 5.0 Library.

When I added "older libraries" to the new ASP.NET, I get the Internal Server 500. With some errors in the IISExpress.
Specifically with System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
After research and some internal Debugging with DNX we finally found the source of failure.
The assembly with display name 'System.Web.Mvc' failed to load in the 'Anonymous' binding context of the AppDomain with ID 1. The cause of the failure
was: System.IO.FileNotFoundException: Could not load file or assembly System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot
find the file specified.
As "my older" libraries was using and older MVC. I updated my libraries after to submit the following command:
Install-Package Microsoft.AspNet.WebApi.Core -version 5.2.3
With the following conclusion:
If you libraries that you are trying to use in ASP.NET vNext depends or use an version MVC 5.2.3 or older, You are unable to use inside the new framework.

Related

Missing Nuget packages error in ,NET core 3.1 app

I am using VS code and not Visual Studio to run my .NET core 3.1 web app.
I have a .NET core 3.1 app. As I am using an Client VDI machine ( It is using Win 7 SP1 operating system) which is blocking the Nuget.Org URL currently.
so as a work around, I have copied over all the Nuget package into an folder in my VDI and updated the path in the Nuget.Config.
But still I am getting below
Missing Package" error. My App do not DIRECTLY depends on these packages.
following is my Nuget.Config file,
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!-- add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" / -->
<add key="nuget.org" value="C:\NugetPackageDownloadLocation" / >
</packageSources>
</configuration>
any help will be appriciated.
Have you tried adding a package source via the CLI? Given how .NET Core handles transitive dependencies getting all the packages required for a relatively complex solution might be tricky.
nuget sources Add -Name "MyServer" -Source \\myserver\packages

Nuget packing not including dependncies in right way

I have a C# library, let say "Utilities" that uses Log4net as dependency, I am using .net framwork 4.6.1, I finished developeing this library and do a nuget pack Utilities.projcs and then push this package.
While I'm try to use this Utilities in another project that which is an asp.net core "2.1" project, I got this exception
**Method not found: 'log4net.ILog log4net.LogManager.GetLogger(System.String)'.**
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'log4net.ILog log4net.LogManager.GetLogger(System.String)'.
Source=Utilities
StackTrace:
at Utilities.Logging.MyLogManager.GetLogger(String loggerKey)
My Utilities nuget spec file
<?xml version="1.0"?>
<package >
<metadata>
<id>Utilities</id>
<version>1.2.0</version>
<title>Internal Utilities</title>
<authors>My Team</authors>
<owners>My Team</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This dll contains parsers,loggers and other functionalities that is commonly used.</description>
<releaseNotes>Rlease notes</releaseNotes>
<copyright>Copyright 2020</copyright>
<tags>Utilities</tags>
<dependencies>
<dependency id="log4net" version="2.0.8" include="all"/>
</dependencies>
</metadata>
<files>
<file src="bin\Debug\log4net.dll" target="" />
<file src="bin\Debug\log4net.xml" target="" />
<file src="bin\Debug\Utilities.dll" target="" />
</files>
</package>
Any ideas to solve this, I spent the last couple of days to solve it but no luck
NOTE: When I add Reference manually by location in my pc (My library + Log4net) it works fine!!
Create class library in .net standard.
For more info about .net standard visit https://learn.microsoft.com/en-us/dotnet/standard/net-standard
How to create .net standard class library -> https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?tabs=csharp
How to create .net standard Nuget -> https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli
this image will help you understand the concept behind shared libraries.

How to install Microsoft.AspNetCore.Mvc.Core with version 3.1.0 from NuGet?

I have a netstandard2.0 class library project, which depend on Microsoft.AspNetCore.Mvc.Core with version 2.1.1.
I am presently trying to upgrade this project to netstardand2.1,being used by a netcore3.1 application, but I find that the package Microsoft.AspNetCore.Mvc.Core with version 3.1.0 is not available on NuGet? Where can I find this package?
I found this solution in ASP.NET Core 3.1 documentation.
With the release of .NET Core 3.0, many ASP.NET Core assemblies are no longer published to NuGet as packages. Instead, the assemblies are
included in the Microsoft.AspNetCore.App shared framework, which is
installed with the .NET Core SDK and runtime installers. For a list of
packages no longer being published, see Remove obsolete package
references
As of .NET Core 3.0, projects using the Microsoft.NET.Sdk.Web MSBuild SDK implicitly reference the shared framework. Projects using the
Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference
ASP.NET Core to use ASP.NET Core APIs in the shared framework.
To reference ASP.NET Core, add the following element to your project file: (*.csproj file)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
For more details visit this link: Use ASP.NET Core APIs in a class library
We have to add below tags on *.csproj file?
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
I added, my net core class worked now!

how to setup EFcore database first in separate class library in ASP.NET Core MVC application?

I have a DB and I want to add a separate class library for entity framework to access my data but when I run scaffold command it makes a DB model in my web application.
You should select your Data project as default project in package manager console.
Be careful, you have ef core references in data.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0-preview3-35497"/>
<PackageReference Include="System.Data.Common" Version="4.2.1" />
</ItemGroup>
</Project>
I realized that there are some errors in ClassLibrary projects. You can take a look at them:
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#targeting-class-library-projects-is-not-supported
https://github.com/dotnet/cli/issues/8735
https://github.com/aspnet/EntityFrameworkCore/issues/10298
#ibrahimozgon's answer is right and helped me. However, I encountered a few errors in the DbContext class on the way which he didn't mention how to solve:
'EntityTypeBuilder' does not contain a definition for 'ToTable' and no accessible extension method 'ToTable' accepting a first argument of type 'EntityTypeBuilder' could be found (are you missing a using directive or an assembly reference?
and
'KeyBuilder' does not contain a definition for 'ForSqlServerIsClustered' and no accessible extension method 'ForSqlServerIsClustered' accepting a first argument of type 'KeyBuilder' could be found (are you missing a using directive or an assembly reference?
To resolve these errors in DbContext class, open up package manager console again and select the default project to be the class library. Enter these commands one by one:
- Install package: Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.4 (or whichever latest version)
- Install package: Install-Package Microsoft.EntityFrameworkCore.Relational -Version 2.2.4 (or whichever latest version)
- If errors persist try: Install-Package or Update-Package Microsoft.EntityFrameworkCore.Tools (or whichever latest version)

Create NuGet package for C++/CLI (mixed) assembly

I have created a C++/CLI (mixed) assembly which has a managed wrapper class around some unmanaged C++ code. The managed part targets .NET 4.6.1, I got a file entry.cpp with just this line to do that:
[assembly:System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.6.1", FrameworkDisplayName = L".NET Framework 4.6.1")];
When I now manually include the compiled assembly in a .NET 4.6.1 project I can use the managed class as expected.
This project can be build four ways: x86 or x64 as either debug or release build. It has no managed dependencies.
Now I want one (or if required multiple) NuGet packages which I can upload to my feed and use the wrapper assembly easily in every .NET 4.6.1 compatible project I would like. How do I achieve this?
So far I tried two approaches:
First, I created a .autopkg file which is according to this blog post the way to provide native DLLs. The files section of that file looks like this:
files {
// include: { *.h };
[x86,v120,release] {
symbols: { ..\Release\*.pdb; }
bin: { ..\Release\*.dll; }
};
[x86,v120,debug] {
symbols: { ..\Debug\*.pdb; }
bin: { ..\Debug\*.dll; }
};
};
This process results in three .nupkg files which I can upload to my feed. But when I try to install that package to a .NET 4.6.1 project I get this error message:
Could not install package 'MyCppCliWrapper.redist 1.0.0.2'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
So I rethought if I should not use the way for managed assembly to create the .nupkg because the assembly has a managed class I want to use from managed code. I created a .nuspec (using nuget spec) and provided the metadata. Then I try to create my package like this:
nuget pack MyCppCliWrapper.nuspec -Prop Configuration=Release -Prop Platform=x86 -Build
But that results in a package which contains the whole project with all source files and temporary files, just like a zip file of that folder.
Obviously there is also missing the meta information about targeted framework.
When I try to use the project file to create the package (like with C# assemblies) this fails too:
Please specify a nuspec, project.json, or project file to use
The C++ project files, .vcxproj, seem to be unsupported by NuGet (I am using the NuGet 3.5.0.1938 command line utility).
Will I need to build manually and provide all files in the files section of the .nuspec? If yes, how would he know from this line which DLL is for which .NET framework plus platform?
<file src="bin\**\*.dll" target="lib" />
I believe Hans Passant is right, this is just a regular managed nuget package but the packager does not handle the .vcxproj files so I made up my own .nuspec:
<?xml version="1.0"?>
<package >
<metadata>
...
</metadata>
<files>
<file src="readme.txt" target="" />
<file src="bin\Win32\Release\*.dll" target="lib\net461" />
<file src="bin\Win32\Release\*.pdb" target="lib\net461" />
</files>
</package>
The package generated this ways works.
There is one question remaining: This way, do I have to do two packages, one for 32bit and one for 64bit - or is it possible to include them in one package (which I would prefer) and have the consuming project use one or another depending on the target architecture (any-cpu is mostly 32bit)?
I don't know if this could still help you, but I've managed to pack both x64 and x86 C++ code, and a C# wrapper that was compiled on AnyCPU.
On My C# project, I have two Platforms: "x86" and "x64".
On my Nuget folder, I have the following structure:
\Project
\Project.1.0.nuspec
\build
\x64
\*.dll
\*.pdb
\x86
\*.dll
\*.pdb
\Project.targets
\lib
\net452
\Wrapper.dll
\Wrapper.pdb
Project.nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Project</id>
<version>1.0</version>
<authors>nilsonneto</authors>
<owners>nilsonneto</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Example.</description>
<references>
<reference file="Wrapper.dll" />
</references>
</metadata>
<files>
<file src="build\Project.targets" target="build\Project.targets" />
<file src="build\x64\**" target="build\x64" />
<file src="build\x86\**" target="build\x86" />
<file src="lib\net452\Wrapper.dll" target="lib\net452\Wrapper.dll" />
<file src="lib\net452\Wrapper.pdb" target="lib\net452\Wrapper.pdb" />
</files>
</package>
Project.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)\$(Platform)\*.*" />
<Content Include="#(NativeLibs)">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Notice the $(Platform), which is where the name of the Platform being build on Visual Studio will be placed, which is why I separated the C++ DLLs in folders with the same name as the Platforms in Visual Studio.
And according to the documentation (https://learn.microsoft.com/en-us/nuget/create-packages/native-packages), all native DLLs have to be placed in the \build directory.
Native NuGet packages targeting native then provide files in \build, \content, and \tools folders; \lib is not used in this case (NuGet cannot directly add references to a C++ project). A package may also include targets and props files in \build that NuGet will automatically import into projects that consume the package. Those files must be named the same as the package ID with the .targets and/or .props extensions.
So, just adjust the folder names based on the Platforms you support on the .NET project and your set.