4

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()

1
  • What is your current Resolver? How do you actually register the implementation of the interface? Commented Jun 23, 2014 at 13:22

1 Answer 1

3

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this and it says interface name is not valid at this point
Where are you configuring your DI container?
Also, when you say, 'it says' do you mean that Visual Studio is reporting this error and hence not allowing you to compile. Or is that a run time error?
With the syntax it throws compile time error.Also I didnt configure any DI container. . DependencyResolver has defined an extension method GetService () that allows to pass interface name only.but it can not accept T along with the interface. Do I need to configure DI container and initialize that in appstart
Sorry, I meant that you should replace T with the actual type that you want to create the generic for. However you also need to implement a container and register that with DependencyResolver otherwise it will always return null. I will create a sample app and post the code here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.