C# StructureMap ASP.net MVC未注入接口
我已经尝试让结构映射依赖关系正常工作,但在将接口放入构造函数而不是放入类名时,它无法正常工作 以下代码起作用:C# StructureMap ASP.net MVC未注入接口,c#,asp.net-mvc-4,structuremap,C#,Asp.net Mvc 4,Structuremap,我已经尝试让结构映射依赖关系正常工作,但在将接口放入构造函数而不是放入类名时,它无法正常工作 以下代码起作用: public class HomeController : Controller { private readonly MyService _service; public HomeController(MyService service) { _service = service; } public ActionResult
public class HomeController : Controller
{
private readonly MyService _service;
public HomeController(MyService service)
{
_service = service;
}
public ActionResult Index()
{
return View();
}
}
public class MyService : IService
{
public string GetName()
{
return "Hello";
}
}
public interface IService
{
string GetName();
}
但以下代码不起作用:
public class HomeController : Controller
{
private readonly IService _service;
public HomeController(IService service)
{
_service = service;
}
public ActionResult Index()
{
return View();
}
}
public class MyService : IService
{
public string GetName()
{
return "Hello";
}
}
public interface IService
{
string GetName();
}
以下是DependencyResolution类的逻辑:
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
// x.For<IExample>().Use<Example>();
});
return ObjectFactory.Container;
}
publicstaticontainer初始化(){
ObjectFactory.Initialize(x=>
{
x、 扫描(扫描=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
});
//x.For().Use();
});
返回ObjectFactory.Container;
}
我正在使用StructureMap.MVC4 nuget包来设置依赖项注入。我做错了什么?在调用程序集中,如果您只有一个表示接口的实现类,您可以像下面这样使用
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
如果没有SingleImplementationsOfInterface()方法,structuremap无法识别IService接口的正确实现类
或
你可以像下面这样绘制地图
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
ObjectFactory.Initialize(x=>
{
x、 扫描(扫描=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
});
x、 For().Use();
});
在调用程序集中,如果只有一个表示接口的实现类,则可以像下面这样使用
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
如果没有SingleImplementationsOfInterface()方法,structuremap无法识别IService接口的正确实现类
或
你可以像下面这样绘制地图
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
ObjectFactory.Initialize(x=>
{
x、 扫描(扫描=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
});
x、 For().Use();
});
在调用程序集中,如果只有一个表示接口的实现类,则可以像下面这样使用
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
如果没有SingleImplementationsOfInterface()方法,structuremap无法识别IService接口的正确实现类
或
你可以像下面这样绘制地图
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
ObjectFactory.Initialize(x=>
{
x、 扫描(扫描=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
});
x、 For().Use();
});
在调用程序集中,如果只有一个表示接口的实现类,则可以像下面这样使用
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
如果没有SingleImplementationsOfInterface()方法,structuremap无法识别IService接口的正确实现类
或
你可以像下面这样绘制地图
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.SingleImplementationsOfInterface();
});
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IService>().Use<MyService>();
});
ObjectFactory.Initialize(x=>
{
x、 扫描(扫描=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
});
x、 For().Use();
});
试试这段代码:
public class MvcBootStrapper
{
public static void ConfigurationStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<MyService>();
});
}
}
公共类MvcBootStrapper
{
公共静态void配置StructureMap()
{
ObjectFactory.Initialize(x=>
{
x、 AddRegistry();
});
}
}
最后,像这样注册类和接口:
public class SampleRegistery : Registry
{
public SampleRegistery ()
{
ForRequestedType<IService>().TheDefaultIsConcreteType<MyService>();
}
}
公共类采样器注册表:注册表
{
公共登记处()
{
ForRequestedType()。默认的ConcreteType();
}
}
有关更多信息,请参阅文章。请尝试以下代码:
public class MvcBootStrapper
{
public static void ConfigurationStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<MyService>();
});
}
}
公共类MvcBootStrapper
{
公共静态void配置StructureMap()
{
ObjectFactory.Initialize(x=>
{
x、 AddRegistry();
});
}
}
最后,像这样注册类和接口:
public class SampleRegistery : Registry
{
public SampleRegistery ()
{
ForRequestedType<IService>().TheDefaultIsConcreteType<MyService>();
}
}
公共类采样器注册表:注册表
{
公共登记处()
{
ForRequestedType()。默认的ConcreteType();
}
}
有关更多信息,请参阅文章。请尝试以下代码:
public class MvcBootStrapper
{
public static void ConfigurationStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<MyService>();
});
}
}
公共类MvcBootStrapper
{
公共静态void配置StructureMap()
{
ObjectFactory.Initialize(x=>
{
x、 AddRegistry();
});
}
}
最后,像这样注册类和接口:
public class SampleRegistery : Registry
{
public SampleRegistery ()
{
ForRequestedType<IService>().TheDefaultIsConcreteType<MyService>();
}
}
公共类采样器注册表:注册表
{
公共登记处()
{
ForRequestedType()。默认的ConcreteType();
}
}
有关更多信息,请参阅文章。请尝试以下代码:
public class MvcBootStrapper
{
public static void ConfigurationStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry<MyService>();
});
}
}
公共类MvcBootStrapper
{
公共静态void配置StructureMap()
{
ObjectFactory.Initialize(x=>
{
x、 AddRegistry();
});
}
}
最后,像这样注册类和接口:
public class SampleRegistery : Registry
{
public SampleRegistery ()
{
ForRequestedType<IService>().TheDefaultIsConcreteType<MyService>();
}
}
公共类采样器注册表:注册表
{
公共登记处()
{
ForRequestedType()。默认的ConcreteType();
}
}
有关更多信息,请参阅文章。在设置StructureMap绑定的地方发布代码?我想我应该使用x.for().Use();如果引用的程序集中只有一个MyService,有没有办法让它假设MyService?@Jonathan-WithDefaultConventions()应该为您这样做,所以我不确定为什么没有。也许它们也必须在同一个名称空间中?@mmillican我可以