Asp.net mvc 4 温莎Web服务注入属性

Asp.net mvc 4 温莎Web服务注入属性,asp.net-mvc-4,castle-windsor,Asp.net Mvc 4,Castle Windsor,我有一个MVC应用程序,并将我的存储库注入到我的控制器中,使其正常工作。 此外,我的解决方案中有一个Webservice,它使用完全相同的存储库,但当调用我的Webservice时,我的存储库属性为null 我通过以下方式注册我的存储库: container.Register(Classes.FromAssembly(Assembly.GetAssembly(typeof(HdtRepository))).InSameNamespaceAs<HdtRepository>().With

我有一个MVC应用程序,并将我的存储库注入到我的控制器中,使其正常工作。 此外,我的解决方案中有一个Webservice,它使用完全相同的存储库,但当调用我的Webservice时,我的存储库属性为null

我通过以下方式注册我的存储库:

container.Register(Classes.FromAssembly(Assembly.GetAssembly(typeof(HdtRepository))).InSameNamespaceAs<HdtRepository>().WithService.DefaultInterfaces().LifestyleTransient());
有人能告诉我为什么存储库没有注入到我的Web服务中吗

目前,我在Web服务的构造函数中添加了以下内容:

 public MyWebservice()
     {
        _userRepo = MvcApplication.container.Resolve<IUserRepository>();
        _hdtRepo = MvcApplication.container.Resolve<IHdtRepository>();
        _timeRepo = MvcApplication.container.Resolve<ITimeRecordRepository>();
        _locationRepo = MvcApplication.container.Resolve<ILocationRepository>();
        _wayRepo = MvcApplication.container.Resolve<IWayPointRepository>();
        _wayDataRepo = MvcApplication.container.Resolve<IWayDataRepository>();
    }
publicMyWebService()
{
_userRepo=mvcapapplication.container.Resolve();
_hdtRepo=mvcapapplication.container.Resolve();
_timeRepo=mvcapapplication.container.Resolve();
_locationRepo=mvcapapplication.container.Resolve();
_wayRepo=mvcapapplication.container.Resolve();
_wayDataRepo=mvcapapplication.container.Resolve();
}
但据我所知,这实际上是一个反模式。 我对国际奥委会的事情都不熟悉,所以有人能告诉我问题出在哪里吗

干杯,
Stefan

首先让我们使用一些Windsor安装程序来设置您的项目。它们大部分看起来都是这样的

  public class ServiceInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
               container.Register(Component.For<IEncryptionService().ImplementedBy<EncryptionService>());

    }
}
现在,在控制器中,您可以执行以下操作:

 public class TempController : Controller
{
    private readonly IEncryptionService _encryptionService;

    public TempController(IEncryptionService encryptionService )
    {
        _encryptionService = encryptionService;
    }

    public ActionResult Index()
    {
        // Example of calling a method on the encryption service.
        string hash, salt;
        _encryptionService.GethashAndSaltString("I Need Some Loving", out hash, out salt);
        return View();
    }
}

请让我知道,如果你得到一些与构造函数注入工作。解决这个问题将是一个巨大的帮助,你将不会使用属性注入。一旦我们解决了所有这些问题,我们就可以看看你的网络服务问题。

我想这是不可能的,因为我读过一些其他的帖子。 问题是您无法为控制器的“WindsorController factory”等Web服务创建自定义工厂。 我要转到WCF服务


为什么要使用属性注入?您应该使用构造函数注入。同意ctor注入注释。此外,您可能希望尝试通过接口而不是类型向windsor容器注册组件。这样可以控制组件的生活方式。现在,您的注册码声明所有内容都是单例的,如果没有由另一个lifestlye明确定义,这是默认设置;trainsient或perwebrequest。很抱歉这个问题,但为什么财产注入那么糟糕?这不是完全一样吗?由于c#3.0,我可以使用:Test asdf=new Test(){Prop1=1234,Prop2=“asdf”},并且不再需要很多不同的构造函数。当我使用此代码注册存储库时,我的控制器属性也不再被注入:container.register(Component.for(typeof(IRepository)).ImplementedBy(typeof(RepositoryBase))。生活方式。暂时性);我实现它的方式与本文中描述的id完全相同:UoW的东西除外,因为这打破了懒散地分享代码的愿望,但我认为我已经拥有了这些东西。我基于本文实现了我的应用程序:好的,我在我的控制器和Web服务中改为构造函数注入。我想现在我必须更改“WebserviceFactory”如果有类似的内容,就像我对“ControllerFactory”所做的一样因为只支持无参数构造函数。哈哈…绝对不是!这与我的问题无关!!我不确定你的评论。我想我的回答对你有帮助。你切换到构造函数注入,而不是使用你发布的糟糕代码。然后你留下了一个不自然的回答。很好,非常专业。我们从未联系过你r webservice,因为你从来没有回复过。这太荒谬了。你只是那种不真正阅读文章,只想通过发布与所问问题无关的内容来获得分数的人……我要问管理员是否应该将你的回复标记为答案……好吗?
  public class ContainerConfig
{
    private static IWindsorContainer _container;

    public static IWindsorContainer ConfigureContainer()
    {
        _container = new WindsorContainer();
        _container.Install(FromAssembly.This()).Install(FromAssembly.Named("Project.Dependency"));
        _container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel, true));
        _container.AddFacility<TypedFactoryFacility>();

        var controllerFactory = new WindsorControllerFactory(_container.Kernel);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        return _container;
    }
}
protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        ContainerConfig.ConfigureContainer();

    }
 public class TempController : Controller
{
    private readonly IEncryptionService _encryptionService;

    public TempController(IEncryptionService encryptionService )
    {
        _encryptionService = encryptionService;
    }

    public ActionResult Index()
    {
        // Example of calling a method on the encryption service.
        string hash, salt;
        _encryptionService.GethashAndSaltString("I Need Some Loving", out hash, out salt);
        return View();
    }
}