0

I'm trying to run function getOrg though hosted services but some how its not working I'm not sure what I'm doing wrong.

Error:

Argument 1: cannot convert from 'method group' to '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();
            }
        }
}
Jojo
  • 127
  • 1
  • 2
  • 12
  • You are using `Timer` [constructor](https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer.-ctor) with [TimerCallback](https://learn.microsoft.com/en-us/dotnet/api/system.threading.timercallback) parameter, which pass a method with following signature: `void SomeName(object state)`. Now look at your `getOrg`. – Sinatr Aug 21 '19 at 12:58

2 Answers2

3

TimerCallback takes on object parameter for state. Try changing getOrg to:

public async void getOrg(object state)
Sean
  • 60,939
  • 11
  • 97
  • 136
  • I need `getOrg` to Task not void my bad something like this public `async Task getOrg(object state)` because there are two other functions that are chained together from `getOrg` and they all are async once those those functions finishes then await `_DBcontext.SaveChangesAsync();` will be executed – Jojo Aug 21 '19 at 13:15
  • @Jojo - You don't need to make `getOrg` an `async Task` in order to await within `getOrg`. Event handlers is one of the few places where it's acceptable to have an `async void` method. – Sean Aug 21 '19 at 13:17
  • Sure got it thank you – Jojo Aug 21 '19 at 13:22
1

You are providing wrong parameters to System.Threading.Timer constructor.

The first parameter should be a delegate type (instead of getOrg):

public delegate void TimerCallback(object state);
  1. So add a delegate to your code:

    private void TimerProc(object state)
    {
    }
    
  2. Change the constructor:

    _timer = new Timer(TimerProc, null, 0, 1000); // getting error here
    
EylM
  • 5,967
  • 2
  • 16
  • 28