【问题标题】:Resolving generics with Castle Windsor in WCF在 WCF 中使用 Castle Windsor 解析泛型
【发布时间】:2015-06-23 12:53:48
【问题描述】:

我花了一点时间来完成以下设计,希望得到一些反馈 - 我正在使用 Windsor 来属性注入一个供 Wcf 服务使用的类型。

我的界面最初看起来像这样,其中响应和请求类型是抽象的:

public interface IFooHandler
{
    CheckFooResponse CheckFoo(CheckFooRequest request);
}

我担心我无法控制在实现 IFooHandler 的类中使用抽象类的哪些实现,因此设计更改为:

SO Question 1

public interface ICheckFooHandler<TRequest, TResponse> 
    where TRequest : CheckFooRequest where TResponse : CheckFooResponse
{
    TResponse CheckFoo(TRequest request);
}

及用法:

public class CheckFooHandler : ICheckFooHandler<MyFooRequest, MyFooResponse>
{
    MyFooResponse CheckFoo(MyFooRequest request) { ... }
}

然后我尝试给温莎接线:

IoCContainer = new WindsorContainer();

IoCContainer.Register
(Component
.For(typeof(ICheckFooHandler<,>))
.ImplementedBy(typeof(CheckFooHandler<,>)));

直到我将 CheckFooHandler 的实现更改为:

SO Question 2

public class CheckFooHandler<TRequest, TResponse> :  ICheckFooHandler<TRequest, TResponse>
    where TRequest : MyCheckFooRequest 
    where TResponse : MyCheckFooResponse, new()
{
    public TResponse CheckFoo(TRequest request)
    {
        ...
        var response = new TResponse();
        response.MyResponseProperty = "baz";
    }

它满足通用接口契约,同时允许 Windsor 的通用语法工作。

在我的 Wcf 服务中,我有以下由 Windsor 注入的私有属性:

private ICheckFooHandler<MyCheckFooRequest, MyCheckFooResponse> _checkFooHandler;

private ICheckFooHandler<MyCheckFooRequest, MyCheckFooResponse> CheckFooHandler
{
    get
    {
        if (_checkFooHandler == null)
        {
            _checkFooHandler = Global.IoCContainer
                .Resolve<ICheckFooHandler<MyFooRequest, MyFooResponse>>();
        }
    }
}

这种设计让我在处理程序中编译时安全,并允许我按照前面的问题使用 IoC。

我的主要问题是必须使用具体的 MyFoo 类型而不是 Resolve()、属性和字段签名中的抽象类型。我希望能够指定具体类型一次,然后让 Windsor 完成其余的工作。

最后我从未使用过 new() 泛型约束,我的用法在具体的 CheckFoo() 中是否有效?

【问题讨论】:

    标签: c# wcf generics castle-windsor


    【解决方案1】:

    您没有提供如何设置 Wcf 服务的信息,因此假设您没有使用 Castle Windsor。

    您可以使用 Wcf Facility 完全设置您的应用程序运行时及其 Wcf 服务,由 Castle Windsor 容器提供。

    例子:

        container.AddFacility<WcfFacility>();
    
        foreach (var wcfType in types.Where(t => t.IsInterface == false && t.IsAbstract == false)) // types should be coming from AppDomain.Current assemblies
        {
            foreach (var anInterface in wcfType.GetInterfaces().Where(anInterface => anInterface
                .GetCustomAttributes(typeof(ServiceContractAttribute), true).Any()))
            {
                var serviceModel = new DefaultServiceModel().Hosted();
    
                container.Register(Component.For(anInterface)
                                            .ImplementedBy(wcfType)
                                            .AsWcfService(serviceModel)
                        );
            }
        }
    

    它将注册可用的 WCF 服务。然后,您应该能够直接在 WCF 服务的构造函数中依赖类型化依赖项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多