gpt4 book ai didi

c# - 具有接口(interface)和通用模​​型的 WCF

转载 作者:行者123 更新时间:2023-11-30 23:29:33 24 4
gpt4 key购买 nike

SO 上有很多关于 WCF 中的接口(interface)和泛型的问题,但我找不到与我遇到的问题相同的问题。

我有一个契约(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; }
}

它与 ChannelFactory<IService> 一起使用上面的代码效果很好。但现在我想向服务添加另一个方法,它也返回 ICollectionWrapper<T> .

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

所以我像注册另一个一样注册它:

[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; }
}

一旦我有两个ServiceKnownTypesCollectionWrapper服务失败并出现以下错误:

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

内部异常:

An existing connection was forcibly closed by the remote host.

我可以在这两行代码之间切换(删除一行,然后添加另一行):

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

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

然后每种方法都起作用,但绝不会同时发生。知道如何让它工作吗?我不想使用具体类。

这是我尝试过的(但失败了):

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

[ServiceKnownType(typeof(CollectionWrapper<>))]

我还尝试为 IAssociation 和 ICustomer 分配一个公共(public)接口(interface),但它也没有用。

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

它非常适合 IEnumerable<T>IList<T>但不是我的ICollectionWrapper<T>

编辑:

ICustomerIAssociation (和它们的实现)没有任何共同点。他们没有从彼此那里继承任何东西,也没有任何其他共同的依赖关系。

最佳答案

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

服务如何知道 ISearchQuery 参数存在哪些已知类型?

我认为您需要将 [KnownType(typeof(SearchQuery))](或您调用的任何 ISearchQuery 实现)添加到您的服务定义中。

关于c# - 具有接口(interface)和通用模​​型的 WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35338236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com