English 中文(简体)
使用海委会集装箱进行的WCF属性注射
原标题:WCF Attribute injection using an IOC Container

使用 APSP.NET MVC I 能够替换过滤器 Provider 。

 var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
 FilterProviders.Providers.Remove(oldProvider);
 FilterProviders.Providers.Add(new CustomFilterProvider(_container));

使用我自己的自定义提供者。 它不赋予我使用工厂模式创建控制器过滤属性的能力, 但我确实能够使用输入属性来设定属性使用容器可能需要的依附性 。

能否用周转基金来做类似的事情, 以便我可以将依附性(财产注射是罚款) 注入用户定义的类别, 这些类别来自Attraitte , 我用在服务方法上(这些服务是使用海洋学委员会创建的)?

我使用Castle Windsors Wcf Fac Facity, 但一般解决方案(适用于任何容器)或许会更好。

最佳回答

一种办法就是使用集装箱OnCreate 方法或类似方法,在登记时采取以下措施:

  Container.Register(Component.For<IMyService>().ImplementedBy<MyService>().OnCreate(WireUpAttibutes).LifeStyle.Transient);

然后有以下方法

  private static void WireUpAttibutes<T>(IKernel kernel, T instance) {
     var attributes = instance.GetType().GetCustomAttributes(typeof(MyAttribute), false);
     foreach (var attribute in attributes) {
        WireUp(kernel, attribute.GetType(), attribute);
     }
  }

  private static void WireUp(IKernel kernel, Type type, object instance) {
     var properties = type.GetProperties().Where(p => p.CanWrite && p.PropertyType.IsPublic);
     foreach (var propertyInfo in properties.Where(propertyInfo => kernel.HasComponent(propertyInfo.PropertyType))) {
        propertyInfo.SetValue(instance, kernel.Resolve(propertyInfo.PropertyType), null);
     }
  }
问题回答

暂无回答




相关问题
WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签