How to link UWP Class Library into an WPF Application?
I have created an UWP Class Library with a single test class. I have an WPF .NET application which wants to consume that class library. What are the steps I need to follow?
Following this tutorial, I wanted to add library MyLib in my Application MyApp. But I am finding following compiler errors,
Severity Code Description Project File Line Suppression State
Error NU1201 Project MyLib is not compatible with netcoreapp3.1
(.NETCoreApp,Version=v3.1). Project MyLib supports:
uap10.0.19041 (UAP,Version=v10.0.19041) MyApp C:\Users....\MyApp.csproj
##UPDATE
After adding following code,
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<AssetTargetFallback>uap10.0.19041</AssetTargetFallback>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MyLib\MyLib.csproj" />
</ItemGroup>
</Project>
as suggested by TamBui in the answer, I am getting build error. However there have been two compiler warnings from the beginning. Sharing if it can give any clue,
Warning NETSDK1137 It is no longer necessary to use the
Microsoft.NET.Sdk.WindowsDesktop SDK. Consider changing the Sdk
attribute of the root Project element to
'Microsoft.NET.Sdk'. MyApp C:\Program Files\dotnet\sdk\5.0.103\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 376
Warning MSB3270 There was a mismatch between the processor
architecture of the project being built "MSIL" and the processor
architecture of the reference
"C:\Users...\MyLib\bin\x64\Debug\MyLib.dll",
"AMD64". This mismatch may cause runtime failures. Please consider
changing the targeted processor architecture of your project through
the Configuration Manager so as to align the processor architectures
between your project and references, or take a dependency on
references with a processor architecture that matches the targeted
processor architecture of your project. MyApp C:\Program Files
(x86)\Microsoft Visual
Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2123
A WPF application cannot reference a UWP class library. In short, the two different platforms or frameworks are not compatible with each other.
You should change the target framework of your class library (MyLib.csproj) to either .NET Standard or the same framework that your WPF app targets:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
You need to add the AssetTargetFallback that matches your UWP project's target version to your WPF project's PropertyGroup. Select your WPF project in the Solution Explorer, and you will be able to edit the project's properties.
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_68824006</RootNamespace>
<AssetTargetFallback>uap10.0.19041</AssetTargetFallback>
<UseWPF>true</UseWPF>
</PropertyGroup>
Related
I have a library that is shared between .NET Core 3.1 and .NET Framework 4.7.2. I have found that when I run the .NET Core 3.1 Azure Function I can debug the library code, but when running the MVC app I cannot. Also, if I try to use intellisense to navigate to definitions within the library it will not take me to the code, but to the meta-data.
I've added a "full" debug type specifier to the top of the Library's csproj file like this and that does not help:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<DebugType>full</DebugType>
</PropertyGroup>
I have other similar libraries that do not have this issue and have tried copying over some config attributes, but nothing seems to help. How might this be fixed?
I am using the latest version of VS 2019 and I have noticed that the PDB file size has increased since making the DebugType full. If I try to load the symbols manually using the Modules window it tells me a debug file of the correct type cannot be found.
It turns out I had to sign the assembly or give it a guid for my Framework app to navigate into it, or debug it. The csproj looks like this now at the top:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{<some guid>}</ProjectGuid>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<DebugType>full</DebugType>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile><some snk file name>.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
The old options to have VS generate the strong signing stuff for you can still be accessed in VS by right-clicking on the project and selecting Properties.
I have a solution with several executables in it (say, MainApp.exe and Tool.exe).
The main goal is to ensure that the tool (Tool.exe) with its dependencies is copied to the main executable directory during build.
I used the advice from here, and it seemed to work with the older Visual Studio version (at least with some version prior to 16.8).
My project structure (simplified) looks like this:
Solution.sln
├ MainApp.csproj
├ Tool.csproj
| └ App.config
└ ToolLib.csproj
Tool project contains App.config file, and references ToolLib project.
My MainApp.csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Tool/Tool.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Content</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Targets>Build;DebugSymbolsProjectOutputGroup</Targets>
</ProjectReference>
</ItemGroup>
</Project>
After upgrading to 16.8 after the compilation the file Tool.exe was indeed copied to the output directory, but neither its dependency ToolLib.dll nor Tool.config was copied to the output directory any more.
Is this a bug or intended behaviour? What is the proper way to ensure that the whole Tool with all the needed dependencies is copied to the MainApp's output dir?
Added test project reproducing the problem here: https://github.com/vladd/ReferenceOutputAssembly
What you gave is too old and it is not suitable for VS2019. And all your projects target to net core 3.1. I have tested your project both in VS2019 16.8 , VS2019 16.7, even 16.6 which all act the same behavior as you described. Only contain the Tool.dll and Tool.exe.
So I wonder why you said before that the result of the build of ToolLib will be printed in the main project.
Actually, <ReferenceOutputAssembly>false</ReferenceOutputAssembly> will prevent the most main output files of the referenced project and its dependency project being copied into the main project.
Suggestion
You have to set it as true:
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
If you want to not copy ToolLib.pdb and Tool.pdb files into the main project, you could add these node on MainApp.csproj file:
<PropertyGroup>
<AllowedReferenceRelatedFileExtensions>*.pdb;.dll.config</AllowedReferenceRelatedFileExtensions>
</PropertyGroup>
If you also want to copy pdb files, you should add .pdb under AllowedReferenceRelatedFileExtensions.
<AllowedReferenceRelatedFileExtensions>.pdb;.dll.config</AllowedReferenceRelatedFileExtensions>
Update 1
I tried your suggestion but with it the files Tools.deps,json and
Tool.runtimeconfig.json are not copied, so running the tool fails.
Add this on MainApp.csproj file:
<PropertyGroup>
<AllowedReferenceRelatedFileExtensions>.pdb;.dll.config;.runtimeconfig.dev.json;.runtimeconfig.json</AllowedReferenceRelatedFileExtensions>
</PropertyGroup>
I'm trying out WPF .NET Core for the first time, and one of the things I always do in a new WPF .NET Framework project is turn on the console, but I receive an error when I try to do this in .NET Core.
In traditional WPF, targeting .NET Framework, this was fairly simple;
Set the Build Action for App.xaml to Page
Define a Main method somewhere, and tag it with the STAThreadAttribute
In the project settings, set the output type to Console Application, and set the Startup object to wherever you put the Main method
I replicated these steps in the WPF .NET Core, but I get an error when I try to change the Build Action for App.xaml
The error (it occurs immediately after selecting Page in the dropdown in the Properties window):
An error has occurred while saving the edited properties listed below:
Build Action
One or more values are invalid.
Cannot add 'App.xaml' to the project, because the path is explicitly excluded from the project
(C:\Program Files\dotnet\sdk\3.0.100\Sdks\Microsoft.NET.Sdk.WindowsDesktop\targets\Microsoft.NET.Sdk.WindowsDesktop.targets (45,5)).
Does anyone know a way around this, or another way to enable the console in WPF .NET Core?
Setting the following properties will make your WPF application a "Console Application"
<PropertyGroup>
<OutputType>Exe</OutputType>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>
The SDK automatically change OutputType from Exe to WinExe for WPF and WinForms apps. See https://learn.microsoft.com/en-us/dotnet/core/compatibility/windows-forms/5.0/automatically-infer-winexe-output-type
After some trial and error, I figured out that the .csproj file got messed up. I don't know how exactly it went wrong, I suspect it had to do with editing the project through its Properties window in that version of VS2019.
I edited the .csproj by hand, and it works if it looks as follows:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<ApplicationIcon />
<StartupObject>ProjectName.App</StartupObject>
</PropertyGroup>
<ItemGroup>
<Page Include="App.xaml"></Page>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Remove="App.xaml"></ApplicationDefinition> <--- key part
</ItemGroup>
</Project>
I checked the .csproj file for a working .NET Framework project, and the key seems to be removing App.xaml as ApplicationDefinition by hand, as the .NET Framework .csproj did not have that section in it.
I am working on an ASP.Net core 2.1 web app project. I have 1 project in my solution and 3 other libraries, it's and advanced architecture (data access layer (DAL), business layer (BL), common layer (CL)), so i need to add references to connect some libraries and project. I have added CL reference to my project and to libraries DAL and BL. Now I have to add reference BL to my project, but when I add I get this type of error:
Version conflict detected fr Microsoft.EntityFrameworkCore/ Install/reference Microsoft.EntityFrameworkCore 2.2.1 directly to project 'WEB' to resolve this issue
When I am trying to install that version it says
Package restore failed. Rolling back package changes for 'WEB'
I can also mention that when I add reference BL to my project, it also includes DAL, and CL itself, (and DAL contains Microsoft.EntityFrameworkCore (2.2.1), can't understand what's the problem, any ideas?
Additional Images of the problem in here.
project.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CL\CL.csproj" />
</ItemGroup>
</Project>
i have added CL reference as you can see, now i want to add BL reference and get this error
The issue is because you're having local directory path to one of your {projectName}.csproj file
Kindly review your .csproj files by Right clicking project and select Edit {projectName}.scproj
e.g
<Reference Include="Microsoft.EntityFrameworkCore">
<HintPath>..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\Microsoft.EntityFrameworkCore\2.1.1\lib\netstandard2.0\Microsoft.EntityFrameworkCore.dll</HintPath>
</Reference>
If yes than go to Nuget Package Manager and add your library bu selecting nuget.org as Package Source
after restoring from nuget Package manager csproject file will have following tag Added
to
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.1" />
Here {projectName} is the name of C# project.
Just want to chip in that i had the same issue but with EntityFrameworkCore 3.0 preview. I solved it by simply downgrading all entityframework 3.0(preview) nuget packages to latest stable (2.2).
Hopefully this helps someone aswell, took me hours...
Option 1 check visual studio error window and identify which package is making the conflict .
make sure both packages have save version code
I'm writing a class library to abstract our company's Database functions, but when the code gets to instantiating one of my database objects we get a:
FileNotFound Exception:
Could not load file or assembly 'MySql.Data,
Version=8.0.13.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or
one of its dependencies. The system cannot find the file specified.
The MySql.Data Dependencies as stated on the website are:
.NETStandard 2.0 Google.Protobuf (>= 3.5.1)
System.Configuration.ConfigurationManager (>= 4.4.1)
System.Security.Permissions (>= 4.4.1)
System.Text.Encoding.CodePages (>= 4.4.0)
But all of them are installed automatically.
The NuGet package is MySql.Data (8.0.13) (which installs successfully)
Project is a .NET Standard 2.0 class library
There are no compile errors or even warnings; just the above error at run-time.
Have looked through Could not load file or assembly or one of its dependencies which advises checking where the dependencies are not being found - so I did - but it doesn't say how to fix the missing reference when you've found where it is?
Using Process Monitor I was able to find the failed CreateFile operation DLL calls, referencing ...\TestingGUI\bin\Debug\MySql.Data\MySql.Data.dll which, manually checking, is not there.
The project that runs is a WinForms app that references another .NET Standard class library (essentially a middleman) which then references the database library which depends on MySql.Data.
Doing a search in the whole solution directory, there are no MySql.Data.dll files, especially after a full solution build.
Here is my csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RootNamespace>App1.Database</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.13" />
</ItemGroup>
</Project>
So, who's a .NET Wizard?
UPDATE:
So it turns out the code works fine when run from a .Net Core console app, but only has the error when referenced from a .NET app (specifically winforms). So I've given up having a GUI for now and am just using a .NET Core console app. I thought .NET Standard was compatible with everything, but maybe not? Anyway, I will keep my question here for anyone else having troubles.
UPDATE 2:
Thanks to #Itay Podhajcer's answer we managed to get it working with .NET Winforms by also including the NuGet package there.
I remember encountering the same issue, I think it was related to the new nuget referencing model.
Try adding the missing nuget package directly to the WinForms project.Far from ideal solution, but it should work.
Hope it helps!
Visual Studio not copying dependencies | Could not load file or assembly
It seems the dependencies and assembly are not copied to the output folder. You can check the thread for some more details:
PackageReference is missing copy files to output feature
To resolve this issue, you can try to following workaround:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Hope this helps.