简体   繁体   English

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

[英]Error referencing dll in .net standard 2.0 library from .net core console application

I am getting the following error message when attempting to use the SendGrid nuget package in my .net standard 2.0 libary. 尝试在.net标准2.0库中使用SendGrid nuget包时,出现以下错误消息。 This error message is being displayed in my catch of my console application 此错误消息显示在我的控制台应用程序捕获中

Could not load file or assembly 'SendGrid, Version=9.8.0.0, Culture=neutral, PublicKeyToken=4f047e93159395ca'. The system cannot find the file specified.

The console application target framework is: .NET Core 2.0 控制台应用程序目标框架为:.NET Core 2.0

The library dll is referenced in the console application by going to Dependencies -> Add Reference -> Selecting the dll from the bin directory in the library project folder once the library has been built (Eventually this will get moved out to a private nuget feed). 库构建后,在控制台应用程序中通过转至依赖关系->添加引用->从库项目文件夹中的bin目录中选择dll来引用该库dll(最终,该dll将移至私有nuget提要中) 。

The .net core console application code looks like so: .net核心控制台应用程序代码如下所示:

static void Main(string[] args)
{
    try
    {
        MailSender.SendEmail(new EmailRequest { Email = "toEmail@gmail.com" });
    }
    catch (Exception e)
    {

    }
}

This is the Library Target Framework: .NET Standard 2.0 这是库目标框架:.NET Standard 2.0

This is the MailSender method that I am getting the error from: 这是我从以下错误中获取的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;
            }
        }
    }
}

The Sendgrid Nuget package I downloaded is version.9.9 我下载的Sendgrid Nuget软件包是version.9.9

I noticed that in the error message it was referencing version 9.8 so I downloaded the Nuget Package Explorer and saw this for the package in the referenced dll path. 我注意到在错误消息中它引用的是9.8版,因此我下载了Nuget软件包资源管理器,并在引用的dll路径中看到了该软件包。

在此处输入图片说明

Is there some compatibility issue going on between a core 2.0 application and standard 2.0 library? 核心2.0应用程序和标准2.0库之间是否存在一些兼容性问题? I am interested in using standard for compatibility purposes. 我对出于兼容性目的使用标准感兴趣。 Any help as to how to get this resolved is greatly appreciated! 非常感谢您提供有关如何解决此问题的帮助!

EDIT: Also noticed that if I move the console application into the same solution and reference the project instead of the dll, the code works fine. 编辑:还注意到,如果我将控制台应用程序移到相同的解决方案中,并引用项目而不是dll,则代码可以正常工作。 Sadly I am not looking to keep these projects together. 可悲的是,我不想将这些项目放在一起。

Try putting the dll in the console solution, or in a another random folder, this seems like its more of visual studio not recognizing the dll, the compatibility seems to be correct. 尝试将dll放入控制台解决方案或另一个随机文件夹中,这似乎是Visual Studio无法识别dll的原因,兼容性似乎是正确的。 Strange issue. 奇怪的问题。 Also could try re-targeting them temporarily just to see if it is indeed a version compatibility issue. 也可以尝试暂时将它们重新定位,以查看是否确实是版本兼容性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM