I have some code in a Universal Windows class library I want to use from a .NET core project.
I have a pi running Windows IOT for which I installed adafruits PWM hat. I have found an UWP project containing the needed C# code and that works great.
But at the pi I also want to have an asp.net core project that uses the UWP project. But if I add a reference from my asp.net core to UWP it tells that it is not supported which also make sense as they are at same level in the .net architecture.
I use Visual Studio 2019 and just installed .NET Core 3.0 preview3.
Any way I can achive to use the UWP from my core project?
If you want to reference, it can't be done as you said yourself it makes no sense.
Some workaround would be to move the code from UWP project to the new .Net Core project and then reference it from both projects. If you use some UWP APIs you might consider to use a shared project with conditional compiling instead of the new .Net Core project.
Try adding
<ItemGroup>
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.6.0-preview4.19212.13" />
<Reference Include="Windows">
<HintPath>C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd</HintPath>
</Reference>
</ItemGroup>
to your csproj file. It allowed me to use UWP api in asp.net core preview 3 project.
Related
I created an UWP app, and a .NET 6.0 project, and added reference of UWP project to the .NET 6 project.
Output of executing proj.bat file that runs this solution executes the following error:.
Error NU1201: Project MyUWPProject is not compatible with net6.0 (.NETCoreApp,Version=v6.0). Project MyUWPProject supports
: uap10.0.18362 (UAP,Version=v10.0.18362)
How can fix this? Is what I'm trying to do even possible?
The error message shows that the environment of your .NET 6.0 project is .net6.0 and the environment of the UWP project doesn't support .net6.0. So the UWP project is not compatible with the .NET project.
If you want to use Windows Runtime APIs, you could just make some confirugation in for your .NET 6.0 porject so that you could directly call Windows Runtime APIs in your app. You will need to Edit Your Project File and Modify TargetFramework Element to net6.0-windows10.0.19041.0.
You could check this document - Call Windows Runtime APIs in desktop apps for detailed steps.
I have a .Net 4.6 class library X, whose x.dll I'm referencing in my .NetCore Console application.
However, when I try to run the console application, it just throws an error saying that the dll does not exists under "bin\Debug\netcoreapp2.1\bin" folder. Whereas, the "bin\Debug\netcoreapp2.1" does not have a bin folder.
I could see the x.dll of my class library under bin\Debug\netcoreapp2.1. but no bin folder.
I tried searching this issue over the internet but couldn't find anything.
The problem here is that you can't reference a .NET Framework dll from a .NET Core application. To get around this, you can multi-target your class library to both .Net 4.6 and .NET Standard.
Check .csproj file of your .NET Core project.
There should be something like this
<ItemGroup>
<Reference Include="X" HintPath="..\bin\Debug\netcoreapp2.1\X.dll" />
</ItemGroup>
You should specify a correct path to the X.dll.
Note that it is not the best practice to reference .NET Framework libraries from .NET Core application. You would have failures if you forgot to check that .NET Framework library is compatible with .NET Core runtime and run your application on Linux.
I have a solution with two host projects (one is a Web Host and the other is a Generic Host) and a class library project referenced by those two host projects.
In the Sdk attribute of the root tag <Project> of the *.csproj file, both host projects (web host and generic host) are using Microsoft.NET.Sdk.Web, but the class library project uses the Microsoft.NET.Sdk.
The two host projects references the Microsoft.AspNetCore.App metapackage.
The class library project are using Microsoft.NETCore.App, but it references some ASP.NET Core packages individually (packages of Microsoft.AspNetCore.App that are not in Microsoft.NETCore.App).
About the correct SDK and metapackages:
1) In the generic host project, should I use pure .NET Core (Microsoft.NET.Sdk and Microsoft.NETCore.App) instead of ASP.NET Core (Microsoft.NET.Sdk.Web and Microsoft.AspNetCore.App) since it is not a web project?
2) In the class library project, is it fine to use Microsoft.NET.Sdk with Microsoft.AspNetCore.App to avoid the possibility to reference different versions of packages that belong to Microsoft.AspNetCore.App (to avoid for example, Microsoft.AspNetCore.App#2.1.0 in the host project and Microsoft.Extensions.Configuration#2.0.0 in the class library project)? Or I can only use Microsoft.AspNetCore.App metapackage with Microsoft.NET.Sdk.Web SDK?
3) What difference does it make to use Microsoft.NET.Sdk or Microsoft.NET.Sdk.Web? The docs says that "The SDK, as the layering document describes, is a set of MSBuild tasks and targets that can build .NET Core code.", but why do we need to have both of them? In practice, what Microsoft.NET.Sdk.Web does that Microsoft.NET.Sdk doesn't?
Ad (1) and (3): What are the differences between the "core" and and web SDKs, how do these affect generic host apps?
The most important differences are:
Default Items
The web SDK has different definitions and globbing patterns for which files to include in your published application.
E.g. when you have an appsettings.json file, projects using the web sdk will automatically include it since there are patterns in place that ensure that .config, .json files and all files in a wwwroot folder are all part of the publish output. See the MSBuild source code on GitHub for these patterns.
If you have a generic host and don't use the Web SDK, you may need to add code to the csproj file to specify which files to copy to the publish directory (or use an IDE to change the "copy to output directory" setting which also includes files in the publish output but will also copy them to the build output):
<ItemGroup>
<None Update="*.json" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
Web Publish logic
Another essential part of the Web SDK is that it contains the deployment logic for web applications.
If you plan to use publish profiles (.pubxml files) or deploy to azure or filesystems using MSBuild / MSDeploy, you will need this publishing logic.
Ad (2): Which SDK to use for class libraries?
For maximum compatibility when publishing public libraries (e.g. via NuGet), use the core SDK and reference individual packages with the lowest possible version - e.g. 2.1.0 / 2.1.1.
If you develop a class library containing razor views, you will need to use the Microsoft.NET.Sdk.Razor SDK to get razor tooling (e.g. when you use the dotnet new razorclasslib template).
For libraries and test projects where you want to use the same meta package reference as the application, things are a bit complicated at the moment but it's going to get better:
For ASP.NET Core 2.1 tools(!) (CLI 2.1.*), I suggest using the non-web SDK for class libraries and use the version 2.1.1 of that package. Don't ever upgrade it, even if NuGet offers you an upgrade.
For test projects in 2.1 tools(!) (CLI 2.1.*), it is a bit different and tricky, see Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime
Beginning in 2.2 tools (CLI 2.2.100+), the version-less package references to ASP.NET Core metapackages are moved to the core SDK so you can develop libraries and test projects for both ASP.NET Core 2.1 and 2.2 using the ""core"" SDK (provided you use tools 2.2.100+) using version-less package references:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
In .NET Core / ASP.NET Core 3.0, you will be able to reference the framework via a new mechanism altogether (no web-SDK needed):
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
I've just created a fresh project using dotnet new web. My Google-foo may be failing me, but I didn't find anything relating to my answer (please link to another SO answer, or relevant documentation if I've missed something obvious).
If I want to ensure this new project is .NET Standard 2.0 compliant, what do I now do?
It is not inherently possible to run a netstandard project as an executable. Since netstandard was designed to be used for libraries.
In order to develop your web application entirely in netstandard2.0, you would have to create a separate project that targets either .NET Core or .NET Framework to execute your library that contains your web app (developed using .NET Standard).
1. Executable Project (ex: console app)
-- Target Framework: netcoreapp2.0 / net462
2. Web Application Project (library)
-- Target Framework: netstandard2.0
You can use the following steps to change the target framework of your project.
Step 1. Target the desired framework
Right-click on your project and select Edit *****.csproj
In the .csproj file, you need to replace the target framework to the .NET Framework.
Example .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web"> //<-- note the .Web for the web template
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
For a list of the Target Framework Moniker (TFM) (ie, net47, netstandard2.0, netcoreapp2.0, etc.*) you can check this link out:
https://learn.microsoft.com/en-us/dotnet/standard/frameworks
Step 2. Run dotnet restore
Go to your output window and run dotnet restore.
Note: Sometimes Visual Studio may misbehave (depending on which update you have installed), so you may have to close and re-open your Visual Studio. Otherwise, sometimes a clean/re-build may do the trick.
Targeting both frameworks
You can pick one or the other, or even target both frameworks.
<TargetFrameworks>netcoreapp2.0; net47</TargetFrameworks> //<-- note the plural form!
NET Standard is for class libraries. Applications must target netcoreapp* where * is a version number. The following shows the compatibility matrix: https://learn.microsoft.com/en-us/dotnet/standard/net-standard
For example, .NET Core 2 can consume .NET Standard version 2 and below.
I have a WPF application under the .Net framework 4.5 and a web app under the DNX Core.
I want to share some code between those two apps.
I tried to create a "portable classe library" ... with no result.
I tried to do it with a nuget package ... with no result (and too complex to dispatch modifications)
Thanks for your propositions
Normally when creating a portable class Library, you can be compatible with .NET 4.5 and Dnx project. The only problem is that .Net 4.5 relies on csproj when your Dnx project relies on project.json.
Thus you won't have it out of the box, check out your options here: What are my options for sharing code between DNX / ASP.NET 5 projects (project.json / xproj) and other C# projects (csproj) within a single solution?
Note that this solution will probably not work in the next version of dotnet core if they finally remove the project.json...