Could not load assembly System.Runtime.CompilerServices.Unsafe - entity-framework

I am working on .NET Core app + NET Standard 2 library.
This library uses EntityFrameworkCore.SQLServer.
When I added yet EntityFrameworkCore.PostgreSQL then app during the start gives me an error:
... Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExce
ptionInterceptor.MoveNext(CancellationToken cancellationToken)
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, C
ulture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Npgsql.NpgsqlWriteBuffer.Write[T](T value)
I have reinstalled the EntityFrameworkCore.PostgreSQL - not fixed.
May be I can add this assembly to my library but where I can get it:
'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, C
ulture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Thanx.

Related

Can't use assembly in PowerShell after I've installed the appropriate nuget package

I'm having a problem with a library installed by a NuGet package not working from PowerShell. I've installed it like this (also tried CurrentUser scope and a later version of the package but it makes no difference in the end):
Install-Package Microsoft.Bcl.AsyncInterfaces -Version 1.0.0.0
Microsoft.Bcl.AsyncInterfaces is required by an AWS.Tools module I'm leveraging, but no matter what I do the module assemblies cannot find it. At first I couldn't even add the type into my own session:
Add-Type -AssemblyName Microsoft.Bcl.AsyncInterfaces
Add-Type: Cannot find path '/home/username/src/work-aws/generate-hostname/Microsoft.Bcl.AsyncInterfaces.dll' because it does not exist.
But I was able to load it directly from the package source directory:
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | Split-Path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
However... the assemblies in the AWS module still can't see it. The errors I'm getting from the AWS classes are as follows:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
What am I missing here? I'm not sure how to get this AWS module to see the assembly even after invoking Add-Type to forcibly load the assembly from its fully-qualified path.
I've posted the output of the Exception variable here, though everything still indicates Microsoft.Bcl.AsyncInterfaces can't be found:
ErrorRecord : Exception calling "LoadTable" with "2" argument(s): "The type initializer for 'Amazon.DynamoDBv2.DynamoDBEntryConversion' threw an exception."
WasThrownFromThrowStatement : False
TargetSite : Void ConvertToMethodInvocationException(System.Exception, System.Type, System.String, Int32, System.Reflection.MemberInfo)
StackTrace : at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String
methodName, Int32 numArgs, MemberInfo memberInfo)
at CallSite.Target(Closure , CallSite , Type , Object , String )
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at System.Management.Automation.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Message : Exception calling "LoadTable" with "2" argument(s): "The type initializer for 'Amazon.DynamoDBv2.DynamoDBEntryConversion' threw an exception."
Data : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException : System.TypeInitializationException: The type initializer for 'Amazon.DynamoDBv2.DynamoDBEntryConversion' threw an exception.
---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeAssembly.get_DefinedTypes()
at Amazon.DynamoDBv2.DynamoDBEntryConversion.AddConverters(String suffix)
at Amazon.DynamoDBv2.DynamoDBEntryConversion.SetV1Converters()
at Amazon.DynamoDBv2.DynamoDBEntryConversion..ctor(ConversionSchema schema, Boolean isImmutable)
at Amazon.DynamoDBv2.DynamoDBEntryConversion..cctor()
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
--- End of inner exception stack trace ---
at CallSite.Target(Closure , CallSite , Type , Object , String )
HelpLink :
Source : System.Management.Automation
HResult : -2146233087
I found that the reason assemblies installed with Install-Package need to be loaded in this way:
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | split-path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
is because the package/lib directories in the PackagementManagement provider folders are not part of .NET default probe paths. This is less of a problem when you have modules or projects where the assembly paths are configurable in a file, but in a PowerShell session AFAIK you can't modify the assembly probe path. So you have to load the assembly with the full path.
As for why the AWS assemblies in question couldn't find the assembly name after I loaded it into my session, I found the solution as well. After some additional testing, I found that there is an order of operations here; if you add an assembly with Add-Type or [Reflection.Assembly]::LoadFile after another assembly attempts to use it, that second assembly won't be able to find the first assembly for the duration of the PowerShell session. In my case, any assembly making use of Amazon.DynamoDBv2.DynamoDBEntryConversion in some way was failing because it could never locate Microsoft.Bcl.AsyncInterfaces.
My issue here was I would load up a new session, and start running my code forgetting to first use Add-Type to add the Microsoft.Bcl.AsyncInterfaces assembly, then use the AWS library which relied on it. Once the method failed, I added the assembly but it was too late for that session. So the fix here was to do the following:
# Load the Microsoft.Bcl.AsyncInterfaces assembly
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | split-path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
# Read a DynamoDB table from AWS
$regionEndpoint = [RegionEndPoint]::GetBySystemName('us-west-2')
$client = [AmazonDynamoDBClient]::new($regionEndpoint)
[DocumentModel.Table]::LoadTable($client, 'MyDDBTableName') # This is the call that was failing before, succeeds now

