【问题标题】:cannot convert from 'method group' to 'TimerCallback' [duplicate]无法从“方法组”转换为“TimerCallback”[重复]
【发布时间】:2019-08-21 12:53:43
【问题描述】:

我正在尝试通过托管服务运行函数 getOrg,但有些它无法正常工作我不确定我做错了什么。

错误:

参数 1:无法从“方法组”转换为“TimerCallback” (CS1503)

public class TokenService : IHostedService
{
    public IConfiguration _Configuration { get; }
    protected IMemoryCache _cache;
    private Timer _timer;
    public IHttpClientFactory _clientFactory;

    private readonly IServiceScopeFactory _scopeFactory;

    public TokenService(IConfiguration configuration, IMemoryCache memoryCache, IHttpClientFactory clientFactory, IServiceScopeFactory scopeFactory)
    {
        _Configuration = configuration;
        _cache = memoryCache;
        _clientFactory = clientFactory;
        _scopeFactory = scopeFactory;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _timer = new Timer(getOrg, null, 0, 1000); // getting error here
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        //Timer does not have a stop. 
        _timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    public async Task getOrg()
        {
            var request = new HttpRequestMessage(HttpMethod.Get, "organizations");
            var response = await _client_NP.SendAsync(request);
            var json = await response.Content.ReadAsStringAsync();
            OrganizationsClass.OrgsRootObject model = JsonConvert.DeserializeObject<OrganizationsClass.OrgsRootObject>(json);


            using (var scope = _scopeFactory.CreateScope())
            {
                var _DBcontext = scope.ServiceProvider.GetRequiredService<DBContext>();

                foreach (var item in model.resources)
                {
                    var g = Guid.Parse(item.guid);
                    var x = _DBcontext.Organizations.FirstOrDefault(o => o.OrgGuid == g);
                    if (x == null)
                    {
                        _DBcontext.Organizations.Add(new Organizations
                        {
                            OrgGuid = g,
                            Name = item.name,
                            CreatedAt = item.created_at,
                            UpdatedAt = item.updated_at,
                            Timestamp = DateTime.Now,
                            Foundation = 3
                        });
                    }
                    else if (x.UpdatedAt != item.updated_at)
                    {
                        x.CreatedAt = item.created_at;
                        x.UpdatedAt = item.updated_at;
                        x.Timestamp = DateTime.Now;
                    }
                }

                await getSpace();

                await _DBcontext.SaveChangesAsync();
            }
        }
}

【问题讨论】:

  • 您正在使用Timer constructorTimerCallback 参数,它传递一个具有以下签名的方法:void SomeName(object state)。现在看看你的getOrg

标签: c# asp.net-core .net-core


【解决方案1】:

TimerCallback 接受状态的对象参数。尝试将getOrg 更改为:

public async void getOrg(object state)

【讨论】:

  • 我需要 getOrg 来执行任务,这不会使我的坏事像这样 public async Task getOrg(object state) 因为还有其他两个函数从 getOrg 链接在一起,一旦这些函数完成,它们都是异步的然后 await _DBcontext.SaveChangesAsync(); 将被执行
  • @Jojo - 您无需将getOrg 设为async Task 即可在getOrg 内等待。事件处理程序是少数可以接受 async void 方法的地方之一。
  • 知道了,谢谢
【解决方案2】:

您向System.Threading.Timer 构造函数提供了错误的参数。

第一个参数应该是delegate type(而不是getOrg)

public delegate void TimerCallback(object state);
  1. 所以在你的代码中添加一个委托:

    private void TimerProc(object state)
    {
    }
    
  2. 改变构造函数:

    _timer = new Timer(TimerProc, null, 0, 1000); // getting error here
    

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 2013-10-02
    • 1970-01-01
    • 2021-01-26
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多