I'm trying to use the SendGrid extensions for Azure WebJobs. I've tried to follow the example, but, unfortunately, the WebJob app crashes with the following error:
Could not load file or assembly 'SendGridMail, Version=6.1.0.0, Culture=neutral, PublicKeyToken=2ae73662c35d80e4' or one of its dependencies. The system cannot find the file specified.
Having run into something similar once before involving NewtonSoft's Json, I tried to fix the problem by adding the following to app.config:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="SendGridMail" publicKeyToken="2ae73662c35d80e4" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
</assemblyBinding>
Unfortunately, that didn't help (same error message).
I noticed that the WebJobs SendGrid extension nuget package install an old version of SendGrid (v6.1). But I'm trying to follow the WebJob Extensions examples, and they have the following using statement:
using SendGrid.Helpers.Mail;
and, unfortunately, the SendGrid.Helpers.Mail namespace doesn't exist in v6.1 of SendGrid.
Additional Details
Based on Tom's feedback, I uninstalled the SendGrid extensions nuget package, and installed the required libraries "manually" by editing project.json. That gets me the latest stable version of each of the libraries...but the app still crashes on startup with the same error.
Here's the startup code:
public static void Main(string[] args)
{
var config = new JobHostConfiguration()
{
NameResolver = new QueueNameResolver( new AzureContext() ),
};
if( config.IsDevelopment )
{
config.UseDevelopmentSettings();
}
config.Tracing.ConsoleLevel = TraceLevel.Info;
// this is the line where the exception gets thrown
config.UseSendGrid();
JobHost host = new JobHost( config );
host.RunAndBlock();
}
I am following the SendGrid example, modifying a method signature in Functions.cs as follows:
public static void ProcessPhoneFileMessage(
[QueueTrigger( "%" + nameof( ContainerQueueConstants.PhoneFiles ) + "%" )] AgencyOutreachMessage msg,
[SendGrid] out Mail message
)
{
StringWriter swLogger = new StringWriter();
try
{
GeneratePhoneFileJob fmJob = new GeneratePhoneFileJob( swLogger, msg );
fmJob.Execute();
}
catch( Exception e )
{
swLogger.WriteLine( $"{nameof( GeneratePhoneFileJob )} triggered an exception, message was: {e.Message}" );
}
message = new Mail();
message.Subject = "Phone File Job";
message.AddContent( new Content( "text/plain", "Completed the Phone File Job" ) );
message.AddContent( new Content( "text/plain", swLogger.ToString() ) );
Personalization personalization = new Personalization();
personalization.AddTo(new Email("mark@arcabama.com", "Mark Olbert") );
message.AddPersonalization( personalization );
}
If I don't do the call to UseSendGrid() in the app startup code I get an exception when the method definition is parses, telling me to be sure to call UseSendGrid(). But, as I noted above, UseSendGrid() blows up.
No Exception, But No Email Either
Based on Tom's example, I modified the message-processing function so it didn't use an out Mail parameter, and simply built and sent the email from within the method body:
SendGridAPIClient sg = new SendGridAPIClient( "redacted" );
Mail message = new Mail();
message.Subject = "Phone File Job";
message.AddContent( new Content( "text/plain", "Completed the Phone File Job" ) );
message.AddContent( new Content( "text/plain", swLogger.ToString() ) );
Personalization personalization = new Personalization();
personalization.AddTo(new Email("redacted", "Mark Olbert") );
message.AddPersonalization( personalization );
sg.client.mail.send.post( requestBody: message.Get() );
The console app now starts up fine, and the message processor runs fine...but no email gets sent. Or, rather, none arrives, and when I check my dashboard at SendGrid.com there's no record of any activity.
Success!
I finally got this to work. I believe the "final" problem was that I'd neglected to include a From address for the email I was creating. Once I did that, it worked like a charm.