Identifying the version of Entity Framework - c#

One of our development machines is giving an error saying that DbContext does not implement IDisposable. According to a similar SO post, this is because we are not referencing EF 5. The problem assembly has a *.edmx file built with older EF 4 constructs (ObjectContext). This same assembly also has a newly added *.edmx file with the newer constructs (DbContext).
A different project on a different machine also uses DbContext and works fine. Said project shows the following information for EntityFramework.dll:
version: 4.4.0.0
Runtime Version: v4.0.30319
I checked the bad build machine and it also has this same version of EntityFramework.dll.
Microsoft has had confusing conflicts between development and marketing version naming conventions. So does EF 5 mean the dll version should say 5.0?
In other words, am I running v4.0 on the good build machine? If so why is the build successful?

The Entity Framework v5.0 dll is dependent on the .NET CLR you are targeting. If your project is set to use the .NET 4.0 framework, the runtime version of the EntityFramework.dll will be 4.4.0.0.
If you are using Entity Framework with a CLR target of .NET 4.5, the version of the EntityFramework.dll will be 5.0.
When you add EntityFramework via nuget, it will look at your project's CLR target runtime and add the appropriate EntityFramework.dll reference. If you later change your target, you should uninstall and reinstall the EntityFramework reference via nuget to make sure that the correct version of the runtime is referenced by your project.
Reference post by Julie Lerman about the topic.

Related

Extra files in the project (NPGSQL)

The question is stupid enough, but it didn’t work with NuGet before. After installing the NuGet package through the console, to connect to PostgreSQL, files appeared that NetFramework should contain. Without copying these files, the project does not start. What to do to get rid of them and not copy them to the directory with the program. In my understanding, these files should be taken from the framework.
See screenshots:
All need files in project C#
My guess is that you have .Net Standard 2.0 libraries/dependencies in your project.
.NET 4.6.1 might add additional runtime dependencies in your output folder:
.NET 4.6.1, 4.6.2, .NET 4.7 and 4.7.1 all have partial .NET Standard
2.0 support in the natively shipped runtimes, but they still are .NET Standard 2.0 compliant by adding additional runtime dependencies into
your output folder to provide the missing functionality. NuGet along
with the runtime targeting handles automatically adding those
dependencies to your projects to provide the needed runtime support
for those extra features. A lot of those assemblies override behavior
from the base framework and .NET uses runtime redirects to route api
calls to the appropriate assemblies rather than than mscorlib.dll or
other system assemblies.
.NET 4.7.2 is the first version of the full .NET Framework that is
fully .NET Standard 2.0 compliant without any additional dependencies.
See: https://weblog.west-wind.com/posts/2019/Feb/19/Using-NET-Standard-with-Full-Framework-NET
In other words, targeting .NET 4.7.2 should get rid of the additional files.

Does Windows cache the .NET version used by an assembly?

I made the mistake of trying to deploy a program built with .NET 4.7.1 on a machine that didn't have that version of the .NET framework installed. It popped the following error message:
Since installing a new .NET framework requires a reboot, I don't want to go that route yet. So I tried downgrading the target framework to 4.6.1... but I still get the same error!
I verified that the assembly was compiled targeting the 4.6.1 framework:
As a test, I renamed the assembly VueLinks2.exe and it started just fine.
The only thing I can think of is that Windows "remembers" what framework is associated with an assembly. Is that correct? Is it possible to undo that and downgrade an app to a previous version without renaming the file?
There's no cache. Deciding what framework to use is a combination of the <requiredRuntime> (legacy) and <supportedRuntime> elements in the [exe].app.config and the assembly's framework version if those are not supplied. Renaming the executable will cause the [exe].app.config to no longer be matched.

Compile time error when attempting to upgrade ServiceStack from 4.6.3 to 5.5.0 while Targeting .NET 4.7.2

I have a full framework .NET 4.7.2 (also tried 4.6.1) class library that references ServiceStack 4.5.8.0. When attempting to upgrade to ServiceStack 5.5.0, I get the following compile-time error:
The type 'IReturn<>' is defined in an assembly that is not referenced.
You must add a reference to assembly 'ServiceStack.Interfaces,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=e06fbc6124f57c43'.
and
Argument 1: cannot convert from 'MyRadNameSpace.MySweetClass' to
'ServiceStack.IReturnVoid
There is a related question, but the question revolves around using .NET Standard and .NET core, whereas this library targets the full framework.
How can I troubleshoot this issue?
The error suggests you still have a dependency that's referencing an incompatible ServiceStack.Interfaces build. This is typically due to having dependencies that reference both .NET Framework and .NET Standard builds of ServiceStack packages within the same project in which case you'll need to multi-target your .NET Standard projects.
It could also be due to a version mismatch. Note all dependencies should be referencing the same version version number for all ServiceStack packages, i.e. they all should be referencing the same 5.x version. You can also try deleting the /bin and /obj folders to force a clean restore and build.

'Could not load file or assembly 'netstandard, Version=2.0.0.0, ...'. Reference assemblies should not be loaded for execution

