Non-static method requires a target. 返回

SqlSugar 沟通中
3 120
该叫什么 2:1 发布于6天前
悬赏:0 飞吻

问题描述

错误出现在使用sqlsugar和cap的订阅类中, 通过cap面板看到,使用了sqlsugar的消息都会报错,同事使用ef,同样用阿里rabbitmq,他没有见到过这个错,奇怪的是,cap自动重试没用,手动重试就正常,或重启项目后前一两分钟正常。其他地方未发现此错误:"cap-exception": "SubscriberExecutionFailedException-->Non-static method requires a target."


错误日志:

System.Reflection.TargetException: Non-static method requires a target.

   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

   at SqlSugar.FastCopy.GetDiff[T](T item, T trackingData)

   at SqlSugar.UpdateableProvider`1.Tracking(T item)

   at SqlSugar.UpdateableProvider`1.Init()

   at SqlSugar.SqlSugarProvider.CreateUpdateable[T](T[] UpdateObjs)

   at SqlSugar.SqlSugarProvider.Updateable[T](T[] UpdateObjs)

   at SqlSugar.SqlSugarProvider.Updateable[T](T UpdateObj)

   at SqlSugar.SqlSugarClient.Updateable[T](T UpdateObj)

   at SqlSugar.SqlSugarScope.Updateable[T](T UpdateObj)

   at xxxx.Application.Service.SyncData.Access.AccessRecordSubscriber.Handle(GateRequest request) in E:\xxx\AccessRecordSubscriber.cs:line 122

2026-04-07T01:33:50.734315831Z


错误代码:

private readonly ICache _cache;

private readonly ILogger<AccessRecordSubscriber> _logger;

private readonly ISqlSugarRepository<Register> _RegisterRep;

private readonly ISqlSugarRepository<DoorAccessRecord> _DoorAccessRecordRep;


public AccessRecordSubscriber(ICache cache, ISqlSugarRepository<DoorAccessRecord> doorAccessRecordRep, ISqlSugarRepository<Register> registerRep, ILogger<AccessRecordSubscriber> logger)

{

    _cache = cache;

    _DoorAccessRecordRep = doorAccessRecordRep;

    _RegisterRep = registerRep;

    _logger = logger;

}


[CapSubscribe("Access.Record.Received")]

public async Task Handle(AccessRequest request)

{

    _logger.LogInformation("开始处理门禁消息: ExternalId={ExternalId}, OccurTime={OccurTime}", request.ExternalId, request.OccurTime);


    if (!DateTime.TryParse(request.OccurTime, out var occurTime))

        occurTime = DateTime.Now;


    var lockKey = $"lock:access_process:{request.ExternalId}:{request.OccurTime}";

    var lockValue = Guid.NewGuid().ToString("N");


    try

    {

        bool isLocked = await _cache.AddLock(lockKey, lockValue, TimeSpan.FromSeconds(15));

        if (!isLocked)

        {

            return;

        }


        bool isExist = await _DoorAccessRecordRep.Context.Queryable<DoorAccessRecord>().AnyAsync(x => x.PassCredential == request.ExternalId && x.EntryTime == occurTime);


        if (isExist)

        {

            _logger.LogInformation("消息已处理过 {ExternalId}", request.ExternalId);

            return;

        }


        var register = await _RegisterRep.Context.Queryable<Register>().With(SqlWith.NoLock).Where(r => r.ActivityId == 10134 && r.CertificateCode == request.ExternalId).FirstAsync();


        await _DoorAccessRecordRep.Context.AsTenant().BeginTranAsync();


        try

        {

            var accessRecord = new DoorAccessRecord

            {

                PassCredential = request.ExternalId,

                DeviceLocation = request.AccessPoint,

                DeviceLocationId = 0L,

                DeviceChannelAlias = request.PassageCode,

                DeviceChannelId = 0L,

                EntryTime = occurTime,

                CreatedTime = DateTime.Now,

                Status = GateStatus.Normal,

                CompensationDirection = null,

                CompensationFlag = false,

            };


            if (register != null)

            {

                .......

                accessRecord.Title = register.Title;


                if (register.EntranceState != "1")

                {

                    register.EntranceState = "1";

                    register.EntranceTime = occurTime;


                    await _RegisterRep.Context.Updateable(register)

                        .UpdateColumns(it => new { it.EntranceState, it.EntranceTime })

                        .ExecuteCommandAsync();

                }

            }

            else

            {

                accessRecord.RegisterId = 0;

                _logger.LogWarning("未找到匹配的注册人员信息: ExternalId={ExternalId}", request.ExternalId);

            }


            await _DoorAccessRecordRep.Context.Insertable(accessRecord).ExecuteCommandAsync();


            await _DoorAccessRecordRep.Context.AsTenant().CommitTranAsync();

            _logger.LogInformation("门禁消息处理成功入库: {ExternalId}", request.ExternalId);

        }

        catch (Exception ex)

        {

            await _DoorAccessRecordRep.Context.AsTenant().RollbackTranAsync();

            _logger.LogError(ex, "门禁消息处理失败: {ExternalId}", request.ExternalId);

            throw;

        }

    }

    finally

    {

        await _cache.ReleaseLock(lockKey, lockValue);

    }

}


热忱回答3

  • 这个是多个类库下的sqlsugar版本不一致引起的。 删掉sqlsugar.dll重新发布。

    0 回复
  • 2:1 2:1 VIP0
    5天前

    @fate sta:好的谢谢,我下午试一试, 目前临时解决办法是用sqlsugar执行原生sql, 和cap的并发也有关系,项目只引入了SqlSugar.IOC 2.0.0 和 sqlSugarCore5.1.4.105

    0 回复
  • 2:1 2:1 VIP0
    3天前

    @fate sta:升级版本后观察了段时间 没再出现这个问题了

    0 回复