I’m currently using SendGrid but now need to use Hangfire for scheduling.
This is what I’ve done
BackgroundJob.Enqueue(() => client.SendEmailAsync(msg, new CancellationToken()));
But I get this exception…
System.InvalidOperationException
Unable to resolve service for type ‘System.Net.IWebProxy’ while attempting to activate ‘SendGrid.SendGridClient’.
System.InvalidOperationException: Unable to resolve service for type ‘System.Net.IWebProxy’ while attempting to activate ‘SendGrid.SendGridClient’.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass9_0.b__0()
at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func1 continuation) at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable1 filters)
at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)
From what i can see in the stacktrace, it seems that you’re trying to inject SendGridClient but IWebProxy is not registered in your dependency injection.
I never used it, so you’re probably better than me on that domain.
Maybe something like this would work inside your startup : services.AddTransient<ISendGridClient>(isp => new SendGridClient(apiKey));
I noticed that in the example that i saw on the internet, they never use injection, so if my example before doesn’t work, you probably could remove it from your constructor parameter and try to use it inside of your method