WCF容器(IContainer)如何在非WCF项目类中使用?
将Autofac添加到服务项目(在下面的示例中)是唯一的解决方案,或者有任何其他优雅的方法来实现这一点。
以参数形式传递服务是不可行的
任何帮助都将不胜感激。
Wcf服务项目(使用Autofac WCF)
public class DiscountWCFService
{
public IDiscountService service {get; set;}
}服务项目
public class DiscountService : IDiscountService
{
public IDiscountRepository repository {get; set;}
}
public static class DiscountExtension
{
public static IDiscountProcessor GetProcessor(this Discount discount)
{
if(discount.Type=1) return TypeAProcessor();
if(discount.Type=2) return TypeBProcessor();
return null;
}
}
public class TypeAProcessor : IDiscountProcessor
{
// Is it possible to use Autofac?
// E.G. IService fooService = AutofacContainer.Resolve<IFooService>
}
public class TypeBProcessor : IDiscountProcessor
{
}实体项目
public class Discount
{
} 发布于 2016-06-20 11:50:25
在Autofac中使用键登记节省了我的时间。
我的案例的示例解决方案是
builder.RegisterType<TypeAProcessor>().As<IDiscountProcessor>().Keyed<IDataService>(Type.One);
builder.RegisterType<TypeBProcessor>().As<IDiscountProcessor>().Keyed<IDataService>(Type.Two);
builder.Register<Func<Discount, IDiscountProcessor>>(c =>
{
var componentContext = c.Resolve<IComponentContext>();
return (discount) =>
{
var processor = componentContext.ResolveKeyed<IDiscountProcessor>(discount.Type);
return dataService;
};
});https://stackoverflow.com/questions/37916664
复制相似问题