WCF:具有继承和ServiceKnownType的泛型

时间:2017-02-07 22:29:49

标签: c# wcf generics inheritance

我收到错误:

  

InnerException消息为'第1行位置1392错误。元素'http://schemas.datacontract.org/2004/07/Subscriber.Application.DTO.Entities:LastTransaction'包含映射到名称“http://schemas.datacontract.org/2004/07/Subscriber.Application.DTO.Entities:CreditCardTransactionDTO”的类型的数据。

我有一个PaginatioDTO课程:

[DataContract]
public class PaginationDTO<T> : IPagination<T> {

    [DataMember]
    public List<T> Data { get; private set; }

    [DataMember]
    public int PageSize { get; private set; }

    [DataMember]
    public int CurrentPage { get; private set; }

    [DataMember]
    public int Items { get; private set; }
}

我在WCF服务接口中调用此方法

[ServiceKnownType("GetKnownTypes", typeof(KnownTypesHelper))]
[ServiceContract]
public interface ISubscriberService : IDisposable {

    [OperationContract]
    PaginationDTO<ChargeDTO> GetMerchantCharges(string merchantId, int page, int size);
}

当我更新我的WCF客户端时,我发现它的Reference.cs中有以下行

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISubscriberService/GetMerchantCharges", ReplyAction="http://tempuri.org/ISubscriberService/GetMerchantChargesResponse")]

我发现如果我添加行

[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Subscriber.Application.DTO.Entities.CreditCardTransactionDTO))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Subscriber.Application.DTO.Entities.BoletoTransactionDTO))]

我不再收到错误了。

在我的班级KnownTypesHelper上我有:

    internal static class KnownTypesHelper {

    private static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider) {
        return new List<Type>() {
            typeof(CreditCardTransactionDTO),
            typeof(BoletoTransactionDTO)
        };
    }

我想知道的是,在我的KnownTypesHelper中,我怎么能声明我的PaginationDTO收到一个ChargeDTO作为T,而在ChargeDTO中,我有一个类型为“TransactionDTO”的道具,可以是CreditCardTransactionDTO或BoletoTransactionDTO。

0 个答案:

没有答案