Search code examples
c#asp.net-coreasp.net-web-apigrpcgrpc-web

In gRPC unable to bind enum values from message to dontnet core Dto


I need to define a string array type dataType in Grpc message. not sure how to do. right now i am doing it as a

repeated string Title= 1,

here i need name field as string array Type. But it is showing error that it is, field is readonly type when bind data in it:

public override async Task<UserResponse> CreateUser(
    UserModel request, ServerCallContext context)
{
    var eventResponse = new UserResponse();
    var createCmd = new CreateUserCommand
    {
        Model = new UserDto
        {
            Title = request.Title,
            Id = request.Id,
        }
    }
}

here in title i need to bind data


Solution

  • The generated code from protoc here gives you something like:

    private readonly RepeatedField<string> title_ = new RepeatedField<string>();
    [DebuggerNonUserCodeAttribute]
    public RepeatedField<string> Title {
        get { return title_; }
    }
    

    So: Title is indeed read-only. This means that instead of assigning it, you should explore what APIs exist for adding to it - i.e.

    var user = new UserDto
    {
        Id = request.Id,
    }
    user.Title.Add(request.Title);
    // or AddRange, etc
    

    You may still be able to use initializer syntax, too:

    new UserDto
    {
        Id = request.Id,
        Title = { request.Title }
    }
    

    (which is an .Add)