Goal:
From a .NET 4.7 console app, using reflection with Assembly.GetType(), I am trying extract the Type of a netstandard 2.0 class from Assembly X. Then I want to create an instance of this Type with Activator.CreateInstance().
What I am trying to do:
However, this assembly X has a dependency to netstandard 2.0. To be able to get the Type, netstandard dependency has to be loaded into the AppDomain. That's why when the AppDomain is requesting the netstandard assembly through the AssemblyResolve event, I simply load the dll like this :
var netStandardDllPath = #"C:\Users\xxx\.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll";
return Assembly.LoadFrom(netStandardDllPath);
Which throws:
System.BadImageFormatException: 'Could not load file or assembly
'file:///C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll'
or one of its dependencies. Reference assemblies should not be loaded
for execution. They can only be loaded in the Reflection-only loader
context. (Exception from HRESULT: 0x80131058)'
Inner Exception: BadImageFormatException: Cannot load a reference
assembly for execution.
What I know:
I know that they want us to load the DLL with Assembly.ReflectionOnlyLoadFrom. But doing that will prevent me from instanciate the type with Activator.CreateInstance(). See Microsoft official post
Also, I tried referencing the Nuget packages NETStandard.Library 2.0.0-preview1-25301-01 and NETStandard.Library.NETFramework 2.0.0-preview1-25305-02 in my console app so it would have the netstandard 2.0 libraries referenced, but it didn't change anything.
Question:
Does anyone would know if there is a proper way to load that dll without error, or maybe if this is a bug, or else? Or why this kind of dll is not able to load for execution?
The netstandard.dll you are trying to load is a reference assembly that which cannot be loaded for runtime on .NET Framework as pointed out by others. However if you need to resolve that dependency you will need to runtime version that maps to the framework you are trying to run on.
For .NET Standard support we are including them as part of the msbuild extensions that ship with VS so you will want to get the version of netstandard.dll from there. Depending on which version of VS2017 you have installed it should be somewhere like C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll or from the .NET Core 2.0 SDK you can find it C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll
Try using one of those versions in your scenario.
Wow. I just spent several hours tracking the cause of this "could not load ... netstandard" error down.
For me, the problem was that my .NET Framework project (which references both .NET Framework and .NET Standard libraries) was built with .NET Framework 4.7.2 and the system where I was deploying and running it did not have 4.7.2 installed.
Deploying a very small Console project with the same basic structure and references and executing that in a Command window finally revealed the correct error, in a pop-up, that .NET Framework 4.7.2 was missing.
If you're struggling with this particular error, make sure you have the necessary .NET Framework installed.
Set Copy Local to true in netstandard.dll properties.
Open Solution Explorer and right click on netstandard.dll.
Set Copy Local to true.
You can't load a reference assembly.
.NET Standard is a collection of APIs that must be provided by .NET Standard compatible implementations.
A reference assembly only contains contracts. This means that it contains no implementation. The assembly you are trying to load contains the .NET Standard 2.0 contracts.
A contract looks like this: https://github.com/dotnet/standard/blob/master/netstandard/ref/mscorlib.cs
EDIT: .NET Framework 4.7 implements .NET Standard 2.0, so you shouldn't need to load any assembly to use Activator.CreateInstance() to instantiate a .NET Standard type.
NETStandard 2.0.0-preview1 in not compatibility with net461 and net47.
but for realese .NET Core SDK 2.0 assemblies (as well as 2.0.0-preview2)
var netStandardDllPath = #"c:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\netstandard.dll";
Console.WriteLine(Assembly.LoadFrom(netStandardDllPath).FullName);
all is ok.
But if you steel need to load preview1 libraries, maybe you should to use netstandard2.0 instead net471.
For me solved doing the following:
1 - Installed latest .Net Framework on server.
2 - Updated windows server and my local machine.
3 - Went to Manage Nuget Package and updated all references on the update tab.
Perhaps only doing step 3 can solve in your case
In case if IBM Message Queue references are used in the project solution, this exception indicates that the DLL used for refering MQ classes are incompatible with the host(server) .NET version installed.
In this scenario, either we need to update server with latest update and make sure .NET latest version is available or use lower version of IBM Message queue DLL as reference.
Old version DLL - amqmdnet.dll (no new features will be introduced by IBM as not in support)
Latest version DLL - amqmdnetstd.dll (to run IBM MQ classes for .NET Standard, you must install Microsoft .NET Core)
Install NetStandard.Library 2.0.0.0 from NuGet , It works for me. when I downgrade .net framework 4.6.1 to 4.6.0
If you are having this issue for a project that used to work, try deleting the bin and obj folders since caching can cause this, too.

Errors on core framework types after changing target framework to .Net Framework 4.0.1

When working with an existing project that has been changed from .Net Framework 4.0 to .Net Framework 4.0.1 (formally, Microsoft .NET Framework 4 Platform Update 1 - Runtime Update (KB2478063) ) in Visual Studio using the project properties, core types (i.e. int and string) become undefined. What's broken and how do you fix it?
In the project references, the reference to mscorlib appears to be deleted as part of the update mechanism which changes the target framework version. Adding a new
<Reference Include="System.Core">4.0.1 entry to the project file cures the issue. This appears to need to be done in a text editor, as the built-in tooling doesn't provide an obvious control to force the reference back in.

Categories