Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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# 在WCF中使用Castle Windsor解析泛型_C#_Wcf_Generics_Castle Windsor - Fatal编程技术网

C# 在WCF中使用Castle Windsor解析泛型

C# 在WCF中使用Castle Windsor解析泛型,c#,wcf,generics,castle-windsor,C#,Wcf,Generics,Castle Windsor,我花了一点时间来了解以下设计,并希望得到一些反馈-我正在使用Windsor属性注入一个类型,供Wcf服务使用 我的界面最初看起来是这样的,其中响应和请求类型是抽象的: public interface IFooHandler { CheckFooResponse CheckFoo(CheckFooRequest request); } 我担心我无法控制在实现IFooHandler的类中使用了哪些抽象类的实现,因此设计更改为: 公共接口ICheckFoodHandler where-T

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

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

public interface IFooHandler
{
    CheckFooResponse CheckFoo(CheckFooRequest request);
}
我担心我无法控制在实现IFooHandler的类中使用了哪些抽象类的实现,因此设计更改为:

公共接口ICheckFoodHandler
where-TRequest:checkfoo请求where-treresponse:checkfoo响应
{
treresponse CheckFoo(TRequest请求);
}
使用方法:

public class CheckFooHandler : ICheckFooHandler<MyFooRequest, MyFooResponse>
{
    MyFooResponse CheckFoo(MyFooRequest request) { ... }
}
公共类checkFoodHandler:ICheckFoodHandler
{
MyFooResponse CheckFoo(MyFooRequest请求){…}
}
然后我试着连接温莎:

IoCContainer = new WindsorContainer();

IoCContainer.Register
(Component
.For(typeof(ICheckFooHandler<,>))
.ImplementedBy(typeof(CheckFooHandler<,>)));
IoCContainer=新的WindsorContainer();
IoCContainer.寄存器
(组成部分
.For(类型为(ICheckFoodHandler))
.ImplementedBy(typeof(checkfoodhandler)));
直到我将CheckFoodHandler的实现更改为:

公共类checkFoodHandler:ICheckFoodHandler
其中TRequest:MyCheckFooRequest
where-TResponse:MyCheckFooResponse,new()
{
公共响应CheckFoo(TRequest请求)
{
...
var响应=新的响应();
response.MyResponseProperty=“baz”;
}
它实现了通用接口契约,同时允许Windsor的通用语法工作

在我的Wcf服务中,我拥有温莎注入的以下私有财产:

private ICheckFooHandler<MyCheckFooRequest, MyCheckFooResponse> _checkFooHandler;

private ICheckFooHandler<MyCheckFooRequest, MyCheckFooResponse> CheckFooHandler
{
    get
    {
        if (_checkFooHandler == null)
        {
            _checkFooHandler = Global.IoCContainer
                .Resolve<ICheckFooHandler<MyFooRequest, MyFooResponse>>();
        }
    }
}
private icheckfoodhandler\u checkfoodhandler;
私有ICheckFoodHandler checkFoodHandler
{
得到
{
if(_checkfoodhandler==null)
{
_checkFoodHandler=Global.IoCContainer
.Resolve();
}
}
}

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

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


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

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

您可以使用Castle Windsor容器完全设置应用程序运行时和it Wcf服务

例如:

    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)
                    );
        }
    }
container.AddFacility();
foreach(var wcfType in types.Where(t=>t.IsInterface==false&&t.isastract==false))//类型应来自AppDomain.Current程序集
{
foreach(var)是wcfType.GetInterfaces()中的接口,其中(an接口=>an接口
.GetCustomAttributes(typeof(ServiceContractAttribute),true).Any())
{
var serviceModel=new DefaultServiceModel().Hosted();
容器寄存器(组件)(接口)
.ImplementedBy(wcfType)
.AsWcfService(serviceModel)
);
}
}
它将注册可用的WCF服务。然后您应该能够直接依赖WCF服务构造函数中的类型依赖项

private ICheckFooHandler<MyCheckFooRequest, MyCheckFooResponse> _checkFooHandler;

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