我下载了Unity.MVC 5.9.2,但是我的控制器遇到了我需要的新依赖项时遇到了问题。根据我的读物,我要做的就是在RegisterTypes方法中注册类型,其余的由Unity负责
我收到的错误:
无法为System.String选择构造函数 说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
异常详细信息:System.InvalidOperationException:无法为System.String选择构造函数
我已经阅读了所有相关教程,但仍然不知道我在这里缺少什么
代码:
/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public static class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container =
new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
/// <summary>
/// Configured Unity Container.
/// </summary>
public static IUnityContainer Container => container.Value;
#endregion
/// <summary>
/// Registers the type mappings with the Unity container.
/// </summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>
/// There is no need to register concrete types such as controllers or
/// API controllers (unless you want to change the defaults), as Unity
/// allows resolving a concrete type even if it was not previously
/// registered.
/// </remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below.
// Make sure to add a Unity.Configuration to the using statements.
// container.LoadConfiguration();
// TODO: Register your type's mappings here.
container.RegisterType<ILoader, Loader>();
}
}
public class SomeController : Controller
{
private ILoader _loader;
public SomeController (ILoader loader)
{
_loader = loader;
}
[HttpGet]
public async Task<ActionResult> GetBatches()
{
try
{
return Json("", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable, "Unable to get batches for recurring mail.");
}
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterTypes(UnityConfig.Container);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public static class UnityMvcActivator
{
/// <summary>
/// Integrates Unity when the application starts.
/// </summary>
public static void Start()
{
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(UnityConfig.Container));
DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Container));
// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
/// <summary>
/// Disposes the Unity container when the application is shut down.
/// </summary>
public static void Shutdown()
{
UnityConfig.Container.Dispose();
}
}