I need to use Microsoft.Graph and Microsoft.Graph.Beta Nuget packages in paralell and referenced them like this in my csproj-File:
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraphBeta" Version="1.25.10" Aliases="MSGraphBeta"/>
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.25.10"/>
Within my existing code, only the Graph SDK should be used, so I set no alias there. This means I import it with:
using Microsoft.Graph;
Never the less, I still get the following error:
The type 'GraphServiceClient' exists in both 'Microsoft.Graph.Beta, Version=4.50.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'Microsoft.Graph, Version=4.34.0.0,
I'm working with asp.net core, targetFramework is net7.0.
What am I missing to use both SDKs in parallel?
Related
I have the dreaded 'Could not load file or assembly' and this time I ran out of options to fix it, hope you are smarter.
My (simplified) solution is this:
Winforms.exe, Framework 4.8. This references:
Client.dll, .Net Standard 2.0 which uses
IdentityModel.OidcClient 5.0.0 which uses
System.Text.Json
Since Client.dll is not .Net Core 5.0, I added System.Text.Json as a NuGet package. This leads to the following exception when calling RefreshTokenAsync on OidcClient:
System.IO.FileLoadException: 'Could not load file or assembly 'System.Text.Json, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
I tried a couple of things but none have worked so far:
Add the closest version to 5.0.0.2 of System.Text.Json I could find (5.0.0, 5.0.1) to Client.dll, issue remains.
Add the latest version to 5.0.0.2 of System.Text.Json I could find (6.0.1) to Client.dll, issue remains.
Add a package redirect to Winforms.exe (even though Generate Auto Redirects = true):
<dependentAssembly>
<assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="5.0.0.2" newVersion="6.0.1"/>
</dependentAssembly>
No dice
Tried adding the NuGet (all different versions) to WinForms.exe, with or without redirect. Issue remains.
I'm not sure how to fix this anymore, any takers? Thanks so much in advance.
I did something similar with EF Core and EF6. Inside cproj add a condition:
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<PackageReference Include="System.Text.Json" Version="5.0.0"></PackageReference>
</ItemGroup>
Then use preprocessor directives to load the assembly:
#if NET48
using System.Text.Json;
#endif
is there a way to avoid nuget package references to ease development on a developers machine?
we are currently about to move some of our projects in our "shared" solution to the new csproj file structure (using <Project Sdk="Microsoft.NET.Sdk">) and using <TargetFramework>netstandard2.0</TargetFramework>.
By doing so we had to include <PackageReference Include="System.Text.Encodings.Web" Version="4.7.0" /> and changing some of our code in project "S" of our shared solution.
Within another solution backend we are having multiple projects. Some of them are referencing the assembly of project S by using assembly reference to S.dll like so:
<Reference Include="ournamespace.S">
<HintPath>..\..\artifacts\Shared\ournamespace.S\ournamespace.S.dll</HintPath>
</Reference>
When we build every works fine. However when running our web application W from backend solution we get this exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Encodings.Web, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
Before we have migrated our shared projects to the "new SDK" project file format, before using .netstandard2.0 (we were using .net framework 4.7.2) and before we switched the package reference to System.Text.Encodings.Web (we were using <PackageReference Include="AntiXSS" Version="4.3.0" />) we have not received any error.
The only way i can think of is that we would need to switch from assembly reference for S.dll to use nuget - to get S dependencies.
However using nuget packages for our shared solutions projects and developing in backend projects would become a nightmare as we would need to create a new nuget package (and publishing it) on every change of S and would need to increase the version number in the package references in backend projects all the time. Also this would become very impractical as our developers are using various git feature branches too (thinking about versioning conflicts; having to release unfinished packages and maybe using version suffix "alpha_"+{branchName} to distinct the branch that this version is coming from).
How to develop on localhost? Is there a way to avoid nuget (but getting its dependencies resolved correctly!)?
I was already reading about having assembly references for local development while using nuget package references for CI builds by using conditionals in the csproj file (however this is also not working very well with VS2017; also this would not resolve our problem with the dependency problem written above on localhost)
What other possibilities are there? Is there a best way on how to handle this?
Thanks in advance!
P.S. I dont want to include S's dependencies to every project that references S by using package references there as well. This would not be a solution and becomes more cumbersome when S might get new dependencies for whatever reason.
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
solved my problems (see How to get .NET Core projects to copy NuGet references to build output? for details)
I've just created 2 projects with Visual Studio 2019 (16.3.3):
class library (netstandard2.0)
+Microsoft.EntityFrameworkCore, Version 2.2.4
+custom Repository class to hide DbContext class
WPF project (v4.7.1)
+reference to the class library above
+some code to use the Repository:
var r = new Repository(#"Server=(localdb)\MsSqlLocalDB; Database=EfCore2Wpf; Trusted_Connection=True;");
DataContext = r.GetItems(); // FileNotFoundException here.
Here's the complete exception:
System.IO.FileNotFoundException: 'Could not load file or assembly "Microsoft.EntityFrameworkCore, Version=2.2.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" or one of its dependencies. The specified module could not be found.'
That's right! There's no Microsoft.EntityFrameworkCore.dll inside \WpfApp\bin\Debug folder.
But why?
(I have some legacy solutions with the same project types and they work. What's wrong here?)
For your information:
1) Including a single .NET Core package like Microsoft.EntityFrameworkCore.SqlServer cause a Million <Reference Include="..."><HintPath>..\packages\...dll</HintPath></Reference> entries. That's no option.
2) As far as I remember I had to migrate my legacy WPF projects to pass indirect .NET Core references: https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference. But currently I don't need any NuGet package in my WPF project so there's no package.config. And without a package.config file I cannot migrate!
It's a bug and not fixed because nobody cares: https://developercommunity.visualstudio.com/content/problem/769172/a-full-net-project-is-not-in-packagereference-form.html
Workaround:
Add an arbitrary NuGet Package (e.g. NLog, AutoMapper, MvvmLightLibs or whatever).
Right-click on the new packages.config file → "Migrate packages.config to PackageReference..." (you might not need this step if "PackageReference" is your "Default package management format", check Tools → NuGet Package Manager)
Works!
Or even faster: Add to your *.csproj
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.7" />
</ItemGroup>
Warning: Use tool support like ReSharper's "Optimize References..." with caution! It would remove the unused reference. :-(
I have custom publish process which firstly merges some assemblies into one via ILRepack, then performs some other steps and finally cleans up publish directory - removes merged dependencies from APP_NAME.deps.json, relevant assemblies and symbol files.
In order to implement the last step, I've created a NuGet package with a custom MsBuild task.
According to Nate's blog post, I've set PrivateAssets="All" in order to ship all task's dependencies within the package:
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.9.20" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Update="#(PackageReference)" PrivateAssets="All" />
</ItemGroup>
Package layout looks like:
Suddenly, during publish, this step fails with error:
task failed unexpectedly. Could not load file or assembly
'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific
file. (0x80131621)
I can't understand why task tries to load version 12.0.0.0 while I have Newtonsoft.Json 12.0.2 (as specified in PackageReference).
Thank you for any help!
Updated:
According to this msbuild spec currently MSBuild tasks have serious limitations:
Tasks in MSBuild are dynamically loaded assemblies with potentially separate and colliding dependency trees. Currently MSBuild on .NET Core has no isolation between tasks and as such only one version of any given assembly can be loaded. Prime example of this is Newtonsoft.Json which has multiple versions, but all the tasks must agree on it to work.
As Lex Li mentioned in the comment, 12.0.2 is the NuGet package version which corresponds to the 12.0.0.0 assembly version. So the system attempts to load the right version of the assembly.
According to task-isolation-and-dependencies.md, related issue etc. custom MsBuild tasks have serious limitations.
MsBuild itself includes a dependency on Newtonsoft.Json 11.0.1, so custom tasks can't load any other version of this dependency.
Workarounds:
Use the same dependency version as MsBuild. I guess this approach is fragile and should not be used.
Create and package console application instead of a custom MsBuild task. I've chosen this approach because it is easily extensible and allows us to use any dependency version. Nate's blog post gives an overview of the approach.
As Martin Ullrich mentioned in the comment, we could use task with isolation boundaries. ContextAwareTask.cs demonstrates the approach.
I am creating a c# .net framework library (.dll) that will act as a plugin for another piece of software. I want to use the Newtonsoft.json library but when the I call the json library I get the classic "FileNotFoundException":
Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
The .dll is definitely being copied to the directory I am trying to run the plugin from. A lot of others have had this issue and I have tried everything I've seen (updating, reinstalling, etc.) but no luck yet. Because this is a class library I do not have an "app.config" file to play with binding redirects, so I haven't been able to try that.
I have very basic code right now, this package is the only external reference I am using. I've only been working on this today, only ever trying with Newtonsoft.Json version 12.0.2. It is weird to me that it appears VS is looking for 12.0.0.0, however. This is not an option to download. Similarly, I've tried downgrading to version 11.0.2, and then it gives me the same error saying it can't find version 11.0.0.0.
Even though your project may not have an .app config, you can still view the package references by unloading your project and viewing the .csproj file.
To try and force a direct reference to Newtonsoft.Json 12.0.2, you can add this snippet
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
</PackageReference>
</ItemGroup>
I would recommend removing any other references in your .csproj for Newtonsoft.Json as well