Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ServiceContext.SetRawBytesResponse #677

Merged
merged 4 commits into from
Sep 19, 2023
Merged

Conversation

mayuki
Copy link
Member

@mayuki mayuki commented Sep 19, 2023

This PR introduces ServiceContext.SetRawBytesResponse method.

This method allows you to set raw byte sequences as a response. This makes it possible to send a cached response body without serialization.

public override async ValueTask Invoke(ServiceContext context, Func<ServiceContext, ValueTask> next)
{
    if (ResponseBytesCache.TryGetValue(context.CallContext.Method, out var cachedBytes))
    {
        context.SetRawBytesResponse(cachedBytes);
        return;
    }

    await next(context);

    ResponseBytesCache[context.CallContext.Method] = MessagePackSerializer.Serialize(context.Result);
}

Note

The raw byte sequence must be serialized as a MessagePack (or custom serialization) format. MagicOnion will write a byte requence directly into the response buffer.

@mayuki mayuki merged commit edfe2f0 into main Sep 19, 2023
4 checks passed
@mayuki mayuki deleted the feature/RawBytesResponse branch September 19, 2023 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant