Using Hangfire and SendGrid


#1

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)

Am I doing something wrong?


#2

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.