You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

依赖注入报错:激活EncryptionProcedureService时无法解析TextBox服务

问题解决:无法解析TextBox服务的依赖注入异常

问题根源

你的EncryptionProcedureService直接在构造函数中依赖TextBox控件,这有两个核心问题:

  • DI容器未注册TextBox类型,无法自动解析并注入该控件
  • 加密服务作为业务逻辑层,直接依赖UI控件违反了单一职责原则,导致代码耦合度高、难以测试和维护

修复步骤

1. 重构加密服务,与UI完全解耦

修改EncryptionProcedureService,让它只接收字符串形式的密码和密钥,彻底移除对UI控件的依赖:

public class EncryptionProcedureService: IVerschlüsselungsVerfahrenService
{
    public string Passwort { get; set; }
    public string MasterKey { get; set; }

    // 改为接收字符串参数,专注加密逻辑
    public EncryptionProcedureService(string passwort, string masterKey)
    {
        Passwort = passwort;
        MasterKey = masterKey;
    }

    public string encryptPasswort()
    {
        string encryptedPasswort = "";

        StringBuilder passwortReader = new StringBuilder(Passwort);
        StringBuilder masterKeyReader = new StringBuilder(MasterKey);

        for (int i = 0; i < passwortReader.Length; i++)
        {
            // 修复原代码索引越界问题:从masterKey最后一个有效索引开始循环
            for (int j = masterKeyReader.Length - 1; j >= 0; j--)
            {
                passwortReader[i] = masterKeyReader[j];
                encryptedPasswort += passwortReader[i];
            }
        }
        return encryptedPasswort;
    }
}

2. 修正DI容器配置与窗体实例化逻辑

在Program中,不要提前从容器获取加密服务(因为密码和密钥需要用户在UI输入后才能确定),改为让MainForm依赖加密服务接口,在需要加密时动态传入UI输入的文本:

public static class Program
{
    [STAThread]
    static void Main()
    {
        var serviceProvider = new ServiceCollection()
          .AddSingleton<IMainForm, MainForm>()
          .AddSingleton<IEncryptionProcedureService>()
          .BuildServiceProvider();

        ApplicationConfiguration.Initialize();

        // 从DI容器获取MainForm实例,而非手动new
        var form1 = serviceProvider.GetRequiredService<IMainForm>();
        Application.Run(form1 as Form);
    }
}

3. 在MainForm中触发加密操作

在MainForm中,当用户执行加密操作(比如点击按钮)时,获取TextBox的输入文本,传入加密服务完成逻辑:

public class MainForm : Form, IMainForm
{
    private readonly IEncryptionProcedureService _encryptionService;
    private TextBox _passwortEingabe;
    private TextBox _masterKey;
    private Button _encryptBtn;

    public MainForm(IEncryptionProcedureService encryptionService)
    {
        _encryptionService = encryptionService;
        InitializeUI();
    }

    private void InitializeUI()
    {
        // 初始化UI控件:_passwortEingabe、_masterKey、_encryptBtn
        _encryptBtn.Click += OnEncryptClick;
    }

    private void OnEncryptClick(object sender, EventArgs e)
    {
        // 用当前UI输入创建服务实例,执行加密
        var encryptService = new EncryptionProcedureService(_passwortEingabe.Text, _masterKey.Text);
        string result = encryptService.encryptPasswort();
        // 处理加密结果,例如显示到界面控件
    }
}

额外提示

如果想进一步优化,可以通过工厂模式封装EncryptionProcedureService的实例创建逻辑,避免在窗体中直接new服务,让依赖注入的使用更规范。

内容的提问来源于stack exchange,提问作者user22498067

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 18:53:17