尝试在我的.net标准2.0库中使用SendGrid nuget包时收到以下错误消息 . 此错误消息显示在我的控制台应用程序的catch中
Could not load file or assembly 'SendGrid, Version=9.8.0.0, Culture=neutral, PublicKeyToken=4f047e93159395ca'. The system cannot find the file specified.
控制台应用程序目标框架是:.NET Core 2.0
在构建库之后,通过转到依赖关系 - >添加引用 - >从库项目文件夹中的bin目录中选择dll,在控制台应用程序中引用库dll(最终将移出到私有nuget提要) .
.net核心控制台应用程序代码如下所示:
static void Main(string[] args)
{
try
{
MailSender.SendEmail(new EmailRequest { Email = "toEmail@gmail.com" });
}
catch (Exception e)
{
}
}
这是图书馆目标框架:.NET Standard 2.0
这是我收到错误的MailSender方法:
using EmailUtility.Models;
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Collections.Generic;
namespace EmailUtility
{
public static class MailSender
{
public static void SendEmail(EmailRequest request)
{
var message = new SendGridMessage();
message.SetFrom(new EmailAddress("fromEmail@gmail.com"));
var recipients = new List<EmailAddress>
{
new EmailAddress(request.Email)
};
message.AddTos(recipients);
message.SetSubject("Test Email");
message.AddContent(MimeType.Html, "<h1>Test Email</h1>");
var client = new SendGridClient("APIKEY");
try
{
var response = client.SendEmailAsync(message);
}
catch (Exception e)
{
throw;
}
}
}
}
我下载的Sendgrid Nuget包是version.9.9
我注意到在错误消息中它引用了版本9.8所以我下载了Nuget Package Explorer并在引用的dll路径中看到了这个包 .
核心2.0应用程序和标准2.0库之间是否存在兼容性问题?我有兴趣使用标准进行兼容 . 任何关于如何解决这个问题的帮助非常感谢!
编辑:还注意到,如果我将控制台应用程序移动到相同的解决方案并引用项目而不是dll,代码工作正常 . 可悲的是,我不打算将这些项目放在一起 .
1 回答
尝试将dll放在控制台解决方案中,或者放在另一个随机文件夹中,这似乎更像是视觉工作室没有识别dll,兼容性似乎是正确的 . 奇怪的问题 . 也可以尝试暂时重新定位它们,看看它是否确实是版本兼容性问题 .