Legacy assembly reference error in asp.net-vnext - asp.net

I am using VS2015 CTP5 and I am referencing a legacy class library compiled with 4.5.1. During compile, I get this warning:
The primary reference "D:\components.dll" could not be resolved because it was built against the ".NETFramework,Version=v4.5.1" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
Here is my project.json after adding the reference
"frameworks": {
"aspnet50": {
"dependencies": {
"components": "1.0.0-*"
}
}
},

Since the "component" library is build for .net 45 and assuming that you build that library in an older version of visual studio, it will not work in aspnetcore5 but will work on aspnet5 (these are the new version for .net). if you want to get rid of the error and still use your component library, you will need remove the aspnetcore5 json node from the project.json file but the project that you building will not be compatible with aspnetcore5. So your project.json file for the frameworks section should look like this.
"frameworks": {
"aspnet50": {
"frameworkAssemblies": {
"System": "4.0.0.0"
},
"dependencies": {
}
},
"net45": {
"dependencies": { "components": "1.0.0"},
"frameworkAssemblies": { }
}
}
And your reference should look like, I have warning sing next to the component library because I don't have that in my code.
You can look at this question to get more information.
Question 1,
Question 2

Add the library to frameworkDependencies not dependencies
"net45": {
"frameworkAssemblies": {
"components": "1.0.0"
},
"dependencies": {
// NuGet packages go here
}

For me, none of the above worked and after spending a lot of hours investigating... I finally found a solution!
I have to create a new package in the NuGet Package Explorer for my dll, save and export it to a local folder (use the File->Save and File->Export commands). Then declare my local repository(folder) to Visual Studio, go to Tools->Options->NuGet Package Manager->Package Sources and insert a record for my local repository - see image below.

Related

How to Reference ".NETPlatform,Version=v5.0" Framework in Xamarin.Forms Project?

I'm currently working on a Xamarin.Forms project named ABCD, using macOS Sierra v10.12.6 and Visual Studio (VS) for Mac v7.3.2 (the steps leading up to this problem are detailed here).
Having updated my NETStandard.Library package from version 1.6.0 to 2.0.1*, I notice that the project.json file still says:
"frameworks": {
"netstandard1.5": {}
}
*VS update this package to 2.0.1, despite Terminal showing that dotnet --version is 2.1.3.
I went ahead and replaced netstandard1.5 with netstandard2.0 and rebuilt the project. But was then immediately met with this error:
Error: Your project is not referencing the ".NETPlatform,Version=v5.0" framework. Add a reference to ".NETPlatform,Version=v5.0" in the "frameworks" section of your project.json, and then re-run NuGet restore. (ABCD)
I've tried to fix this in the following ways:
1 – Updating project.json:
"frameworks": {
"netstandard2.0": {},
".NETPlatform,Version=v5.0": {}
}
2 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {},
".NETPlatform,Version=v5.0": {
"imports": ["netstandard2.0"]
}
3 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {},
".NETPlatform,Version=v5.0": {
"imports": [".NETCore,Version=v5.0"]
}
4 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {
"imports": [".NETCore,Version=v5.0"]
}
5 (source) – Updating project.json:
"frameworks": {
".NETPlatform,Version=v5.0": {
"imports": ["netstandard2.0"]
}
6 (source, source) – Updating ABCD.csproj:
<PropertyGroup>
<NuGetTargetMoniker> .NETStandard,Version=v2.0 </NuGetTargetMoniker>
</PropertyGroup>
I thought I might find something helpful here and here but wasn't successful.
The same error from above still remains after having tried the above fixes. How do I fix this?
EDIT:
7 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {},
"netplatform50": {}
}
Still seeing the same error.
This post suggests not using project.json but instead to do a dotnet migrate in your project root folder to convert it so that it uses .csproj instead

Using reflection in net core packages targeting several frameworks

I am developing a library(nupkg) in .net core targeting both net45 and netstandard1.3 and at some point I need to use reflection so my project.json looks like this:
{
"version": "1.1.3",
"dependencies": {
common-dependencies
}
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.Reflection": "4.0.0.0"
},
"dependencies": {
"NLog": "4.3.5",
"Newtonsoft.Json": "6.0.4"
}
},
"netstandard1.3": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Reflection.TypeExtensions": "4.1.0",
"Newtonsoft.Json": "8.0.2",
"NLog": "4.4.0-*"
}
}
}
}
Most of the times after a mere install-package my-package everything works like a charm, however a couple of times I get this error on installing:
install-package : Failed to add reference. The package 'my-Package' tried to add a framework reference to 'System.Reflection' which was not found in the GAC. This is possibly a bug in the
package. Please contact the package owners for assistance.
I've found a workaround, which is to uninstall-package every other package on the project I want to install my package. This is very odd and undesired behavior I think.
I do have noticed however that on some of those project had different versions of Newtonsoft.Json installed on different projects. This is a warning I also get when installation fails:
Install failed. Rolling back...
Package 'my-package : Newtonsoft.Json [6.0.4, ), NLog [4.3.5, )' does not exist in project 'Target.Project'
I have no idea what could be going on here, and the fact that it happens randomly and the workaround to fix it don't help much.
Any ideas please?
System.Reflection is a reference assembly, but not a runtime assembly. So, you need to use it when building your code, but not reference it from your built assembly.
To do that, specify "type": "build", e.g.:
"frameworkAssemblies": {
"System.Reflection": { "type": "build" }
}

