首页 > 解决方案 > 我是否需要跨多个命名空间安装相同的引用/依赖项来解决此 FileNotFound 异常?(C#)

问题描述

我正在关注有关制作 Tournament Tracker WinForm 应用程序的 Tim Corey 教程(此时此应用程序有帮助 - https://preview.tinyurl.com/yxmyz8h6

我已经到了我们开始使用一些 NuGet 包将类库连接到 SQL 的地步 - 即 Dapper、System.Data.SqlClient 和 System.Configuration.ConfigurationManager。

到目前为止,该项目分为两个命名空间,包含所有模型和数据访问类的类库 (TrackerLibrary) 和表单 UI (TrackerUI)。我从教程中得到的印象是这些引用只需要存在于类库中而不是 UI 中(因为 TrackerLibrary 是 Tim 指示我们添加它们的地方)

但是如果没有在 TrackerUI 中引用它们 - 当您运行代码时,所有三个都会出现 FileNotFoundException。但是,在教程中,这并没有发生在他身上。

SQL 连接字符串在 TrackerUI 的 App.Config 文件中设置,如下所示...

<connectionStrings>
   <add name="Tournaments" 
   connectionString="Server=localhost;Database=TournamentTracker;Trusted_Connection=True;" 
   providerName="System.Data.SqlClient"/>
</connectionStrings>

TrackerUI 中有一个名为 CreatePrizeForm 的类,它有一个按钮单击方法来验证用户完成的表单,然后将该数据转换为模型并将该模型传递给 SQL...

using System;
using System.Windows.Forms;
using TrackerLibrary;
using TrackerLibrary.Models;

namespace TrackerUI
{
public partial class CreatePrizeForm : Form
{
    public CreatePrizeForm()
    {
        InitializeComponent();
    }

    private void createPrizeButton_Click(object sender, EventArgs e)
    {
        if (ValidateForm())
        {
            PrizeModel model = new PrizeModel(
                placeNameValue.Text, 
                placeNumberValue.Text, 
                prizeAmountValue.Text, 
                prizePercentageValue.Text);

            
            GlobalConfig.Connection.CreatePrize(model); 
            

            placeNameValue.Text = "";
            placeNumberValue.Text = "";
            prizeAmountValue.Text = "0";
            prizePercentageValue.Text = "0";

        }

GlobalConfig 类根据教程的假想客户端要求处理解密我们是保存到 SQL 还是保存到文本文件,并获取连接字符串,看起来像这样......

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Text;
    using TrackerLibrary.DataAccess; 
    using System.Data.SqlClient;
    using Dapper;

    public static class GlobalConfig
{
    public static IDataConnection Connection { get; private set; }

    
    public static void InitializeConnections(DatabaseType db)
    {
        if (db == DatabaseType.Sql)
        {
            
            SqlConnector sql = new SqlConnector();
            Connection = sql;
        }

        else if (db == DatabaseType.TextFile)
        {
            
            TextConnector text = new TextConnector();
            Connection = text;
        }
    }

    public static string CnnString(string name)
    {
        return ConfigurationManager.ConnectionStrings[name].ConnectionString;
    }
}

IDataConnection 接口看起来像这样......

using System;
using System.Collections.Generic;
using System.Text;
using TrackerLibrary.Models;

namespace TrackerLibrary.DataAccess 
{

public interface IDataConnection
{
    PrizeModel CreatePrize(PrizeModel model);
}
} 

CreatePrize 方法看起来像这样......

    using Dapper;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using TrackerLibrary.Models;

    public class SqlConnector : IDataConnection
    {
   
    public PrizeModel CreatePrize(PrizeModel model)
    {
        using (IDbConnection connection = new 
            System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString("Tournaments")))
            {
            var p = new DynamicParameters(); // Dapper object
            p.Add("@PlaceNumber", model.PlaceNumber);
            p.Add("@PlaceName", model.PlaceName);
            p.Add("@PrizeAmount", model.PrizeAmount);
            p.Add("@PrizePercentage", model.PrizePercentage);
            p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);

            connection.Execute("dbo.spPrizes_Insert", p, commandType: CommandType.StoredProcedure);

            model.Id = p.Get<int>("@id"); 

            return model;
          
        } 

当代码到达这里时发生错误......

GlobalConfig.Connection.CreatePrize(model);

除了以下例外...

System.IO.FileNotFoundException:'无法加载文件或程序集'System.Data.SqlClient,Version=4.6.1.2,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。该系统找不到指定的文件。'

当我将 System.Data.SqlClient NuGet 包安装到 TrackerUI 的引用中时 - 它与以前一样在同一点出错,但这次它谈论的是 Dapper...

System.IO.FileNotFoundException:'无法加载文件或程序集'Dapper,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null'或其依赖项之一。该系统找不到指定的文件。'

然后,如果您随后将 Dapper 安装到 TrackerUI 引用中,它会通过 GlobalConfig.Connection.CreatePrize 调用到 SqlConnector.cs 和使用错误 (IDbConnection connection = new System.Data.SqlClient.SqlConnection... 下面的行...

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using TrackerLibrary.Models;

namespace TrackerLibrary.DataAccess
{
public class SqlConnector : IDataConnection
{
      public PrizeModel CreatePrize(PrizeModel model)
    {
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString("Tournaments")))
        {
            var p = new DynamicParameters(); // Dapper object
            p.Add("@PlaceNumber", model.PlaceNumber);
            p.Add("@PlaceName", model.PlaceName);
            p.Add("@PrizeAmount", model.PrizeAmount);
            p.Add("@PrizePercentage", model.PrizePercentage);
            p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);

            connection.Execute("dbo.spPrizes_Insert", p, commandType: CommandType.StoredProcedure);

            model.Id = p.Get<int>("@id"); //Pulls ID from the p variable that represents the id of the record in the database

            return model;
          
        } 

与另一个 FileNotFoundException...

System.IO.FileNotFoundException:'无法加载文件或程序集'System.Configuration.ConfigurationManager,Version=4.0.3.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51'或其依赖项之一。该系统找不到指定的文件。'

在 TrackerUI 命名空间中添加对 Dapper、System.Data.SqlClient 和 System.Configuration.ConfigurationManager 的引用可解决异常并使程序能够毫无问题地写入 SQL。我只是想弄清楚这是否是我默认需要做的,或者我之前是否错过了一些东西,TrackerUI 命名空间不应该包含对它们的引用。只是不想养成坏习惯。

抱歉,如果我错过了任何重要的细节 - 这有点新,并试图尽可能彻底,但如果我需要提供其他任何东西,请告诉我。

感谢您的澄清和提前帮助!

标签: c#referencedependencies

解决方案


推荐阅读