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.
Related
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": {}
}
Every time I build a project using the new .NET Core RC2 templates I am not provided with a runnable .EXE file. If I hit F5 for debugging my console application it runs fine through the
C:\Program Files\dotnet\dotnet.exe
application. And if I use the
dotnet run
command in the folder, it runs fine as well. But I see no way to run the application without the .NET Core CLI tools.
The contents of my
bin\Debug\netcoreapp1.0\
folder looks like this:
As you can see there is no .EXE file available. Just the dll.
Am I overlooking something? Or is there something wrong with my project.json file?
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Thanks!
There are actually 2 app models in .NET Core:
Portable apps: heavily inspired by "DNX console apps", these apps don't produce .exe files and are instead executed by the .NET Core shared runtime (whose version is defined by the Microsoft.NETCore.App package, thanks to its special type: platform attribute). The corresponding .NET Core runtime must be installed on the machine to be able to use portable apps. If the exact version cannot be found, an exception is thrown when running dotnet run.
Standalone apps: standalone apps are really similar to good old .NET console apps as they produce .exe files. The .NET Core runtime doesn't have to be installed on the machine, because it is directly embedded with the application itself.
You're currently using the first model. To use the standalone model, you need to tweak your project.json:
Add a runtimes section to list the environments your app will target (e.g win7-x64 or ubuntu.14.04-x64). You can find the complete list here.
Remove the Microsoft.NETCore.App dependency. You can replace it by this package instead: "NETStandard.Library": "1.5.0-rc2-24027".
Here's an example of a standalone app:
{
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"net451": { },
"netcoreapp1.0": {
"dependencies": {
"System.Net.Ping": "4.0.0-rc2-24027"
},
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net451+win8"
]
}
},
"runtimes": {
"win7-x64": { }
}
}
The answer is in the documentation with complete steps now.
You can create two types of deployments for .NET Core applications:
Framework-dependent deployment
Self-contained deployment
For a runnable .EXE file, the Publish self-contained should be used.
To create a runnable application from a .NET Core console application you can use the dotnet tool. Just run in your project directory:
dotnet publish --runtime win7-x64
This creates a standalone app (self-contained deployment; includes all necessary libraries consuming at least 60MB on your disk). Of course you can also choose other runtimes, like osx.10.11-x64 or ubuntu.16.04-x64.
If you used the default configuration (New Project -> Console App (.NET Core)), there is no modification of any configuration file necessary.
step 1: remove "type": "platform", from Project.json under frameworks section
step 2: add run time section to your project.json. Note each section is separeted by a comma. Add your runtime. below is just an example for win 10.
"runtimes": {
"win10-x64": {}
}
Step 3: dotnet restore command on your project. ( open cmd, go to your project folder wherever src folder is there, run dotnet restor)
step 4: dotnet pack
step 4: dotnet build -r win10-x64 - or just build.
Step 5: you can notice .exe created under debug/netcore/win10/
In ASP.NET Core try changing your app type to default, in project.json:
"Microsoft.NETCore.App": {
"type": "default",
"version": "1.0.0-*"
}
I am using EF 6 with MVC Core/6. My models and db context are in separate project and I use DI to inject the db context into the controllers. But when I try to scaffold the controller using EF 6 as the data context class I am getting the following error:
Error
There was an error running the selected code generator:
'A type with the name MyProject.DAL.ModeIs.MyModel does not exist
Microsoft.VisuaIStudio.Web.CodeGeneration.ActionInvokerb__6_0()
Microsoft. Extensions.CommandLineUtiIs.CommandLineAppIication.Exe
cute(String[] args)
Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execu
te(String[] args)'
RC2 is the first release that somewhat allows you to include non-ASP.NET Core projects in your solution. From my experience I've learned it only works if both projects were created in Visual Studio 2015 Update 2 and they both use .NET Framework 4.6.1.
There is a long running issue on GitHub about this. I've struggled with the workarounds presented in that thread and couldn't waste anymore time messing with it. So I decided to wait until a functional release comes out. I put all my shared code in an ASP.NET Core (.NET Framework) project. Made it look like a class library project by creating an empty project and deleting everything except project.json. Then any functionality that is not supported in that stack, I put in a WebAPI 2 method that I can call from my RC2 project.
This might be too late, however I had the same issue, I fixed it by adding to project.json:
"dependencies": {
.....
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.1.0-preview4-final",
"type": "build"
},
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.1.0-preview4-final",
"type": "build"
.....
and
"tools": {
.....
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.1.0-preview4-final",
"imports": [
"portable-net45+win8"
]
},
......
},
.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.
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.