控制台应用程序中的Unity Config错误

时间:2016-04-07 11:44:56

标签: c# .net dependency-injection console-application unity-container

您正在使用依赖注入模式的控制台应用程序,我创建了像

这样的界面
  public interface IRecurringTransactionPlanDataService : IService<RecurringTransactionPlan>
  {
    IQueryable<RecurringTransactionPlan> RecurringTransactionPlanData();
  }

并且该类实现为

 public class RecurringTransactionPlanDataService : Service<RecurringTransactionPlan>, IRecurringTransactionPlanDataService
{
    private readonly IRepositoryAsync<RecurringTransactionPlan> _repository;

    public RecurringTransactionPlanDataService(IRepositoryAsync<RecurringTransactionPlan> repository)
        : base(repository)
    {
        _repository = repository;
    }

    public IQueryable<RecurringTransactionPlan> RecurringTransactionPlanData()
    {
        return _repository.RecurringTransactionPlanData();
    }
}

存储库:

 public static class RecurringTransactionPlanRepository
{
    public static IQueryable<RecurringTransactionPlan> RecurringTransactionPlanData(this IRepository<RecurringTransactionPlan> repository)
    {
        return repository.Queryable();
    } 
}

以上代码都在单独的项目中,我在MVC和控制台应用程序中使用它,而在MVC中使用时,UnityConfig.cs从db中获取数据没有错误,但在控制台应用程序中我们需要手动注册和解析接口,我试过这个,

我的控制台应用程序:

 public class RecurringTransaction
{
    public readonly IRecurringTransactionPlanDataService _recurringTransactionPlanDataService;

    public RecurringTransaction()
    { 
        var container = new UnityContainer();
        container.RegisterType<IRecurringTransactionPlanDataService, RecurringTransactionPlanDataService>();
        _recurringTransactionPlanDataService = container.Resolve<IRecurringTransactionPlanDataService>();

    } 
}

public class Program
{
    public static void Main(string[] args)
    {

        FeePaymentTracker.UnityConfig.RegisterComponents();

        RecurringTransaction rt = new RecurringTransaction();
        var restult = rt.GetRecurringTransactionRecords();
    }
}

enter image description here

我收到了上述错误。期待您的想法解决错误。

2 个答案:

答案 0 :(得分:1)

在您发布的代码中,您有一个名为IPaymentService的接口及其实现。好像没问题。但是在屏幕截图中,您正在尝试解决名为RecurringTransactionPlanDataService的依赖项。让你已经注册了这个依赖项。您能否添加有关如何在容器中注册依赖项的信息?

代码中的问题是您正在尝试解决实现而不是接口。您应该更改以下行:

_recurringTransactionPlanDataService = container.Resolve<RecurringTransactionPlanDataService>();

与此:

_recurringTransactionPlanDataService = container.Resolve<IRecurringTransactionPlanDataService>();

干杯,

答案 1 :(得分:1)

在您的RecurringTransaction - 方法中,您创建了一个新容器,然后在该新容器中注册RecurringTransactionPlanDataService。但是,您没有注册实现RecurringTransactionPlanDataService具有的依赖项。该容器只有一个注册。

var container = new UnityContainer();
container.RegisterType<IRecurringTransactionPlanDataService, RecurringTransactionPlanDataService>();
_recurringTransactionPlanDataService = container.Resolve<IRecurringTransactionPlanDataService>();

由于RecurringTransactionPlanDataService依赖于IRepositoryAsync<RecurringTransactionPlan>,因此您也需要注册该内容。

将您的代码更改为:

var container = new UnityContainer();
container.RegisterType<IRecurringTransactionPlanDataService, RecurringTransactionPlanDataService>();
container.RegisterType<IRepositoryAsync<RecurringTransactionPlan>, YourRepositoryImplementation>();
_recurringTransactionPlanDataService = container.Resolve<IRecurringTransactionPlanDataService>();

作为旁注,您可能希望重复使用同一个容器。在控制台应用程序中,我通常会解析一个&#34; ProgramStarter&#34;然后进行正确的注入。这样,您只需要在根目录中使用服务定位器反模式。但是可以在应用程序的其余部分使用适当的DI。

class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();
        container.RegisterType<ProgramStarter, ProgramStarter>();

        // Pass the same container to the config.
        FeePaymentTracker.UnityConfig.RegisterComponents(container);
        var program = container.Resolve<ProgramStarter>();
        program.Run();
    }
}

public class ProgramStarter
{
    IRecurringTransactionPlanDataService _dataService;
    public ProgramStarter(IRecurringTransactionPlanDataService dataService)
    {
        _dataService = dataService;
    }

    public void Run()
    {
        // Do stuff.
    }
}