ASP.NET 5 Web Application that references the new Class Library (package) - c#

I am in the process of creating a large solution that contains an ASP.NET 5 MVC Web App which targets the following frameworks:
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
I have several of the new Class Library (package) in my solution for the business layer, data layer, etc.. All of these libraries target the following frameworks:
"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"
}
}
}
I have a few test projects which target the following frameworks just like my MVC web app does:
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
After doing much research I mostly found that anything that is a project like my mvc app and test libraries should target dnx and projects that act as class libraries should just keep their defaults and target net/dotnet5.4.
Can someone please tell me what I am doing wrong because from my MVC Web Application I am unable to reference items from my class libraries (DAL, BLL) unless I add dnx451 to them...

You have "dnxcore50" in your Web application and "dotnet5.4" in your dll, which are probably different sets of referenced libraries. Either your Class Library should target "dnxcore50" or your Web App should target "dotnet5.4" with all it's dependencies

I just kind of resolved similar issue.
Actually, the issue is not the inability to reference your class libraries, but lack of support from Intellisense. You can manually reference your class library in project.json of your application and then use classes from your class library in your application. And application will build and run just fine. At lease mine did build and run. But VS will still highlight your classes form class library with red color and show "Cannot resolve symbol" warning message.
To add support of Intellisense I removed "dnxcore50" from project.json of application and added "net451". So, now I have the following project.json in application:
"frameworks": {
"net451": { },
"dnx451": { }
},
I cannot just reference only "net451" because I can't start the application then. DNX just exits with code 1. But with both "net451" for Intellisense support and "dnx451" for DNX support I'm able to continue work on my project and wait for proper solution in RC2 of ASP.NET Core.

Related

How to add reference between .net 4.5.2 and .net core 4.6 frmaework project

I have created two projects in a single solution. One is .net core (v4.6 framework) class library project and the second one is a normal .net (v4.5.2 framework) class library project which is used to send messages to the service bus topic. So far I have added the following reference on the project.js file.
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
},
"net452": {
"dependencies": {
"TestClassLibrary": {
"target": "project"
}
}
}
}
}
When I add references from normal .net 4.5.2 framework into .net core 4.6 frameworks. I'm getting the following error.
Error CS0246 The type or namespace name 'TestClassLibrary' could not
be found (are you missing a using directive or an assembly
reference?) TestCoreClassLibrary..NETStandard,Version=v1.6 C:\Projects\AzureServiceBusPOC\TestApplication\src\TestCoreClassLibrary\Class1.cs 5 Active
I can't access members and member functions from the normal .net 4.5.2 framework. If anybody knows please let me share your thoughts.
Note:-
I don't know whether the .net core support service bus implementation or not.
Regards,
Parthiban
I wouldn't reference one console application from another console app.
I'd create a class library, extract the shared logic into it, make it target .NET Standard and reference it from both console applications.
project.json allows you to target multiple frameworks:
"frameworks": {
"netstandard1.6": { },
"net45": {}
}

How to test a controller in .NET Core

I have an ASP.NET Core application (NetCoreApp1.1) Web API project and I would like to test a controller of that project. I added a .NET Core class library (targeting NetStandard1.6).
Now the problem I have is that according to Why doesn't Microsoft.NETCore.App support netstandard1.6? I can't reference the Web API project from that class library.
My question is then, does this mean that unless the controllers are placed somewhere else I won't be able to test them anymore? Maybe there is a way to do so but I haven't been able to achieve it in VS 2017 RC.
Test projects should be console applications, not class libraries. A console application references Microsoft.NETCore.App and shouldn't have any problems referencing your Web API project.
A simple example of the project.json for a working test project is:
{
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"xunit": "2.1.0",
"MyApiProject": {
"target": "project"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dotnet"
}
},
"testRunner": "xunit",
}
If you're using .csproj in VS 2017, it'll look different, but the principle should be the same. The test project can reference the API project locally, and uses a test runner like Xunit to run tests.

What's difference between .NetCoreApp and .NetStandard.Library?