System.IO.FileNotFoundException: Could not load assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Getting this Error In Xamarin Form Android Project I am refering Entity framework library int android project. Error as below
Severity Code Description Project File Line Suppression State Error
Exception while loading assemblies: System.IO.FileNotFoundException:
Could not load assembly 'EntityFramework, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089'. Perhaps it doesn't
exist in the Mono for Android profile? File name:
'EntityFramework.dll' at
Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference
reference, ReaderParameters parameters) at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection`1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver
resolver) RadLoc.Android C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets
1500
I am stuck here please help me out of this..
Just like #magicandre1981 said. Entity Framework doesn't fit in Xamarin.Android project. You need to use EntityFramework Core.
For a simple jump in and brief introduction, you can refer to Building Android Apps with Entity Framework.
For a windows application with this error:
If your copying the exe to a different folder from /bin/
You also need to include a copy of the application .config file along with the EntityFramework.xml and EntityFramework.SqlServer.xml files.

Calling .Netstandard dll from .net 4.6.2 azure service fabric stateful assembly

I am struggling to call a .netstandard dll from a .Net 4.6.2 azure service fabric statefulservice assembly in Visual Studio 2017 [on windows 7]. I am on latest version of service fabric [5.4.164.9494 and SF SDK version: 2.4.164.9494]
Followed the exact steps as mentioned in Using .net standard 1.5 lib in .net 4.6.2 misses System.Runtime 4.1.0.0. Tried multiple things like creating the netstandard class library from .Net core,.Netstandard and plain old PCL (converted to .net standard from project properties). Nothing works. The project does compile but when I deploy the project to the cluster, The statefulservice never get's initialized and this is the error in the log..
{
"Timestamp": "2017-03-17T09:28:22.8026277-04:00",
"ProviderName": "Microsoft-ServiceFabric-Services",
"Id": 5,
"Message": "RunAsync has failed for a stateful service replica. Application Type Name: ServiceFabricTestType, Application Name: fabric:/ServiceFabricTest, Service Type Name: Stateful1Type, Service Name: fabric:/ServiceFabricTest/Stateful1, Partition Id: 17258b22-c015-4eec-8026-edd3e21fffc4, Replica Id: 131342308870035108, WasCancelled: False, Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
at Stateful1.Stateful1.<RunAsync>d__2.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
at Stateful1.Stateful1.RunAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatefulServiceBase.Microsoft.ServiceFabric.Services.Runtime.IStatefulUserServiceReplica.RunAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatefulServiceReplicaAdapter.<ExecuteRunAsync>d__12.MoveNext()
",
"ProcessId": 22424,
"Level": "Error",
"Keywords": "0x0000F00000000000",
"EventName": "StatefulRunAsyncFailure",
"ActivityID": null,
"RelatedActivityID": null,
"Payload": {
"applicationTypeName": "ServiceFabricTestType",
"applicationName": "fabric:/ServiceFabricTest",
"serviceTypeName": "Stateful1Type",
"serviceName": "fabric:/ServiceFabricTest/Stateful1",
"partitionId": "17258b22-c015-4eec-8026-edd3e21fffc4",
"replicaId": 131342308870035108,
"wasCanceled": false,
"exception": "System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
at Stateful1.Stateful1.<RunAsync>d__2.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
at Stateful1.Stateful1.RunAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatefulServiceBase.Microsoft.ServiceFabric.Services.Runtime.IStatefulUserServiceReplica.RunAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatefulServiceReplicaAdapter.<ExecuteRunAsync>d__12.MoveNext()
"
}
}
Any help is appreciated..
Are you running a SF cluster with Windows Server 2012R2 or 2016? The default option from Azure portal is still 2012R2 and only has .NET 4.5.1 unless you install 4.6.2 by yourself (manual or through script)

EF code first Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information

My MVC project was working, but now I get an error. That error occurs in my controller in my data layer (using Code First)
Unable to load one or more of the requested types. Retrieve the
LoaderExceptions property for more information.
This is the line causing the error:
var cultures = db.Cultures.Select(c => c.Code).ToArray();
In my mvc project I have deleted the data layer and added it again it again and then I set copy local to true but the error was not fixed.
My MVC project use System.Web.Mvc, Version=4.0.0.0 and copy local is true
And Data layer use System.Web.Mvc, Version=3.0.0.0 and copy local is true
I looked error inner exception it give that error
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Nuget.exe fails with "Could not load file or assembly 'System.Net.Http...'

I am trying to run "nuget restore" on a server that does not have a Visual Studio installation. But nuget.exe fails immediately. What else needs to be installed for NuGet to run?
C:\Users\kevin>nuget help
Could not load file or assembly 'System.Net.Http,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or
one of its dependencies. The system cannot find the file specified.
Could not load type
'System.Runtime.CompilerServices.IAsyncStateMachine' from assembly
'mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'. Could not load file or assembly
'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
system cannot find the file specified.
Had this issue as well. Newer versions of NuGet requires .NET 4.5. installing .NET 4.5.2 resolved the issue for me.