{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Mvc/issues/6014","repository_url":"https://api.github.com/repos/aspnet/Mvc","labels_url":"https://api.github.com/repos/aspnet/Mvc/issues/6014/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Mvc/issues/6014/comments","events_url":"https://api.github.com/repos/aspnet/Mvc/issues/6014/events","html_url":"https://github.com/aspnet/Mvc/issues/6014","id":216129527,"number":6014,"title":"Model bind to Interface via DI","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars2.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-03-22T16:33:04Z","updated_at":"2017-03-22T17:13:23Z","closed_at":"2017-03-22T16:47:12Z","body":"### Requirement\r\n\r\nI'd like to bind to a model interface via DI but it doesn't seem to work? Am I doing something wrong?\r\n\r\n### Steps to reproduce\r\n\r\nModels\r\n```csharp\r\npublic interface IMyModel\r\n{\r\n    string Name { get; set; }\r\n    int Count { get; set; }\r\n}\r\n\r\npublic class MyModel : IMyModel\r\n{\r\n    public string Name { get; set; }\r\n    public int Count { get; set; }\r\n}\r\n```\r\nConfigureServices\r\n```csharp\r\npublic void ConfigureServices(IServiceCollection services)\r\n{\r\n    services.AddTransient<IMyModel>((provider) => new MyModel());\r\n    services.AddMvc();\r\n}\r\n```\r\nController Method\r\n```csharp\r\npublic IActionResult Contact(IMyModel model)\r\n{\r\n    ViewData[\"Message\"] = $\"{model.Name} {model.Count}\";\r\n\r\n    return View();\r\n}\r\n```\r\n\r\n### Expected behavior\r\n\r\nTransient object obtained from DI; then its properties set as per non DI type\r\n\r\n### Actual behavior\r\n\r\n```\r\nfail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]\r\n      An unhandled exception has occurred: Could not create an instance of type 'mvc.IMyModel'. \r\nModel bound complex types must not be abstract or value types and must have a parameterless constructor.\r\nSystem.InvalidOperationException: Could not create an instance of type 'mvc.IMyModel'. Model bound complex types must not be abstract or value types and must have a parameterless constructor.\r\n   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)\r\n   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.<BindModelCoreAsync>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindModelAsync>d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindArgumentsCoreAsync>d__6.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)\r\n   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()\r\n```\r\n\r\n/cc @DamianEdwards @rynowak "},"comment":{"url":"https://api.github.com/repos/aspnet/Mvc/issues/comments/288471434","html_url":"https://github.com/aspnet/Mvc/issues/6014#issuecomment-288471434","issue_url":"https://api.github.com/repos/aspnet/Mvc/issues/6014","id":288471434,"user":{"login":"rynowak","id":1430011,"avatar_url":"https://avatars2.githubusercontent.com/u/1430011?v=3","gravatar_id":"","url":"https://api.github.com/users/rynowak","html_url":"https://github.com/rynowak","followers_url":"https://api.github.com/users/rynowak/followers","following_url":"https://api.github.com/users/rynowak/following{/other_user}","gists_url":"https://api.github.com/users/rynowak/gists{/gist_id}","starred_url":"https://api.github.com/users/rynowak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rynowak/subscriptions","organizations_url":"https://api.github.com/users/rynowak/orgs","repos_url":"https://api.github.com/users/rynowak/repos","events_url":"https://api.github.com/users/rynowak/events{/privacy}","received_events_url":"https://api.github.com/users/rynowak/received_events","type":"User","site_admin":false},"created_at":"2017-03-22T17:13:23Z","updated_at":"2017-03-22T17:13:23Z","body":"> then its properties set as per non DI type\r\n\r\n`[FromServices]` does not do this."}}
{"actor":{"display_login":"rynowak"}}