【问题标题】:Error referencing dll in .net standard 2.0 library from .net core console application从 .net 核心控制台应用程序引用 .net 标准 2.0 库中的 dll 时出错
【发布时间】:2018-09-06 11:28:04
【问题描述】:

尝试在我的 .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

库 dll 在控制台应用程序中被引用,方法是转到依赖项 -> 添加引用 -> 在库项目文件夹中的 bin 目录中选择 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 包资源管理器并在引用的 dll 路径中看到了包。

核心 2.0 应用程序和标准 2.0 库之间是否存在兼容性问题?我有兴趣将标准用于兼容性目的。非常感谢任何有关如何解决此问题的帮助!

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

【问题讨论】:

  • 是的,您需要将符合 .Net Standard 的代码移出到其他库,并从 .Net 核心应用程序中使用它们,这就是 .Net Standard 的重点。如果您将库存储在 .Net Core 应用程序中,它会针对 .Net Core 而非 .Net 标准进行编译

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2017-11-20
    • 2017-02-10
    • 1970-01-01
    相关资源
    最近更新 更多