-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I force myProject Choose .NetStandard assembly instead of TargetFramework #1791
Comments
Can you give us some more information here? What does your project look like? Could you share that? It will pick the appropriate API surface based on your Target Framework. |
@livarcocc That's the problem. <TargetFramework>net45;netstandard2.0</TargetFramework>
<PackageReference Condition="'$(TargetFramework)' == 'NetStandard2.0'" Include="Microsoft.AspNetCore.Http" />
<Reference Condition="'$(TargetFramework)' != 'NetStandard'" Include="System.Web" /> and some code #if NETSTANDARD20
var context = IHttpContextAccessor.HttpContext;
#else
var context = System.Web.HttpContext.Current;
#end if
var q = context.Request["q"]; if I use that lib in an Asp.NET Core application ( .NetFramework Runtime ) , The App will Choose Net4.5's version. Is there any way to force my app to choose .net standard version ? |
From the conditions you have in your project above, it seems like the Reference will always be added. Because you are checking against NetStandard alone. |
@livarcocc I know it's bad, and I'll never use that pattern , but I can not controll other people to use that like this. I hope something like this : <TargetFramework>net461</TargetFramework>
...
<PackageReference Include="OneOfThePackage" Version="1.0.0" Choose="NetStandard2.0" /> Is there any attribute to do this ? |
You can condition it with something like this:
Is there a reason why something like this would not work for you? We use it like this our own projects. |
I've seen this before - assuming that You correctly assume that this does not account for ASP.NET Core on .NET Framework. |
That's the right way, but there is an available assembly(.net standard assembly) in that package!! and there are other condition may need this: many nuget package target That's why I hope there is a |
I do feel your pain using this. I don't work for microsoft or the .net foundation so I can only give hints to this. apart from the asp.net core/classic selection, I have also seen issues with different public APIs in NuGet packages: Are you having trouble using a public NuGet package? I could probably cook up custom hacky msbuild targets modifying your compile references as a last resort |
I have similar problem. I have a utilities project targeting .net standard which references a third party nuget. I have another exe project referencing the utilities project. The third party nuget has both .net framework and .net standard dll in it but they have been compiled with different versions of Unity nuget (Unity made a breaking change across version 4 and 5 by changing the dll names). The .net framework version of the dll in third party nuget references version 4 of Unity (Microsoft.Practices.Unity.dll) and .net standard version of the dll in the third party nuget references version 5 of Unity (Unity.dll). The utilities project in my code is targetting .net standard and references the aforementioned nuget so it compiles against the .net standard version of the dll. The exe project which references the utilitites project above targets .net Fx and will pick the .net framework version of the dll from the nuget and place it next to the exe. At runtime the utilities project fails saying the Unity class/method not found. If there was a way to force the exe project to reference the .net standard version of the nuget then we would binplace the .net standard version of the dlls which would then just work. |
I Have the same problem here, I Have a net472 app that requires to be full framework, and I consume a nuget (netStandard2.0), this nuget have a reference for another nuget that provides both net45 and netstandard2.0. This is definitely PackageReference bug or removed feature (that should have being disclaimed). |
Same problem here: We reference NUnit via PackageReference from a We need to use the |
Same issue : NuGet/Home#7385 |
suggest to add 2 attributes on
|
+1 Working inheritance structure:
I would be looking to 'weight' the selection of the netstandard2.0 assembly over the net45 one when net47 is selected as the TargetFramework and the reference .nupkg contains both targets. I would think the solution would present something like this: <ItemGroup>
<PackageReference Include="My.MultiTarget.Package" TargetFramework="netstandard2.0" Version="1.0.0" />
</ItemGroup> In the meantime, there is a path to working around it in certain architectures via a second project that targets netstandard2.0 ref'd from the first that targets net47 with CopyLocalLockFileAssemblies set to true: <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup> |
Doesn't work in my case with a root project targeting net461. It still detect package version outside of dependency constraint. |
+1 We are experiencing this problem as well. We would like to make the netstandard 2.0 version of our libraries the "preferred" version and suggest all customers to use them in place of the legacy platform specific versions that are available in the same nuget package. But customers using the PackageReference project format don't have a way to specify that they want to use the netstandard 2.0 version. We are also experiencing the issue described by @bigbangtheorem: a framework 4.5 application using a netstandard 2.0 class library that uses our multigargeted nuget package (both framework 4.5 and netstandard version are available in the package) will restore the framework 4.5 version of the multitargeted library. @livarcocc I can provide you with more information and sample projects if you are working on this. |
Thank you @bigbangtheorem for the workaround. |
In our case, we want to share a bunch of base libraries between .NET Core 2.2 and .NET 4.7.2 projects. The result is an exception at runtime as the following happens: We need a way to force netstandard for the referenced NuGet. |
+1 for consider transitive dependencies, but want it to be smarter when choose between some runtime-library like Have an similar issues for use any(No build-time warns, so it's hard to see whether it's affacted or not) library which target only ns2.0 and depend an multi-target lib with different dependencies for different target, and then use it in net472 program. Dapper<-System.Data.SqlClient
Wanted:
And see no build-time error, but Tried set
|
I just had the same problem. Unfortunately @bigbangtheorem's workaround did not work for me for some reason, but this one by @duanenewman did. Maybe it helps someone else. |
I concur that this is a feature that is needed because you end up having to fight MSBuild/NuGet and the default way of selecting packages when you are solving more esoteric problems. For example, we're consuming an open source package that has It's exceptionally frustrating to have no control over this as well as NET changing its mind in each subsequent layer of the build. We have:
So to make matters worse, we test the first project and everything looks fine. I'm sure this behaviour is correct and normal for the vast majority of cases, but once tracked down, actually fixing it should be easier than the workarounds listed. (Though I'd be intrigued to understand why I notice this also got made a NuGet feature request over in NuGet/Home#7416 and am curious as to which component actually needs the feature adding. It feels like the problem is the selection by |
Thanks @Skleni, glad that has helped you. I have a followup post that removes breakage due to version # updates here It would be great to be able to specify a
|
+1 for adding some feature to resolve this. Today I have spend 1/2 day looking for workarounds. So far no luck. I would like to make all our binaries to use netstandard2.0 version of a dependent library. ATM those binaries compiled for netcoreap3.1 target just do not work, because those compiled for net472 bring in the dependent library for net472. While all binaries happily work with netstanderd2.0 version of the dependent library. |
+1 |
For clarification and to hopefully help others: The work-around for this issue (.NET Framework application picking the net45 or net47 version of a lib over the netstandard20 version from the same NuGet package) is to change the From: Only downside is you have to do this after every package upgrade. |
@brinkie2004
if you use the |
I had this same issue and solved it with the article mentioned by @Skleni in his comment above (#1791 (comment)). In our case System.Configuration.ConfigurationManager.6.0.1 package contains a different assembly version for net461 (6.0.0.1) than for netstandard2.0 (6.0.0.0) which caused build warnings. I solved it with below xml snippet: <PackageReference Include="System.Configuration.ConfigurationManager" GeneratePathProperty="true" ExcludeAssets="Compile;Runtime;Build" />
<Reference Include="System.Configuration.ConfigurationManager">
<HintPath>$(PkgSystem_Configuration_ConfigurationManager)\lib\netstandard2.0\System.Configuration.ConfigurationManager.dll</HintPath>
</Reference> It's slightly optimized in comparison with the snippet mentioned in the article by generating a path property that can then be used in the reference. Also more assets were excluded, as only excluding Compile did not work for my case. |
[main] Update dependencies from dotnet/arcade
Today , There are Asp.Net (
System.Web.HttpContext
) and Asp.Net Core (Microsoft.AspNetCore.Http.HttpContext
) in .net Framework , and there are a few packages use .net Framwork withSystem.Web.HttpContext
and .net Core (.net standard) withMicrosoft.AspNetCore.Http.HttpContext
for sharing source , so when I use Asp.Net Core on .Net Framework , I just can not use that package because that package fouce me to useSystem.Web.HttpContext
, but there is an available assembly in that package, How do I choose that ?The text was updated successfully, but these errors were encountered: