从.net核心控制台应用程序引用.net标准2.0库中的dll时出错

时间:2018-03-27 22:19:35

标签: c# .net asp.net-core .net-core .net-standard

尝试在我的.net标准2.0库中使用SendGrid nuget包时,收到以下错误消息。此错误消息显示在我的控制台应用程序中

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

通过转到Dependencies - >在控制台应用程序中引用库dll;添加参考 - >一旦构建了库,就从库项目文件夹中的bin目录中选择dll(最终这将转移到私有nuget feed)。

.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路径中看到了这个包。

enter image description here

核心2.0应用程序和标准2.0库之间是否存在兼容性问题?我有兴趣使用标准来实现兼容性。如何解决这个问题的任何帮助非常感谢!

编辑:还注意到,如果我将控制台应用程序移动到相同的解决方案并引用项目而不是dll,代码工作正常。可悲的是,我不打算将这些项目放在一起。

1 个答案:

答案 0 :(得分:0)

尝试将dll放在控制台解决方案中,或者放在另一个随机文件夹中,这似乎更像是视觉工作室无法识别dll,兼容性似乎是正确的。奇怪的问题。也可以尝试暂时重新定位它们,看它是否确实是版本兼容性问题。

相关问题