Skip to content

Add ParameterInfo argument to BindAsync#35791

Merged
halter73 merged 4 commits intomainfrom
halter73/35748
Aug 27, 2021
Merged

Add ParameterInfo argument to BindAsync#35791
halter73 merged 4 commits intomainfrom
halter73/35748

Conversation

@halter73
Copy link
Member

@halter73 halter73 commented Aug 26, 2021

Before:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/webhook", (CloudEvent cloudEvent) =>
{
    redis.Publish(cloudEvent);
});

app.Run();

public class CloudEvent
{
    public static async ValueTask<object?> BindAsync(HttpContext context)
    {
        return await CloudEventParser.ReadEventAsync(context);
    }
}

After:

public class CloudEvent
{
    public static async ValueTask<CloudEvent?> BindAsync(HttpContext context, ParameterInfo parameter)
    {
        return await CloudEventParser.ReadEventAsync(context, parameter.Name);
    }
}

This adds a ParameterInfo as a second parameter and changes the return type from ValueTask<object?> to ValueTask<T> (and/or ValueTask<T?> for reference types).

Static methods with close-but-different signatures (e.g. ValueTask<CloudEvent?> BindAsync(HttpContext context) or ValueTask<object?> BindAsync(HttpContext context, ParameterInfo parameter) will not be matched.

Note: If CloudEvent is a struct, the return type must be ValueTask<CloudEventStruct> not ValueTask<ClouldEventStruct?>. I do not think this is a major issue since if CloudEven was a struct we already wouldn't call BindAsync given a CloudEventStruct? parameter. When we add support for nullable struct BindAsync parameter, we should also look for a nullable-returning BindAsync method.

public struct CloudEventStruct
{
	// This will NOT be matched because it returns a nullable struct!!!
    public static async ValueTask<CloudEventStruct?> BindAsync(HttpContext context, ParameterInfo parameter)
    {
        return await CloudEventParser.ReadEventAsync(context, parameter.Name);
    }
}

Fixes #35748

@halter73
Copy link
Member Author

/backport to release/6.0-rc1

@github-actions
Copy link
Contributor

Started backporting to release/6.0-rc1: https://github.com/dotnet/aspnetcore/actions/runs/1171970183

@Pilchie Pilchie added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Aug 26, 2021
@halter73 halter73 changed the title Add ParameterInfo argument to BindAsync and return specific type instead of object Add ParameterInfo argument to BindAsync Aug 27, 2021
@halter73 halter73 merged commit d2c48ca into main Aug 27, 2021
@halter73 halter73 deleted the halter73/35748 branch August 27, 2021 03:54
@ghost ghost added this to the 7.0-preview1 milestone Aug 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ParameterInfo argument to BindAsync and return specific type instead of object

4 participants