WebJob无法加载SendGridMail程序集 [英] WebJob Not Loading SendGridMail Assembly

查看:77
本文介绍了WebJob无法加载SendGridMail程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Azure WebJobs使用SendGrid扩展.我尝试按照该示例进行操作,但是不幸的是,WebJob应用程序崩溃并出现以下错误:

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:

无法加载文件或程序集'SendGridMail,Version = 6.1.0.0, 文化=中性,PublicKeyToken = 2ae73662c35d80e4'或其中之一 依赖关系.系统找不到指定的文件.

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中添加以下内容来解决该问题:

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).

我注意到WebJobs SendGrid扩展nuget软件包安装了旧版本的SendGrid(v6.1).但是我正在尝试遵循WebJob扩展示例,并且它们具有以下using语句:

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;

,不幸的是,SendGrid v6.1中不存在SendGrid.Helpers.Mail命名空间.

and, unfortunately, the SendGrid.Helpers.Mail namespace doesn't exist in v6.1 of SendGrid.

其他详细信息

根据Tom的反馈,我卸载了SendGrid扩展nuget软件包,并通过编辑project.json手动"安装了所需的库.这为我提供了每个库的最新稳定版本...但是该应用程序在启动时仍然崩溃,并出现相同的错误.

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.

这是启动代码:

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中的方法签名:

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 );
}

如果我未在应用启动代码中调用UseSendGrid(),则在解析方法定义时会出现异常,告诉我一定要调用UseSendGrid().但是,如上所述,UseSendGrid()爆炸了.

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.

没有例外,但没有电子邮件

基于Tom的示例,我修改了消息处理功能,以使其不使用out Mail参数,而只是在方法主体内构建和发送电子邮件:

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() );

控制台应用程序现在可以正常启动,并且消息处理器可以正常运行...但是没有发送电子邮件.或者,更确切地说,没有任何信息到达,并且当我在SendGrid.com上检查仪表板时,没有任何活动的记录.

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.

成功!

我终于有了这个工作.我认为最终"问题是我忽略了要创建的电子邮件的发件人"地址.一旦我做到了,它就像一种魅力.

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.

推荐答案

请尝试使用mydemo代码.我使用了 WebJobs.Extensions.SendGrid 库,测试了它.发布到WebApp后,它可以在WebJob中成功工作. 以下是我的测试代码和package.config文件:

Please have a try to use mydemo code. I use the WebJobs.Extensions.SendGrid library,I have tested it. It works successfully in the WebJob after publishing to WebApp. The following is my test code and package.config file:

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文件如下:

The packages.config file is as following:

<?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>

这篇关于WebJob无法加载SendGridMail程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