我的WebApi项目中有以下解析器:
config.DependencyResolver = new UnityResolver(container); // HttpConfiguration config
但是在控制台应用程序中,我没有HttpContiguration。如何告诉我的统一容器从控制台应用程序使用此DependencyResolver?
最佳答案
也许有更好的方法,但是我进行注册,然后让容器解析我的“第一类”。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Registering dependencies ...");
var container = new UnityContainer();
container.RegisterType<ProgramStarter, ProgramStarter>(); // Register a class that continues your program.
// Do your registrations.
RegisterTypes(container);
// Let Unity resolve ProgramStarter and create a build plan.
var program = container.Resolve<ProgramStarter>();
Console.WriteLine("All done. Starting program...");
program.Run();
}
}
还有我的
ProgramStarter
类。public class ProgramStarter
{
private readonly IService _service;
public ProgramStarter(IService service)
{
// Unity has created this instance and resolved all dependencies.
_service= service;
}
public void Run()
{
// Do what you want to do.
}
}