grpc: [C#] AmbiguousMatchException: Multiple custom attributes of the same type found

What version of gRPC and what language are you using?

C# projects:

  • GrpcClients (netstandard2.0)
  • Net7WebApi (net 7 on IIS)
  • Net48WebApi (net 4.8 on IIS)
<PropertyGroup>
	<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
	<PackageReference Include="Google.Protobuf" Version="3.23.0" />
	<PackageReference Include="Grpc.Core" Version="2.46.6" />
	<PackageReference Include="Grpc.Tools" Version="2.54.0">
		<PrivateAssets>all</PrivateAssets>
		<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	</PackageReference>
	<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

What operating system (Linux, Windows,…) and version?

Win Server 2019

What runtime / compiler are you using (e.g. python version or version of gcc)

.Net Framework v4.8, .Net 7, VS 2022 latest

What did you do?

var channel = new Grpc.Core.Channel(config.Host, config.Port, CreateSslCredentials(cert));

What did you see instead?

[AmbiguousMatchException: Multiple custom attributes of the same type found.]
   System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) +110
   System.Runtime.InteropServices.RuntimeInformation.get_FrameworkDescription() +88

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +92
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +190
   System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) +57
   System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) +21
   Grpc.Core.Internal.PlatformApis.TryGetFrameworkDescription() in /var/local/git/grpc/src/csharp/Grpc.Core/Internal/PlatformApis.cs:217
   Grpc.Core.Internal.PlatformApis..cctor() in /var/local/git/grpc/src/csharp/Grpc.Core/Internal/PlatformApis.cs:77

[TypeInitializationException: The type initializer for 'Grpc.Core.Internal.PlatformApis' threw an exception.]
   Grpc.Core.Internal.PlatformApis.get_FrameworkDescription() +0
   Grpc.Core.Internal.UserAgentStringProvider..cctor() in /var/local/git/grpc/src/csharp/Grpc.Core/Internal/UserAgentStringProvider.cs:34

[TypeInitializationException: The type initializer for 'Grpc.Core.Internal.UserAgentStringProvider' threw an exception.]
   Grpc.Core.Internal.UserAgentStringProvider.get_DefaultInstance() +0
   Grpc.Core.Channel.EnsureUserAgentChannelOption(Dictionary`2 options) in /var/local/git/grpc/src/csharp/Grpc.Core/Channel.cs:330
   Grpc.Core.Channel..ctor(String target, ChannelCredentials credentials, IEnumerable`1 options) in /var/local/git/grpc/src/csharp/Grpc.Core/Channel.cs:69
   Grpc.Core.Channel..ctor(String host, Int32 port, ChannelCredentials credentials) in /var/local/git/grpc/src/csharp/Grpc.Core/Channel.cs:104
   PolicyCenterAPI.<>c.<Register>b__1_0(IServiceCollection services) in C:\Users\ag\Source\Repos\calcom-server\PolicyCenterAPI\App_Start\WebApiConfig.cs:45
   PolicyCenterAPI.DependencyInjectionConfig.UseDependencyInjection(HttpConfiguration config, Action`1 addServices) in C:\Users\ag\Source\Repos\calcom-server\PolicyCenterAPI\Composition\DependencyInjectionConfig.cs:15
   PolicyCenterAPI.DependencyInjectionConfig.UseDependencyInjection(HttpConfiguration config, Action`1 addServices, Action`1 addFilters) in C:\Users\ag\Source\Repos\calcom-server\PolicyCenterAPI\Composition\DependencyInjectionConfig.cs:25
   PolicyCenterAPI.WebApiConfig.Register(HttpConfiguration config) in C:\Users\ag\Source\Repos\calcom-server\PolicyCenterAPI\App_Start\WebApiConfig.cs:42
   System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +35
   PolicyCenterAPI.WebApiApplication.Application_Start() in C:\Users\ag\Source\Repos\calcom-server\PolicyCenterAPI\Global.asax.cs:19

[HttpException (0x80004005): The type initializer for 'Grpc.Core.Internal.UserAgentStringProvider' threw an exception.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +478
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +220
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): The type initializer for 'Grpc.Core.Internal.UserAgentStringProvider' threw an exception.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +659
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +90
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +189

Comment

hi, i am using common clients project (netstandard2.0) in two web api’s (.Net Framework v4.8 and .Net 7). It works well for me on .Net 7, but .Net Framework v4.8 project getting errors. Playable only on server 2019 and only on IIS.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

I’ve created a pull request to handle the exceptions, but I’d still like to know why and when this happens. https://github.com/grpc/grpc/pull/33141