'消失'或非解析属性特性

问题描述 投票:0回答:0

这是我的属性:

[AttributeUsage(AttributeTargets.Method)]
public class RemoteDatabaseCall : Attribute
{
    public string DestinationName { get; set; }
    public string DatabaseName { get; set; } = "";
    public string Procedure { get; set; } = "";
    public bool UseRedis { get; set; } = false;
    public int RedisCacheTimeInSeconds { get; set; } = 3600;
}

我有一个这样装饰的虚拟端点:

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    private readonly IDatabaseProxy _databaseProxy;

    public DemoController(IDatabaseProxy databaseProxy)
    {
        _databaseProxy = databaseProxy;

    }

    [HttpGet(Name = "Test")]
    [AllowAnonymous]
    [RemoteDatabaseCall(DatabaseName = "ApiData", DestinationName = "BIDB", Procedure = "Temp.ApiTest",RedisCacheTimeInSeconds = 10, UseRedis = true)]
    public async Task<string> Get()
    {
        var a = DatabaseProxy.GetDbAttr();
        var x = await _databaseProxy.ExecuteDatabaseCallProxyAsync(a, "Test", null);
        if (x.Success)
            return x.Result;
        return string.Empty;
    }
}

这里是

DatabaseProxy.GetDbAttr();

    public static RemoteDatabaseCall GetDbAttr()
    {
        MethodBase caller = default;
        for (var i = 0; i < 15; i++)
        {
            var mb = new StackFrame(i).GetMethod();
            if (!mb.GetCustomAttributes<RemoteDatabaseCall>().Any()) continue;
            caller = mb;
            break;
        }

        return caller.GetCustomAttributes<RemoteDatabaseCall>().FirstOrDefault();
    }

该函数旨在获取上一个调用,这样我就可以将属性信息发送到下一个调用。最初这是

ExecuteDatabaseCallProxyAsync()
的一部分,但是今天早些时候我第一次得到这个例外时我把它分开了。

这是部分内容:

_databaseProxy.ExecuteDatabaseCallProxyAsync()

public async Task<(bool Success, string Result, bool WasFetchedFromCache)> ExecuteDatabaseCallProxyAsync(RemoteDatabaseCall attribute, string uniqueKeyForRedisCache, StoredProcedureParameters parameters)
    {
        var correlationId = Guid.NewGuid();

        (bool Success, string Result, bool WasFetchedFromCache) ret = (false, string.Empty, false);

        var an = attribute.DestinationName;
        var ap = attribute.Procedure;
        var u = uniqueKeyForRedisCache;
        var z = an + ap + u;
        var x = z;

        if(!string.IsNullOrEmpty(uniqueKeyForRedisCache) && !uniqueKeyForRedisCache.StartsWith($"{attribute.DatabaseName}"))
            uniqueKeyForRedisCache = $"{attribute.DestinationName}.{attribute.Procedure}:{uniqueKeyForRedisCache}";

an
,
ap
,
u
,
z
x
的废话是我试图理解究竟是什么让我的代码在各个层面都感到困惑。最后一行 (
uniqueKeyForRedisCache = $"{attribute.Dest...
) 总是失败:

System.NullReferenceException: Object reference not set to an instance of an object. at System.Buffer.Memmove(Byte& dest, Byte& src, UIntPtr len)

此外,如果我调试它,那么这就是我得到的:

注意没有

an
u
z
,但
attribute.DestinationName
显然仍然是固定的。

进入下一行然后产生异常。

c# attributes .net-7.0
© www.soinside.com 2019 - 2024. All rights reserved.