I have created a Web API dotnet core project and i would like to use an older C# library (.NET Framework 4.5)
My project.json looks like
...
"frameworks": {
"netcoreapp1.0": {
"imports": [
"net451",
//"dotnet5.6",
//"dnxcore50",
"portable-net45+win8"
],
"dependencies": {
"LibInternal": {
"target": "project"
}
}
}
...
After adding a using in Startup.cs, i get the error - the type or namespace name "..." could not be found.
What is wrong?
Related
Hi how can I change Target Framework Version in ASP.NET Core app in Visual Studio 2015 ?
I would like to target only "NETStandard.Library": "1.6.1".
My project.json frameworks section looks like:
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
}
}
}
and my xproj file target .NET 4.5.2
TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
It might be helpful for you to offer a little more color on what it is you're trying to do, but with what you've given here, some thoughts.
First, NETStandard.Library 1.6.1 is a library/package that is installed via NuGet, not a target framework you can run on. The closest framework you can target is "netstandard1.6", which would imply you're building a class library and not a console or other stand-alone application. To run that combination, your project.json should look like this (minimally):
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.1"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
Based on what you've shown in your snippet though, it looks more like your intention was to create a console application, in which case, getting NETStandard.Library 1.6.1 would be done like this (framework section only):
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.1"
},
"NETStandard.Library": "1.6.1"
}
}
}
Update for VS2017 is that you can edit your .csproj file and manually change <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> to <TargetFrameworkVersion>netstandard1.6</TargetFrameworkVersion>.
More information about specifying different target frameworks and switching between them can be found here.
I'm having a weird problem when using XElement in .NET Core project und targeting .NET Framework 4.6. I've made a very simple project to reproduce the error.
I create a standard library project with only one class.
public class Class1
{
public Class1()
{
XElement element = null;
}
}
And in my project.json, I would like to target .net core and .net framework 4.6
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Xml.XmlDocument": "4.3.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
},
"net46": {
}
}
}
When I compile the project, I always receive the error "The type or namespace name 'XElement' could not be found (are you missing a using directive or an assembly reference?)" (this error happens only with .NET Framework 4.6)
I have no idea why it doesn't compile. I've tried to create a Console Application with .net framework 4.6 and add reference to System.Xml.XmlDocument and it works. Only with .NET core project, it doesn't.
Thank you.
I've found the solution. System.Xml.Linq has to be added as frameworkAssemblies for net46
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Xml.XmlDocument": "4.3.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
},
"net46": {
"frameworkAssemblies": {
"System.Xml.Linq": "4.0.0.0"
}
}
}
}
In a dnxCore project you can run the commands in you project.json file using "dnx command-name" How do you do that for a net461 project? If you try and run it with dnx it says the framework version in wrong. It Seems super simple but I can't figure it out or see any documentation about it.
Thanks,
Dan
You can create a dotnet console application and use it as a dotnet command. You need to set the outputName in project.json. You find more details in this link - https://learn.microsoft.com/en-us/dotnet/articles/core/tools/extensibility
Here is the sample project.json
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"outputName": "dotnet-helloworld"
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
And here is the code
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
You can execute dotnet pack and create nuget package, create a nuget.config file, mention the location of nuget package in the nuget.config and can be used in the tools section also directly from command line.
Here is two links which might help you.
http://dotnetthoughts.net/building-a-custom-dotnet-cli-tool/
http://dotnetthoughts.net/using-nuget-packages-in-aspnet-core/
I have a project that compiles that I want to test using xUnit. However although the project lets me add the references and builds, as soon as I add a using statement to the test class I get the red squiggly lines and the error "type or namespace could not be found."
Even though the xproj of the project I want to test has been added to references, and the namespaces do exist. It has to be something I have done, but I can't see what. Using earlier versions of .net, I have added references to test projects hundreds of times without an issue.
So what is different in the way .NET works, and why does it not recognize my namespaces in my referenced assemblies?
Update: I have removed xUnit and got MSTest working, but I have the same issue. So this may be a feature of the way I have dotnetcore set up, and the references in my json files more than anything else.
This structure works for me in .NET Core 1.0:
global.json
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003121"
}
}
src/MyLibrary/project.json
{
"dependencies": { },
"frameworks": {
"netstandard1.3": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
},
"version": "1.0.0"
}
test/MyLibrary.Test/project.json
{
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"MyLibrary": {
"target": "project",
"version": "1.0.0"
},
"xunit": "2.1.0"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dnxcore50",
"portable-net45+win8"
]
}
},
"testRunner": "xunit",
"version": "1.0.0"
}
Using this directory and project structure, both the Visual Studio Test Explorer and dotnet test work and can run xUnit tests.
Here is the project.json for the main dotnet core web project
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8",
"net461"
]
}
}
If I add the following net461 class library project as a reference to above one. It won't build correctly.
"frameworks": {
"net461": {
}
}
and throw error like The dependency mscorlib could not be resolved.
However, if I create a project by using the old template(no project.json), and add it as a reference to dotnet core project. It works fine.
I wonder how to fix this?
What you're doing is creating a library that will run only on .Net Framework, and then trying to use it from an application that runs on .Net Core. That won't work.
If you want to run on .Net Core, then project.json of your application should contain:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
]
}
}
And library (the version of netstandard will depend on what you want to do):
"frameworks": {
"netstandard1.4": {
}
}
If you want to use dotnet CLI, but still run on .Net Framework, then have the following in both your library and application (where you include framework assemblies inside frameworkAssemblies):
"frameworks": {
"net461": {
"frameworkAssemblies": {
}
}
}