Some assembly binding redirects are not generated automatically even if AutoGenerateBindingRedirects is set to true - nuget

I have a simple net48 project with some nuget package references. However, some assembly binding redirects are not generated, even if AutoGenerateBindingRedirects and GenerateBindingRedirectsOutputType is set to true!
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
...
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAPICodePack.Core" Version="1.1.0" />
<PackageReference Include="Microsoft.WindowsAPICodePack.Shell" Version="1.1.0" />
<PackageReference Include="ZetaLongPaths" Version="1.0.10.41" />
<PackageReference Include="OxyPlot.Core" Version="2.0.0" />
<PackageReference Include="OxyPlot.WindowsForms" Version="2.0.0" />
...
</ItemGroup>
</Project>
For some of our package references the bindings are generated automatically, however for the 5 I have mentioned above no redirect is added!
Since we are using renovate-bot to update our nuget packages, this will break the automation since we need to add these 5 bindings manually which requires adaption once the version has been changed!
Does anybody know how to get those bindings generated automatically?

Related

Could not load type Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsExtensions from assembly Npgsql.EntityFrameworkCore.PostgreSQL

I have 2 EF Core projects A and B
Project A has .net6.0 target and corresponding packages
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
Project B has .netcoreapp3.1 target and corresponding packages
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.4" />
While referencing B to A everything builds OK, but on resolving context from B inside of the project A I am getting the following exception:
System.TypeLoadException
Could not load type 'Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsExtensions' from assembly 'Npgsql.EntityFrameworkCore.PostgreSQL, Version=6.0.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
Why B project's context can't use it's own dependancies from lower version of Postrges package? Is it possible to use it like that, because I can't upgrade the project B and I don't want to downgrade project A
So the avalaible (exept some exotic non-supported workarounds) options I found are :
Multi targeting project B with conditional package reference. For example
Multiple target framework project: different versions of the same NuGet package on different frameworks?
In my case it's
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6"/> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.22"/> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.22"/> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.4"/> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11"/> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1"/> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0"/> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7"/> </ItemGroup>
Proxy a data request to the context through web api or message queue (depending on type of interactions you do with the context data) as the result breaking the direct reference.

.NET MAUI Integration Testing - FileNotFoundException: Could not load file or assembly Microsoft.Maui.Essentials

I'm attempting to integration test a .NET MAUI application utilizing entity framework core.
When the code gets to the line 'this.Database.EnsureCreated()', it gives a file not found exception.
The first thought was trying to reference Microsoft.Maui.Essentials, though when I search for it in NuGet, all I can find is Microsoft.Maui.Essentials.Ref.___ which doesn't seem compatible with my project.
If I try to install it, I get the error 'The package Microsoft.Maui.Essentials.Ref.any 6.0.547 has a package type DotnetPlatform that is incompatible with this project'
How can I resolve this error?
The full length exception is:
System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Maui.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
Integration test code:
...
var options = new DbContextOptionsBuilder<DataContext>()
.UseInMemoryDatabase(databaseName: "Test_Database")
.Options;
var mockDataContext = new Mock<DataContext>(options);
mockDataContextCreator
.Setup(x => x.CreateDbContext())
.Returns(new DataContext(options));
...
DataContext.cs
public class DataContext : DbContext
...
public DataContext(DbContextOptions options)
: base(options)
{
SQLitePCL.Batteries_V2.Init();
if (!this.Database.EnsureCreated())
{
this.Database.Migrate();
}
}
The Database property is of type 'Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade' and is defined in the base class 'DbContext'.
Integration Test CSPROJ File:
...
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />
<PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.547" />
<PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.547" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
...
The fix is surprisingly easy. Add UseMauiEssentials to your test project file. Alternatively you can utilize UseMaui, the difference is explained here.
At least this worked for me out-of-the-box within almost the same setup (xUnit). Took me few hours to find this after number of more complicated but not robust enough attempts.
One of my failing attempts was to add "Microsoft.Maui.Dependencies" NuGet package. I can see it in your original post. With the fix suggested this NuGet package is not needed.
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseMauiEssentials>true</UseMauiEssentials>
<IsPackable>false</IsPackable>
</PropertyGroup>

Get rid of stylecop.json from nuget package

I've setup building nuget package from my project using package tab in project options. Everything seems to work except that my nuget package contains stylecop.json file. Build action for this file is set to "C# analyzer additional file". How to get rid of this file from nuget package?
This is my csproj file content
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl>https://github.com/aixasz/ImageShareTemplate/blob/develop/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/aixasz/ImageShareTemplate/</PackageProjectUrl>
<Description>ImageShareTemplate is image template library to share an image to social like facebook, twitter, etc.</Description>
<RepositoryUrl>https://github.com/aixasz/ImageShareTemplate/</RepositoryUrl>
<PackageTags>image socialmedia</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0001" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0001" />
</ItemGroup>
<ItemGroup>
<Folder Include="Fonts\" />
</ItemGroup>
</Project>

Not able see test cases in Text Explorer for the .net core 2 based Test project

I have created a test project based on .Net Core 2 and wrote some NUnit test cases. After installing necessary NuGet packages i.e. NUnit3TestAdapter, I was able to see all test cases in "Test Explorer" and able to execute those. Now, when I looked into the project directory, I found that it's creating "obj" folder and some json files in it. So I tried to change the path of "obj" folder by modifying ".csproj" file. I provided some different path in the parameter "BaseIntermediateOutputPath" and that way, I was able to get rid of "obj" folder. The reason for providing different path was, I wanted to keep json files separate from source code.
However, after modifying that I am not able to see or execute any test cases from Test Explorer.
Is this a Microsoft bug?
Is any packages having dependency on "obj" folder?
P.S.
I am using "NUnit" and "NSubstitute" packages for my test project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputPath>..\..\build\$(Configuration)\UnitTests\</OutputPath>
<BaseIntermediateOutputPath>..\..\work\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
</ItemGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
<ProjectReference Include="..\UtilityLibrary\UtilityLibrary.csproj" />
</ItemGroup>
</Project>
When .NET Core projects build, they do not copy all referenced files into the bin folder. When you add Microsoft.NET.Test.Sdk to your test project, one of the things it does is add an AssemblyResolve event handler which loads other dependent assemblies from a list of searchDirectories.
BaseIntermediateOutputPath not working was reported against the VSTest project and is an issue with MSBuild. The workaround is noted in the dotnet sdk repository. From that, you need to use Sdk imports in your csproj instead of the Sdk attribute on the Project element.
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>obj\XXX\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<!-- Body of project -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

netcoreapp1.0 not compatible with net45

3 hours of googling and I still can't find an answer
I tried this also: Package [some package] is not compatible with netcoreapp1.0
Still doesn't work, I even added net45 and net40 reference on the imports. Another is problem is my mvc core project doesnt have a package.json
Because you are probably using VS2017, you won't have a project.json since it doesn't exist any longer. So, doing the following will get you to hopefully a working project.
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNet.SignalR.Core" Version="2.2.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>
right click your project file edit .csproj, then remove and replace the <propertygroup> </propertygroup> and it's contents. Should work now to reference signalr packages. The unfortunate fun is you have to create your own middleware to tie it into the core framework..
https://www.codeproject.com/Articles/1115941/ASP-NET-Core-Building-a-Real-Time-Online-Poll-Syst
I did create the middleware really quick and it does compile. Whether it works I hadn't gotten that far. Keep in mind that link references the really old stuff before VS2017 and .csproj files for the projects.
hope that helps.