我正在尝试使用Azure WebJobs的SendGrid扩展。我试图按照这个例子,但不幸的是,WebJob应用程序崩溃时出现以下错误:
无法加载文件或程序集'SendGridMail,Version = 6.1.0.0, Culture = neutral,PublicKeyToken = 2ae73662c35d80e4'或其中一个 依赖。系统找不到指定的文件。
在涉及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 SendGrid.Helpers.Mail;
,不幸的是,SendGrid的v6.1中不存在SendGrid.Helpers.Mail命名空间。
其他详细信息
根据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("mark@arcabama.com", "Mark Olbert") );
message.AddPersonalization( personalization );
}
如果我没有在app启动代码中调用UseSendGrid(),那么在方法定义解析时会出现异常,告诉我一定要调用UseSendGrid()。但是,正如我上面提到的,UseSendGrid()会爆炸。
没有异常,但没有电子邮件
根据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上检查我的仪表板时,没有任何活动的记录。
成功!
我终于开始工作了。我认为“最终”的问题是,我忽略了为我正在创建的电子邮件添加一个发件人地址。一旦我这样做,它就像一个魅力。
答案 0 :(得分:2)
请尝试使用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@testmail.com");//a test email address
string subject = "Hello World from the SendGrid CSharp Library!";
Email to = new Email("b@testmail.com");// 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>