Путь к файлу контейнера Unity не передается конструктору - PullRequest
0 голосов
/ 19 июня 2019

Я пытаюсь внедрить TextLogger в приложение, однако я пытаюсь понять, как вставить фактический путь к файлу в конструктор.

Ниже приведены мои ILogWriter и TextLoggerкласс

ILogWriter:

public interface ILogWriter
{
    void WriteLog (string message);
}

TextLogger:

public class TextLogger : ILogWriter
{
    [InjectionConstructor]
    public TextLogger (string filePath)
    {
        this.FilePath = filePath;
        this.WriteLog ("test");
    }

    public string FilePath { get; set; }

    public void WriteLog (string message)
    {
        using ( var writer = File.AppendText(this.FilePath))
        {
            writer.WriteLine(message);
        }
    }
}

Я еще не внедрил его, потому что мой код ломается во время построения:

public partial class App : Application
{
    /// <summary>
    /// Startup Logic for App
    /// </summary>
    /// <param name="e"></param>
    protected override void OnStartup (StartupEventArgs e)
    {
        base.OnStartup(e);

        // Dependency Injection
        IUnityContainer container = new UnityContainer();

        // Logging
        var filePath = "C:/mypath/testing.txt";
        container.RegisterType<ILogWriter, TextLogger>(new InjectionConstructor(filePath));

        // It will be injected into my LoggingService, but breaks just before when writing the test message
        container.RegisterType<ILoggingService, LoggingService>();

        // Notifications
        container.RegisterType<INotificationService, NotificationService>();

        // Data Contexts
        container.RegisterType<IViewModelLocator, ViewModelLocator>();
        container.RegisterType<MainWindow>();
        container.Resolve<MainWindow>().Show();
    }
}

При запуске возникает следующая ошибка:

Значение не может быть равно нулю

И когда я проверяю переменную FilePath, оно равно нулю.Как я могу передать путь к файлу в мой TextLogger класс?

...