I am trying to use dependencyinjection in Controller and using System.Web.MvcDependencyResolver.Current.GetService() for creating service instances in controller class.
This works fine when Service Interfaces are non generic as below
public interface IProfileManagementService
{
IList<News> GetSavedSearchList(int userObjectId, ApplicationType applicationType,int? vendorUserObjectId);
}
and my dependency resolver syntax as below gives me instance of ProfileManagementService
DependencyResolver.Current.GetService<IProfileManagementService>();
But If I create any generic service interface as below,
public interface ICommonProfileManagementService<T>
{
IList<T> GetSavedSearchList(int userObjectId, ApplicationType applicationType,int? vendorUserObjectId);
}
But I get a null (CommonProfileManagementService objects are not created) for below code
DependencyResolver.Current.GetService<ICommonProfileManagementService<News>>();
Please Suggest some alternate ways of passing
IService<T>
instead of
IService
to DependencyResolver.Current.GetService()
Here is the full code including the syntax needed to return a generic from DependencyResolver.
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;
namespace ConsoleApplication2
{
class Program
{
public interface IService<T>
{
List<T> GetService();
}
public class Service<T> : IService<T>
{
public List<T> GetService()
{
return new List<T>();
}
}
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType(typeof(IService<>), typeof(Service<>));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
var service = DependencyResolver.Current.GetService(typeof(IService<string>));
Console.WriteLine(service.ToString());
Console.ReadKey();
}
}
}