Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.web.Mvc DependencyResolver无法为泛型类创建对象_C#_Asp.net Mvc 3_Dependency Injection_Dependency Resolver - Fatal编程技术网

C# System.web.Mvc DependencyResolver无法为泛型类创建对象

C# System.web.Mvc DependencyResolver无法为泛型类创建对象,c#,asp.net-mvc-3,dependency-injection,dependency-resolver,C#,Asp.net Mvc 3,Dependency Injection,Dependency Resolver,我正在尝试在控制器中使用dependencyinjection并使用 System.Web.MvcDependencyResolver.Current.GetService()用于在控制器类中创建服务实例 当服务接口是非泛型的时,这可以很好地工作,如下所示 public interface IProfileManagementService { IList<News> GetSavedSearchList(int userObjectId, ApplicationType ap

我正在尝试在控制器中使用dependencyinjection并使用 System.Web.MvcDependencyResolver.Current.GetService()用于在控制器类中创建服务实例

当服务接口是非泛型的时,这可以很好地工作,如下所示

public interface IProfileManagementService
{
   IList<News> GetSavedSearchList(int userObjectId, ApplicationType applicationType,int? vendorUserObjectId);
}

这里是完整的代码,包括从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();
        }
    }
}
使用Microsoft.Practices.Unity;
使用制度;
使用System.Collections.Generic;
使用System.Web.Mvc;
使用Microsoft.Practices.Unity.Mvc;
命名空间控制台应用程序2
{
班级计划
{
公共接口设备
{
列出GetService();
}
公共课服务:IService
{
公共列表GetService()
{
返回新列表();
}
}
静态void Main(字符串[]参数)
{
var container=new UnityContainer();
RegisterType(typeof(IService),typeof(Service));
SetResolver(新UnitedDependencyResolver(容器));
var service=dependencysolver.Current.GetService(typeof(IService));
Console.WriteLine(service.ToString());
Console.ReadKey();
}
}
}

我尝试了这一点,它说接口名称在此点无效。您在哪里配置DI容器?另外,当您说“它说”时,您的意思是Visual Studio正在报告此错误,因此不允许您编译。或者这是一个运行时错误?它的语法抛出编译时错误。而且我没有配置任何DI容器。DependencyResolver定义了一个扩展方法GetService(),该方法只允许传递接口名。但它不能接受T和接口。我是否需要配置DI容器并对其进行初始化?在appstartSorry中,我的意思是您应该将T替换为要为其创建泛型的实际类型。但是,您还需要实现一个容器并将其注册到DependencyResolver,否则它将始终返回null。我将创建一个示例应用程序并在此处发布代码。您当前的
解析器是什么?如何实际注册接口的实现?
public interface ICommonProfileManagementService<T>
{
   IList<T> GetSavedSearchList(int userObjectId, ApplicationType applicationType,int? vendorUserObjectId);
}
DependencyResolver.Current.GetService<ICommonProfileManagementService<News>>();
IService<T>
IService
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();
        }
    }
}