0

我正在学习 c#,我正在尝试Console Application使用Three Tier Architecturewith进行基本登录Factory Method Design Pattern。我添加了所有层并实现了与登录应用程序相关的所有逻辑。但是当我尝试使用dotnet run命令运行代码时,它要求输入,输入后它给出错误

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集“DataAccessLogic,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”。该系统找不到指定的文件。在 C:\Users\xxx\Desktop\FactoryPatternSample\FactoryMethodSampleApplication\FactoryMethod\Program.cs: 14 中的 FactoryMethod.Program.Main(String[] args) 处的 BusinessLogic.User.getUserName()

尽管该文件存在于 BusinessLogic.User.getUserName();

此处提供的代码

ILogin.cs

public interface ILogin
    {
        bool AttemptLogin();
        string getUserName();

    }

用户.cs

using System;
using DataAccessLogic;
namespace BusinessLogic
{
    class User:ILogin
    {
       private string m_username;
       private string m_password;
       public User(string username, string password)
       {
           this.m_username = username;
           this.m_password = password;
       }
       public bool AttemptLogin()
       {
           if(m_username.Equals("abc") && m_password.Equals("abc123"))
           {
               return true;
           }
           return false;
       }
       public string getUserName()
       {
           IGetDetails objectType = GetDetails.getDetails(m_username);
           Console.WriteLine(objectType.getStudentName());
           return objectType.getStudentName();
       }
    }
}

IGetDetails.cs

using System;

namespace DataAccessLogic
{
    public interface IGetDetails
    {
        string getStudentName();
        string getStudentId();
    }
}

获取详细信息.cs

namespace DataAccessLogic
{
    public class GetDetails
    {
        public static IGetDetails getDetails(string username)
        {
            Console.WriteLine(username);
            IGetDetails objectType = null;
            objectType = new GetValue(username);
            return objectType;
        }
    }
}

获取值.cs

namespace DataAccessLogic
{
    public class GetValue:IGetDetails
    {
        private string m_username = string.Empty;
        public GetValue(string username)
        {
            this.m_username = username;
        }
        public string getStudentName()
        {
            return m_username;
        }
        public string getStudentId()
        {
            return "2205";
        }
    }

在 Program.cs 中

ILogin loginType = Login.attemptLogin(email, password);
        if(loginType.AttemptLogin())
        {
            Console.WriteLine("Name: "+ loginType.getUserName());
        }

在 loginType.getUserName() 中给出错误,如果我将getUserName()方法更改为只返回一个像“hello”这样的字符串,它会给出输出,但是当我尝试从IGetDetails给出错误的对象返回字符串时。

完整源代码Github

任何帮助,将不胜感激。

提前致谢。

4

1 回答 1

1

正如我从您的FactoryMethod.csproj文件中看到的那样,您没有DataAccessLogicFactoryMethod项目中引用。这就是为什么DataAccessLogic.dll不复制到文件夹和异常消息实际上告诉你的原因。bin/Debug/netcoreapp2.0/

您可以手动复制此文件并检查您的应用程序现在是否运行,但这将更好地修复您的引用。

解释:

您引用BusinessLogic外部文件依赖项( ..\BusinessLogic\obj\Debug\netstandard2.0\BusinessLogic.dll),而不是project-to-projectBusinessLogic这样,.Net CLR 对依赖项一无所知,只需将此文件复制FactoryMethod到项目的输出目录即可。

然后,在运行时,CLR 将看到,在Program.cs文件的第 14 行它需要一个GetDetails类,该类在DataAccessLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null汇编中。它试图从一个工作目录加载这个程序集,但没有找到它,所以它抛出System.IO.FileNotFoundException.

解决方案:

始终手动复制此文件修复您的引用,以便该FactoryMethod项目将引用BusinessLogic 项目(而不是文件)。这可以在窗口中的Projects选项卡中完成。Reference Manager

现在 Visual Studio 将构建过时的BusinessLogic项目及其copy-if-newer所有依赖项。

BusinessLogic另外,以相同的方式修复项目中的引用。有一个关于引用的好文档。看一看。

于 2018-09-24T05:59:58.747 回答