Skip to content

把自定义的AsyncCommandFilterAttribute贴在抽象父类上,子类继承抽象父类,自定义的异步命令过滤器不生效!但是贴在子类上可以生效,这个如何是好? #828

@zpc870921

Description

@zpc870921

把自定义的异步命令过滤器贴在抽象父类上,子类继承抽象父类,发现自定义的异步命令过滤器不生效!但是贴在子类上可以生效,这个如何是好?

如下是我自定义一个AsyncCommandFilterAttribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class AuthorizationCommandFilterAttribute : AsyncCommandFilterAttribute
{
public override async ValueTask OnCommandExecutedAsync(CommandExecutingContext commandContext)
{

}

public override async ValueTask<bool> OnCommandExecutingAsync(CommandExecutingContext commandContext)
{
     if(commandContext.Session is MySession mySession && mySession.IsAuthenticated)
     {
        return true;
     }

     var encoder = commandContext.Session.Server.ServiceProvider.GetService(typeof(JsonPackageEncoder)) as JsonPackageEncoder;
    //await commandContext.Session.SendAsync(Encoding.UTF8.GetBytes("ERROR: Please Login First!"));
    await commandContext.Session.SendAsync(encoder,new JsonProtocolPackage()
    {
        Key=0x08,
        JsonBody=JsonSerializer.Serialize(new {
        ErrorMsg="Please Login First!"
        },new JsonSerializerOptions { 
            Encoder=System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        })
    });
    return false;
}

}
抽象父类:

/// <summary>
/// json协议命令基类
/// </summary>
[AuthorizationCommandFilter]
[Command(Key =0x00)]
public abstract class JsonCommandBase<TModel> : IAsyncCommand<MySession,JsonProtocolPackage>
    where TModel:class
{
    public async ValueTask ExecuteAsync(MySession session,JsonProtocolPackage package, CancellationToken cancellationToken)
    {
        try
        {
            var model = JsonSerializer.Deserialize<TModel>(package.JsonBody,JsonSerializerOptions.Web);
            if (model != null)
            {
                await ExecuteAsync(session, model, cancellationToken);
            }
        }
        catch (Exception ex) 
        {
            Console.WriteLine(ex);
        }
    }

    protected abstract ValueTask ExecuteAsync(MySession session, TModel model, CancellationToken cancellationToken);
}

[Command(Key =(byte)0x02)] // 对应 Type = 2 数据上传命令
// [AuthorizationCommandFilter]
public class DataUploadCommand : JsonCommandBase//IAsyncCommand<MySession,JsonProtocolPackage>
{
private readonly JsonPackageEncoder _encoder;
public DataUploadCommand(JsonPackageEncoder encoder)
{
_encoder = encoder;
}
protected override async ValueTask ExecuteAsync(MySession session, DataUploadInfo model, CancellationToken cancellationToken)
{
Console.WriteLine($"[数据上传] 设备 {session.DeviceId} 上传数据: {model}");
// await ((IAppSession)session).SendAsync(Encoding.UTF8.GetBytes("DATA_RECEIVED"));
await ((IAppSession)session).SendAsync(_encoder,new JsonProtocolPackage {
Key=0x02,
JsonBody=JsonSerializer.Serialize(new { ErrorMsg="upload success!"})
});
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.0SuperSocket 2.0bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions