【问题标题】:C# gRpc ProtoBuf creating complex message with child repeated, throws Object Reference not set ExceptionC# gRpc ProtoBuf 创建带有重复子项的复杂消息,抛出 Object Reference not set 异常
【发布时间】:2021-12-31 10:46:20
【问题描述】:

我刚开始使用 gRPC,除了消息构建的小问题外,所有工作都很完美:(

如果我使用标准 C# 类执行相同的代码,那么一切正常。

不清楚,为什么会出现这个错误,还在调查中

Grpc.AspNetCore.Server.ServerCallHandler[6]
  Error when executing service method 'GetCars'.
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.MatchTypes(Expression expression, Type targetType)
     at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression)
     at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)
     at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
     at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
     at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
     at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
     at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
     at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
     at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
     at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
     at Google.Protobuf.Collections.RepeatedField`1.AddRange(IEnumerable`1 values)
     at Google.Protobuf.Collections.RepeatedField`1.Add(IEnumerable`1 values)
     at RpcService.CarInfoRpcService.GetCars(RpcCarRequest request, ServerCallContext context) in C:\GitRepo\src\Services\CarInfo.gRpcService\Services\CarInfoRpcService.cs:line 75
     at Grpc.Shared.Server.UnaryServerMethodInvoker`3.Invoke(HttpContext httpContext, ServerCallContext serverCallContext, TRequest request)
  --- End of stack trace from previous location ---
     at Grpc.AspNetCore.Server.Internal.CallHandlers.UnaryServerCallHandler`3.HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
     at Grpc.AspNetCore.Server.Internal.CallHandlers.ServerCallHandlerBase`3.<HandleCallAsync>g__AwaitHandleCall|8_0(HttpContextServerCallContext serverCallContext, Method`2 method, Task handleCall)



var queryResult = dbContext.Cars
                   .AsNoTracking()
                   .Select(car => new RpcCarInfo()
                   {
                       CarId = car.CarId,
                       CarOwners = { car.Owner.Select(el => new RpcCarOwner() { Name = el.Name }) }
                   });

                return Task.FromResult(new RpcCarInfoResponse()
                {
                    Cars = { queryResult }
                });

【问题讨论】:

    标签: c# grpc


    【解决方案1】:

    我是这样做的

            var queryResult = dbContext.Cars.AsNoTracking().ToList();
    
            var carsInfo = queryResult.Select(car => new RpcCarInfo()
            {
                CarId = car.CarId,
                CarOwners = { car.Owner.Select(el => new RpcCarOwner() { Name = el.Name }).ToList() }
            }).ToList();
    
            return Task.FromResult(new RpcCarResponse()
            {
                CarsInfo = { carsInfo }
            });
    

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多