InnerException 抛出 TypeLoadException TypeLoadException:无法从程序集 'System.ServiceModel 加载类型 'System.UriTemplate',

InnerException throws TypeLoadException TypeLoadException: Could not load type 'System.UriTemplate' from assembly 'System.ServiceModel,

我尝试实现一个 azure 函数,但不断收到以下异常。

  "TypeLoadException: Could not load type 'System.UriTemplate' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

似乎还有其他一些人得到了同样的例外。 https://github.com/Azure/azure-functions-host/issues/3568

我已经使用 .NETCore 3.0 和 AzureFunctions v2.0 实现了我的解决方案,并使用 WindowsAzure.ServiceBus v6.0.0、System.private.ServiceModel 4.6.0 和 System.ServiceModel.Primitives 4.6.0。 据我所知,版本应该没有问题,但目前我不确定。

在 link 中有很多解决方法,我已经尝试过但没有结果。在所有这些文件中,我都更改了文件结构以匹配我的文件结构。 (我知道其中一些是用于其他类型的项目等等,但是 none 越不值得一试)。

<Target Name="FixForDotnetWcfIssueBuild" BeforeTargets="PostBuildEvent"> <Copy SourceFiles="$(OutputPath)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" /> </Target> 

<Target Name="FixForDotnetWcfIssuePublish" AfterTargets="AfterPublish"> <Copy SourceFiles="$(PublishDir)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(PublishDir)bin" /> </Target>

所以在我看来 WindowsAzure.ServiceBus 6.0.0 与 .NETCoreApp v3.0 不兼容。

希望有人知道我不知道的事情并愿意提供帮助,我已经告诉了足够多的人来解决这个难题。

Azure Function 有两个版本。一个基于 .NET Framework,另一个基于 .NET Core。从您的描述来看,您似乎使用的是 .NET Core 3.0Function v2.0.

你的项目使用了WindowsAzure.ServiceBus v6.0.0,但是你看过这个Nuget包的说明了吗?

https://www.nuget.org/packages/WindowsAzure.ServiceBus/

它基于 .NET Framework。

如果你在错误的技术堆栈上使用这个包,当然会出现这个异常。