我正在尝试使用 Azure WebJobs 的 SendGrid 扩展。我尝试按照该示例进行操作,但不幸的是,WebJob 应用程序崩溃并出现以下错误:
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.
在涉及 NewtonSoft 的 Json 之前遇到过类似的问题,我尝试通过将以下内容添加到 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>
不幸的是,这没有帮助(相同的错误消息)。
我注意到 WebJobs SendGrid 扩展 nuget 包安装了旧版本的 SendGrid (v6.1)。但我正在尝试遵循 WebJob Extensions 示例,它们具有以下 using 语句:
using SendGrid.Helpers.Mail;
不幸的是,SendGrid.Helpers.Mail 命名空间在 SendGrid v6.1 中不存在。
其他详细信息
根据 Tom 的反馈,我卸载了 SendGrid 扩展 nuget 包,并通过编辑 project.json “手动”安装所需的库。这让我获得了每个库的最新稳定版本...但应用程序在启动时仍然崩溃,并出现相同的错误。
这是启动代码:
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();
}
我按照 SendGrid 示例,修改 Functions.cs 中的方法签名,如下所示:
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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ff2feedf4dffeedfcfefdfef2feb1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>", "Mark Olbert") );
message.AddPersonalization( personalization );
}
如果我不在应用程序启动代码中调用 UseSendGrid(),则在解析方法定义时会出现异常,告诉我一定要调用 UseSendGrid()。但是,正如我上面提到的,UseSendGrid() 会崩溃。
没有异常(exception),但也没有电子邮件
根据 Tom 的示例,我修改了消息处理函数,因此它不使用 out Mail 参数,而是简单地从方法主体内构建并发送电子邮件:
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() );
控制台应用程序现在启动正常,消息处理器运行正常......但没有发送电子邮件。或者更确切地说,没有任何消息到达,当我检查 SendGrid.com 上的仪表板时,没有任何事件记录。
成功!
我终于让它发挥作用了。我认为“最终”问题是我忽略了我正在创建的电子邮件的发件人地址。一旦我这样做了,它就像一个魅力。
最佳答案
请尝试使用mydemo代码。我用WebJobs.Extensions.SendGrid库,我已经测试过了。发布到 WebApp 后,它在 WebJob 中成功运行。 以下是我的测试代码和package.config文件:
using System;
using System.Threading.Tasks;
using SendGrid.Helpers.Mail;
using SendGrid;
namespace TestWebJob
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Start to run the Execute");
Execute().Wait();
Console.WriteLine($"Task finished");
}
static async Task Execute()
{
string apiKey = "your sendgrid API key";
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c2d0c38293f38212d2520622f2321" rel="noreferrer noopener nofollow">[email protected]</a>");//a test email address
string subject = "Hello World from the SendGrid CSharp Library!";
Email to = new Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="503210243523243d31393c7e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>");// another test email address
Content content = new Content("text/plain", "Hello, Email!");
Mail mail = new Mail(from, subject, to, content);
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
}
}
}
packages.config 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.Azure.WebJobs" version="1.1.1" targetFramework="net452" />
<package id="Microsoft.Azure.WebJobs.Core" version="1.1.1" targetFramework="net452" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="1.0.1" targetFramework="net452" />
<package id="Microsoft.Azure.WebJobs.Extensions.SendGrid" version="1.0.1" targetFramework="net452" />
<package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net452" />
<package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net452" />
<package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net452" />
<package id="Microsoft.Tpl.Dataflow" version="4.5.24" targetFramework="net452" />
<package id="Microsoft.Web.WebJobs.Publish" version="1.0.12" targetFramework="net452" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net452" />
<package id="ncrontab" version="2.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="Sendgrid" version="8.0.5" targetFramework="net452" />
<package id="SendGrid.CSharp.HTTP.Client" version="3.0.0" targetFramework="net452" />
<package id="SendGrid.SmtpApi" version="1.3.1" targetFramework="net452" />
<package id="System.Spatial" version="5.6.2" targetFramework="net452" />
<package id="WindowsAzure.Storage" version="4.3.0" targetFramework="net452" />
</packages>
关于azure - WebJob 未加载 SendGridMail 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40276104/