Ethan
Ethan

Reputation: 11

Fail to new an instance of gRPC.Core.Channel when code run in Partial Trust

Grpc.Core.Channel is used call service. When code run in Partial Trust environment, an error below would pop up.

"Inheritance security rules violated by Type:'Grpc.Core.internal.SafeHandleZeroIsInvalid' Derived types must either match the security accessibility of the base type or be less accessible"

How can I use Channel to call Grpc service on a partial trust environment? if can't, is there another way to call Grpc Service?

Look into gRPC code, a .net class SafeHandle is inherited by SafeHandleZeroIsInvalid. As official document described, for partially trusted code, it’s not allowed. InheritanceDemand of SafeHandle is: "for full trust for inheritors. This member cannot be inherited by partially trusted code." https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle?view=netframework-4.8

I had a try to create my own class which inherited from SafeHandle. And, I tryed to new an instance of this class. When code run in Partial Trust, the same error pop up. What I am thinking the SafeHandle is the root reason cause this problem.

//the code is super simple, I just try to create a channel on a constructor. 
public class WriterClient
{
    private Channel _channel;
    private LoggingCenter _logger;
    public WriterClient(int port, ILogging logger)
    {
        _logger = logger == null? new LoggingCenter(new NullLogger()): new LoggingCenter(logger);
        //when run in Partial Trust, the code below will throw an exception.
        _channel = new Channel("127.0.0.1", port, ChannelCredentials.Insecure);            
    }

What I am wanted is to call Grpc service correctly in Partial Trust. Any workaround for it?

Upvotes: 1

Views: 271

Answers (0)

Related Questions