我的代码有什么问题?显示未处理的异常:System.IO.FileNotFoundException

时间:2018-09-24 05:14:43

标签: c# .net factory-pattern three-tier

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

  

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

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

此处提供代码

ILogin.cs

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

    }

User.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();
    }
}

GetDetails.cs

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

GetValue.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”的字符串,则给出输出,但是当我尝试从对象{{1 }}给出错误。

完整密码Github

任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:1)

从您的FactoryMethod.csproj文件中可以看到,您没有在DataAccessLogic项目中引用FactoryMethod。这就是为什么不会DataAccessLogic.dll复制到bin/Debug/netcoreapp2.0/文件夹,而异常消息确实告诉您的原因。

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

说明:

您将BusinessLogic引用为外部文件依赖项..\BusinessLogic\obj\Debug\netstandard2.0\BusinessLogic.dll),而不是 project-to-project 。这样,.Net CLR对BusinessLogic依赖项一无所知,而只是将该文件 copy-if-newer 复制到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项目中修复引用。 有一个不错的docs about references。看看。

相关问题