Dependency Injection (DI) in ASP.NET Core/MVC 6 ViewModel
<p>I'm successfully using ASP.NET 5/MVC 6 DI in my controllers using Constructor Injection.</p>

<p>I now have a scenario where I want my View Models to utalise a service in the Validate method when implementing the IValidatableObject.</p>

<p>Constructor injection in the ViewModel does not work because they need a default parameterless constructor. Validation Context.GetService does not work either.</p>

<pre><code>    public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
    {
        MyService myService = (MyService)validationContext.GetService(typeof(MyService));
</code></pre>

<p>always results in MyService being null.</p>

<p>ASP.NET 4, I would create ValidatableObjectAdapter, register it via  DataAnnotationsModelValidatorProvider.RegisterDefaultValidatableObjectAdapterFactory &amp; then I could use the validationContext to object references to services.</p>

<p>I'm currently using the build in DI container for ASP.NET 5, will move to structuremap at some stage) not that this should matter.</p>

<p>My specific validation is that an object's property (eg, user name) is unique. I want to delegate this test to the service layer.</p>