.Net Core 1.0 has been released couple days ago, and i've started playing with it. I've created simple solution, with one project (class library => .NetStandard.Library) and second, console application (.NetCoreApp). The point is, console application has reference to library, but i cant use types form that library. Are those two frameworks incompatible? Am i missing something?
project.json for console application:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"ConsoleApplicationLibrary": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
project.json for library:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
I've figured it out, that it works, and code compiles, but visual studio still highlights types from library as unknown.
The most likely issue is that .Net Core is expecting ConsoleApplicationLibrary to be a NuGet package. If you want to reference a project, use "ConsoleApplicationLibrary": {"target": "project", "version": "1.0.0-*"}.
After you do that, don't forget to restore packages.
Ok, it's strange, but after disabling resharper, restarting VS and rebuilding solution, it works fine. It seems there is prolem with resharper support for .net core. (resharper v.9.1.3). From this: resharper ultimate blog i understand that only ultimate version supports .net core for now.
.NetCoreApp is a platform and .NetStandard.Library is a library supposed to be cross platform (portable class library) for various .NET platforms runtimes.
You can include a direct reference (package) of NetStandard.Library in any of your .NET platform project that is supported, for ex .NETCoreApp (Dot Net Core 1.X)
Reference: https://learn.microsoft.com/en-us/dotnet/articles/standard/library
I had the same issue and it turned out I needed to update Resharper to it's latest version. I had v9.1.1 so I updated it to 2016.3.2 and that fixed the issue.

Unable to find and add an ASP.NET 5 Class Library project to a solution

I recently created a new ASP.NET 5 Web App and a standard C# Class Library only to find out that the ASP.NET 5 Web App can't reference. I did research and saw that there is an ASP.NET 5 Class Library but can't find it anywhere in my fully updated VS2015 Enterprise.
Is it called Class Library (Package) or Class Library (Portable)?
Yes, Class Library (Package). In fact, you can just add a project.json file with the minimum content and reference that add it to your solution by right clicking on your solution and selecting Add > Existing project....
VS will create xproj file as soon as you reference it (which is only needed for VS). Then, you can reference it like below:
{
"version": "1.0.0",
"dependencies": {
"3-class-lib": ""
},
"frameworks": {
"dnx451": {},
"dnxcore50": {
"dependencies": {
"System.Console": "4.0.0-beta-23516"
}
}
}
}
In this case, the 3-class-lib is the name of the class library folder which ends up being the name. You can refer to this for this sample.

WebSocketTransport not available in Class Library (Portable)

I am trying to figure out why NuGet is not downloading the correct version of Microsoft.AspNet.SignalR.Client.
To make this as easy as possible to understand, here are my projects:
A WPF project, targeting .NET 4.6
A UWP project, targeting .NET 4.6 according to the new project dialog
A Class Library (Portable) project, targeting .NET 4.6 and Windows Universal 10.0, according to the "Targets" property.
All 3 projects have the NuGet package Microsoft.AspNet.SignalR.Client, and both the WPF and UWP project have the class Microsoft.AspNet.SignalR.Client.Transports.WebSocketTransport available. If I copy the class created in the WPF project to the UWP project, it finds all the references and it doesn't give me any error; however, if I try to do the same to the Class Library, it fails to find the WebSocketTransport class.
If the Class Library project has the same target as the UWP project, why isn't the WebSocketTransport class available?
Class Library (Portable) project.json:
{
"supports":
{
"net46.app": {},
"uwp.10.0.app": {}
},
"dependencies": {
"Microsoft.AspNet.SignalR.Client": "2.2.0",
"Microsoft.NETCore": "5.0.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.0"
},
"frameworks": {
"dotnet": {
"imports": "portable-net452+win81"
}
}
}
Universal Windows project.json:
{
"dependencies":
{
"Microsoft.AspNet.SignalR.Client": "2.2.0",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
},
"frameworks":
{
"uap10.0": {}
},
"runtimes":
{
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}
Is there any way I can make the Class Library project download the NuGet package that contains the WebSocketTransport method? The SignalR hub I need to use allows only WebSockets.
I was able to "hack" this by using the AutoTransport class, which seems to have chosen the WebSockets transport correctly upon contacting the Hub, otherwise it would have failed to connect:
await connection.Start(new AutoTransport(new DefaultHttpClient()));

Categories