0

I'm creating a password encrypter and I'm using dependency injection, but somehow this error comes when running the program.

System.InvalidOperationException: "Unable to resolve service for type 'System.Windows.Forms.TextBox' while attempting to activate 'encryptPasswort.EncryptionProcedureService'."

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

    public EncryptionProcedureService(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 static class Program
{
    [STAThread]
    static void Main()
    {
        var serviceProvider = new ServiceCollection()
          .AddSingleton<IMainForm, MainForm>()
          .AddSingleton<IEncryptionProcedureService, EncryptionProcedureService>()
          .BuildServiceProvider();

        ApplicationConfiguration.Initialize();

        var encryptionProcedureService = serviceProvider.GetRequiredService<IEncryptionProcedureService>();

        var form1 = new MainForm(EncryptionProcedureService);
        Application.Run(form1);
    }
}
3
  • 3
    First of all: code in english. I know, me too, I will hack in some comments in German but really: make a habit. Nothing more annoying in international projects than some name or method or whatnot in some cryptic language and you have no idea what it is supposed to mean (or one of its chars is not even on your keyboard ... ) Commented Oct 12, 2023 at 14:27
  • 1
    You are somehow trying to inject a UI Element into a Service. That can't be right. For one, I am confident TextBox is not registered with your DI and two, getting input is not a business of that particular service (at least it shouldn't). Commented Oct 12, 2023 at 14:30
  • 1
    And maybe work on your encryption algorithm ;P haha , No seriously: I suspect this is going to be some sort of password-safe? If not, I'd have to strongly suggest to not encrypt password at all but salt & pepper & hash (with a strong enough algo) etc. Commented Oct 12, 2023 at 14:36

1 Answer 1

1

The issue is in your constructor. Your service contains only one constructor and that constructor requires the injection of "TextBox" and you are not registering that class. Either replace the textboxes with strings or create a 0 constructor that will be used by default. Replace this public VerschlüsselungsVerfahrenService(TextBox passwortEingabe, TextBox masterKey) { Passwort = passwortEingabe.Text; MasterKey = masterKey.Text; } With this: public VerschlüsselungsVerfahrenService(string passwortEingabe, string masterKey) { Passwort = passwortEingabe; MasterKey = masterKey; }

Sign up to request clarification or add additional context in comments.

Comments