WCF随着接口和一个通用模型

WCF随着接口和一个通用模型

本文介绍了WCF随着接口和一个通用模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多对SO关于WCF接口和仿制药的问题,但我无法找到一个指向,因为我有同样的问题。



我有合同的服务:

  [的ServiceContract] 
[ServiceKnownType(typeof运算(CollectionWrapper< IAssociation>)) ]
公共接口IService:
{
[OperationContract的]
ICollectionWrapper< IAssociation> FindAssociation(字符串名称,诠释的pageSize,诠释页);
}

公共接口ICollectionWrapper<&的TModel GT;
{
INT TOTALCOUNT {搞定;组; }
IEnumerable的<&的TModel GT;项目{搞定;组; }
}

[KnownType(typeof运算(OrganizationDto))]
[KnownType(typeof运算(CompanyDto))]
公共类CollectionWrapper<的TModel> :ICollectionWrapper<&的TModel GT;
{
[数据成员]
公众诠释TOTALCOUNT {搞定;组; }
[数据成员]
公开的IEnumerable<&的TModel GT;项目{搞定;组; }
}

公共类CompanyDto:IAssociation
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
}

公共类OrganizationDto:IAssociation
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
}



它消耗了的ChannelFactory< IService> 和代码的伟大工程。但现在我想其他方法添加到服务,这也将返回 ICollectionWrapper< T>

  [OperationContract的] 
ICollectionWrapper< ICustomer>搜索(ISearchQuery SEARCHQUERY);



所以我注册它,因为我与其他所做的:

  [的ServiceContract] 
[ServiceKnownType(typeof运算(CollectionWrapper< IAssociation>))]
[ServiceKnownType(typeof运算(CollectionWrapper< ICustomer>))] / /这行创建错误。
公共接口IService:
{
[OperationContract的]
ICollectionWrapper< IAssociation> FindAssociation(字符串名称,诠释的pageSize,诠释页);

[OperationContract的] //新方法。
ICollectionWrapper< ICustomer>搜索(ISearchQuery SEARCHQUERY);
}

[KnownType(typeof运算(OrganizationDto))]
[KnownType(typeof运算(CompanyDto))]
[KnownType(typeof运算(CustomerDto))//新的模式。
公共类CollectionWrapper<&的TModel GT; :ICollectionWrapper<&的TModel GT;
{
[数据成员]
公众诠释TOTALCOUNT {搞定;组; }
[数据成员]
公开的IEnumerable<&的TModel GT;项目{搞定;组; }
}

和,只要我有两个 ServiceKnownTypes CollectionWrapper 服务失败,出现以下错误:



And the inner exception:

I can toggle between these two lines of code (remove one, and add the other):

[ServiceKnownType(typeof(CollectionWrapper<ICustomer>))]

[ServiceKnownType(typeof(CollectionWrapper<IAssociation>))]

Then each of the methods work, but never at the same time. Any idea how to get it to work? I do not want to use concrete classes.

This is what I've tried (and failed):

[ServiceKnownType(typeof(CollectionWrapper<object>))]

[ServiceKnownType(typeof(CollectionWrapper<>))]

I also tried to assign a common interface to both IAssociation and ICustomer, but it didn't work either.

[ServiceKnownType(typeof(CollectionWrapper<ISomething>))]

It works great for IEnumerable<T> and IList<T> but not with my ICollectionWrapper<T>

EDIT:

ICustomer and IAssociation (and their implementations) have nothing in common. They do not inherit anything from eachother, nor do they have any other common dependencies.

解决方案

After a lot of research I was unable to find a good solution, but I figure I would post the solution I ended up with (even if it didn't solve it the way I wanted)

[ServiceContract]
[ServiceKnownType(typeof(CompanyDto>))]
[ServiceKnownType(typeof(CompanyDto>))]
[ServiceKnownType(typeof(CustomerDto))]
[ServiceKnownType(typeof(OrganizationDto))]
public interface IService :
{
    [OperationContract] // Concrete type
    CollectionWrapper<IAssociation> FindAssociation(string name, int pageSize, int page);

    [OperationContract] // Concrete type
    CollectionWrapper<ICustomer> Search(ISearchQuery searchQuery);
}

It was simply solved by using the concrete class instead of the interface. It's not what I really wanted, but for now it solved my issue.

Possible duplicate here:

Generic return types with interface type params in WCF

这篇关于WCF随着接口和一个通用模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:59