WCF随着接口和一个通用模型 [英] WCF With a interface and a generic model

查看:159
本文介绍了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> <以上/ code>和代码的伟大工程。但现在我想其他方法添加到服务,这也将返回 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 服务失败,出现以下错误:




基础连接已关闭:在
A收到出现意外的错误




和内部异常:




这是现有的连接被强行关闭远程主机。




我的这两行代码(删除一个,并添加其他)之间切换:

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

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

随后的方法各项工作,,但从来没有在同一时间即可。不知道如何得到它的工作?我不希望使用的具体类



这是我试过什么(失败):

  [ServiceKnownType(typeof运算(CollectionWrapper<对象>))] 

[ServiceKnownType(typeof运算(CollectionWrapper<>))]

我也试过分配一个通用的接口,既IAssociation和ICustomer,但它也不能工作。

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

的伟大工程的IEnumerable< T> 的IList< T> ,但不与我的 ICollectionWrapper< T>



编辑:



ICustomer IAssociation (及其实施)没有任何共同之处。他们不继承海誓山盟什么,他们也没有任何其他常见的依赖关系。


解决方案

经过大量的研究,我无法找到一个好的解决办法,但我想我会后我结束了(即使它并没有解决它,我想要的方式)

解决方案 [的ServiceContract] 
[ServiceKnownType(typeof运算(CompanyDto>))]
[ServiceKnownType(typeof运算(CompanyDto>))]
[ServiceKnownType(typeof运算(CustomerDto))]
[ServiceKnownType(typeof运算(OrganizationDto))]
公共接口IService:
{
[OperationContract的] //混凝土式
CollectionWrapper< IAssociation> FindAssociation(字符串名称,诠释的pageSize,诠释页);

[OperationContract的] //混凝土式
CollectionWrapper< ICustomer>搜索(ISearchQuery SEARCHQUERY);
}



它是只需要通过具体的类,而不是接口的解决。



<:这不是我真正想要的,但现在它解决了我的问题。



可能的复制在这里A HREF =http://stackoverflow.com/questions/15672183/generic-return-types-with-interface-type-params-in-wcf>通用返回类型与WCF

There's a lot of questions on SO regarding interfaces and generics in WCF, but I'm unable to find one that points to the same problem as I have.

I have a service with a contract:

[ServiceContract]
[ServiceKnownType(typeof(CollectionWrapper<IAssociation>))]
public interface IService : 
{
    [OperationContract]
    ICollectionWrapper<IAssociation> FindAssociation(string name, int pageSize, int page);
}

public interface ICollectionWrapper<TModel>
{
    int TotalCount { get; set; }
    IEnumerable<TModel> Items { get; set; }
}

[KnownType(typeof(OrganizationDto))]
[KnownType(typeof(CompanyDto))]
public class CollectionWrapper<TModel> : ICollectionWrapper<TModel>
{
    [DataMember]
    public int TotalCount { get; set; }
    [DataMember]
    public IEnumerable<TModel> Items { get; set; }
}

public class CompanyDto :  IAssociation
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class OrganizationDto :  IAssociation
{
    public int Id { get; set; }
    public string Name { get; set; }
}

It's consumed with a ChannelFactory<IService> and the code above works great. But now I want to add another method to the service, which also returns ICollectionWrapper<T>.

[OperationContract]
ICollectionWrapper<ICustomer> Search(ISearchQuery searchQuery);

So I register it as I did with the other:

[ServiceContract]
[ServiceKnownType(typeof(CollectionWrapper<IAssociation>))]
[ServiceKnownType(typeof(CollectionWrapper<ICustomer>))] // This line creates the error.
public interface IService : 
{
    [OperationContract]
    ICollectionWrapper<IAssociation> FindAssociation(string name, int pageSize, int page);

    [OperationContract] // New method.
    ICollectionWrapper<ICustomer> Search(ISearchQuery searchQuery);
}

[KnownType(typeof(OrganizationDto))]
[KnownType(typeof(CompanyDto))]
[KnownType(typeof(CustomerDto))] // New model.
public class CollectionWrapper<TModel> : ICollectionWrapper<TModel>
{
    [DataMember]
    public int TotalCount { get; set; }
    [DataMember]
    public IEnumerable<TModel> Items { get; set; }
}

And as soon as I have two ServiceKnownTypes with CollectionWrapper the service fails with the following error:

The underlying connection was closed: An unexpected error occurred on a receive.

And the inner exception:

An existing connection was forcibly closed by the remote host.

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随着接口和一个通用模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