繁体   English   中英

C# gRpc ProtoBuf 创建复杂消息与子重复,抛出 Object 参考未设置异常

[英]C# gRpc ProtoBuf creating complex message with child repeated, throws Object Reference not set Exception

我是从 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 }
                });

我是这样做的

        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 }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM