windows 依赖性注入错误:尝试激活[重复]时无法解析类型的服务

hwamh0ep  于 2023-10-22  发布在  Windows
关注(0)|答案(1)|浏览(94)

此问题已在此处有答案

Dependency Injection error: Unable to resolve service for type while attempting to activate, while class is registered(31个回答)
9天前关闭
我正在创建一个passwort加密器,并使用Dependency Injection,但错误来了

System.InvalidOperationException:“在尝试激活”passwortVerschlueseln. VerschlüsselungsVerfação Service“时,无法解析类型”System.Windows.Forms.TextBox“的服务。"

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

       public VerschlüsselungsVerfahrenService(TextBox passwortEingabe, TextBox masterKey)
       {
           Passwort = passwortEingabe.Text;
           MasterKey = masterKey.Text;
       }

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

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

           for (int i = 0; i < passwortReader.Length; i++)
           {
               for (int j = masterKeyReader.Length; j > 0; j--)
               {
                   passwortReader[i] = masterKeyReader[j];
                   encryptedPasswort += passwortReader[i];
               }
           }
           return encryptedPasswort;
       }
   }
public partial class MainForm : Form, IMainForm
{
    public IVerschlüsselungsVerfahrenService _verschlüsselung;

    public MainForm(IVerschlüsselungsVerfahrenService verschlüsselung)
    {
        _verschlüsselung = verschlüsselung;
        InitializeComponent();
    }

    public void passwortEingabe_TextChanged(object sender, EventArgs e)
    {

        _verschlüsselung.Passwort = passwortEingabe.Text;
    }

    public void masterKeyEingabe_TextChanged(object sender, EventArgs e)
    {
        _verschlüsselung.MasterKey = masterKeyEingabe.Text;
    }

    private void GenerateResultButton_Click(object sender, EventArgs e)
    {
        ErgebnisBox.Text = _verschlüsselung.encryptPasswort();
    }
}
internal static class Program
 {
     /// <summary>
     ///  The main entry point for the application.
     /// </summary>
     [STAThread]
     static void Main()
     {
         var serviceProvider = new ServiceCollection()
           .AddSingleton<IMainForm, MainForm>()
           .AddSingleton<IVerschlüsselungsVerfahrenService, VerschlüsselungsVerfahrenService>()
           .BuildServiceProvider();

         // To customize application configuration such as set high DPI settings or default font,
         // see https://aka.ms/applicationconfiguration.
         ApplicationConfiguration.Initialize();

         var verschlüsselungsVerfahrenService = serviceProvider.GetRequiredService<IVerschlüsselungsVerfahrenService>();

         var form1 = new MainForm(verschlüsselungsVerfahrenService);
         Application.Run(form1);
     }
 }

试着自己解决和谷歌。
请忽略德语和英语的混合。我正在努力

mlnl4t2r

mlnl4t2r1#

问题出在你的构造函数上。您的服务只包含一个构造函数,该构造函数需要注入“TextBox”,而您没有注册该类。或者用字符串替换文本框,或者创建一个默认使用的0构造函数。将public VerschlüsselungsVerfahrenService(TextBox passwortEingabe, TextBox masterKey) { Passwort = passwortEingabe.Text; MasterKey = masterKey.Text; }替换为:public VerschlüsselungsVerfahrenService(string passwortEingabe, string masterKey) { Passwort = passwortEingabe; MasterKey = masterKey; }

相关问题