Using Shared Libraries with .NET Core

I wrote my open source library, LINQ to Twitter, with shared libraries to minimize deployment artifacts and handle platform specific features. I want to support .NET Core and am thinking that the fastest approach would be to reference the shared libraries. The Add References dialog didn't show the shared libraries, so I tried project.json:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"LinqToTwitter.Shared": "*",
"LinqToTwitter.Shared.net": "*"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
I tried a few combinations of versions, but didn't get anywhere. The error messages contain something like this:
The dependency LinqToTwitter.Shared >= * could not be resolved.
Next, I opened the *.xproj and pasted the following Imports into the Project section:
<Import Project="..\LinqToTwitter.Shared\LinqToTwitter.Shared.projitems"
Label="Shared" />
<Import
Project="..\LinqToTwitter.Shared.net\LinqToTwitter.Shared.net.projitems"
Label="Shared" />
This doesn't show references in VS, nothing in metadata, and (as it would follow) can't reference any shared library types from a console app that references the .NET core app.
if library is in the same solution you can reference it using target:project something like
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"LinqToTwitter.Shared": {"target": "project"},
"LinqToTwitter.Shared.net": {"target": "project"}
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
Good news - Visual Studio 2017 now allows .NET Core library projects to reference Shared projects.

Unable to generate Scaffolding controller in asp.net5?

I am getting below error when I try to run From :
c:\visual studio 2015\Projects\AppTest\src\AppTest
PM> dnx gen controller -name BooksController --dataContext MyDBContext --model Book
Finding the generator 'controller'...
dnx : Could not load file or assembly 'AppTest' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500).
I am New to .Net and I am not able to figure it out. I tried updating webtools of VS2015 also still No luck. Any Suggestions?
You try use command line scaffolding, which is dropped before RC1 (I'm not sure in beta7 or beta8). There are only scaffolding in Visual Studio. To be able to see the corresponding scaffolding dialog you have to add some packages in the dependencies of the project.json. See the answer for more details.
After that you can select Controllers folder in the Solution Explore of Visual Studio 2015, opens the context menu and choose "Add" / "Controller...". If your project have all required dependencies then you will see "MVC 6 Controller with views, using Entity Framework" which you can to choose and to click "Add" button. The next dialog contains additional options which corresponds the options of old dnx gen controller utility.
I had the same issue and i solved it doing the following steps:
Make sure the assembly you are referencing and the MVC project are targeting the same framework versions.
By default, the "frameworks" section of the file project.json in a new ASP.NET Core MVC project has the folowing values:
"frameworks": {
"dnx451": { },
"dnxcore50": { }}
And the "frameworks" section in the project.json file of a new Class Library is created as follows:
"frameworks": {
"net451": { },
"dotnet5.4": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Runtime": "4.0.21-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}}
You see, a class library is not targeting the same framework version of the MVC Project. You have to replace "dotnet.5.4" on the class library's project.json file with "dnxcore50", to match the ones used in the MVC project.
"frameworks": {
"net451": { },
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Runtime": "4.0.21-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}}
After that, save the package.json file and let Visual Studio do it's magic to rebuild the package.
I tried to replace the "dnxcore50" with "dotnet5.4" in the MVC Project but it didn't work. Some other assemblies referenced in the MVC Project didn't work with this framework version and a i had a build error.
Make sure you are using the same version of Entity Framework on both assemblies.
IF you want to use EF7, fust run update-package EntityFramwork.Core -Pre for both assemblies and you should be good.
Since to scaffold the controller you need a DbContext, make sure you configured the DbContext you want to use in the ConfigureServices method inside the Startup.cs class.
Should be like this:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyContext>(options =>
options.UseSqlServer(Configuration["Data:MyContextConnection:ConnectionString"])
.MigrationsHistoryTable("MyContextMigrations")
.MigrationsAssembly("MyContextAssembly"));
services.AddMvc();
}
Don't forget to put you connection string in the appsettings.json file or else VS will throw another error.
I did this and i was good to go, i hope this solution works for you.

Mocking framework for ASP.net 5.0 vnext class library project

I have created a ASP.net vnext class library project in Visual Studio 2015. Now I want to test my project. So, I want to use mocking stuffs in my test project.
Here is my project.json file
{
"version": "1.0.0-*",
"dependencies": {
"xunit": "2.0.0-rc3-build2880",
"xunit.runner.aspnet": "2.0.0-rc3-build52",
"My project name with version",
"Moq": "4.2.1502.911"
},
"commands": { "test": "xunit.runner.aspnet" },
"frameworks": {
"aspnet50": {
"dependencies": {
}
}
}
}
But I am getting error :Dependency Moq with the Version 4.2.1502.911 could not be resolved.
I also tried different version of Moq, but results the same: dependency error.
Without Mock I can't move further in testing.
Any help?
based on comment, the error is incorrect package manager settings :
the correct feeds to use are : nuget.org/api/v2 and myget.org/F/aspnetrelease for the asp ones

Resources