无法将 serialize/deserialize 参数传递给 WCF 方法
Unable to serialize/deserialize parameters passed to WCF method
我从 WCF 服务获得了这个接口:
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]
public interface IQuerySageService
{
[OperationContract]
CustomerLedger GetBillingContact(string crmAccountNumber);
[OperationContract]
ImportCrmInvoicesResponse ImportCrmInvoices(List<New.Xrm.Entities.Invoice> invoices);
}
而上面提到的方法来自这个class:
internal static class KnownTypesProvider
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
List<Type> types = new List<Type>();
types.Add(typeof(New.Xrm.Entities.InvoiceDetail));
return types;
}
}
但是当从客户端(CRM 工作流程)调用 ImportCrmInvoices
(Invoice
有 InvoiceDetail
个子项)时,出现以下错误:
There was an error while trying to serialize parameter
http://tempuri.org/:invoices. The InnerException message was 'Type
'New.Xrm.Entities.InvoiceDetail' with data contract name
'InvoiceDetail:http://schemas.datacontract.org/2004/07/New.Xrm.Entities'
is not expected. Consider using a DataContractResolver or add any
types not known statically to the list of known types - for example,
by using the KnownTypeAttribute attribute or by adding them to the
list of known types passed to DataContractSerializer.'. Please see
InnerException for more details."
我认为这个 ServiceKnownType
装饰可以根据我所读的内容来解决问题,但显然不是。
类型(Invoice、InvoiceDetail)包含在程序集中,由客户端和服务器(命名空间 New.Xrm.Entities
)引用
现在,包含这些类型的物理 CS 文件非常大 (7MB),而且几乎无法编辑。所以也许我在某处缺少 DataContract
装饰,但我不想在这个文件中添加它。
还有其他方法可以实现吗?
实现此目的的另一种方法是在您的父数据合同 class 上使用 KnownTypes 属性,您的子 class 是从中派生的。
The KnownTypeAttribute class allows you to specify, in advance, the types that should be included for consideration during deserialization.
(https://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx)
KnownTypes 属性可以采用类型参数,或者您可以在 class returns 有效类型数组中提供方法名称。
例如:
[DataContract]
[KnownTypes(typeof(InvoiceDetail)]
public class Invoice
{
[DataMember]
public string SomeProperty {get; set; }
}
[DataContract]
public class InvoiceDetail : Invoice
{
}
我最终让 WCF 服务在 CRM 中自行查找。 ImportCrmInvoices 方法接受 GUID 列表并获取有关它们本身的数据。
我从 WCF 服务获得了这个接口:
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]
public interface IQuerySageService
{
[OperationContract]
CustomerLedger GetBillingContact(string crmAccountNumber);
[OperationContract]
ImportCrmInvoicesResponse ImportCrmInvoices(List<New.Xrm.Entities.Invoice> invoices);
}
而上面提到的方法来自这个class:
internal static class KnownTypesProvider
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
List<Type> types = new List<Type>();
types.Add(typeof(New.Xrm.Entities.InvoiceDetail));
return types;
}
}
但是当从客户端(CRM 工作流程)调用 ImportCrmInvoices
(Invoice
有 InvoiceDetail
个子项)时,出现以下错误:
There was an error while trying to serialize parameter http://tempuri.org/:invoices. The InnerException message was 'Type 'New.Xrm.Entities.InvoiceDetail' with data contract name 'InvoiceDetail:http://schemas.datacontract.org/2004/07/New.Xrm.Entities' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details."
我认为这个 ServiceKnownType
装饰可以根据我所读的内容来解决问题,但显然不是。
类型(Invoice、InvoiceDetail)包含在程序集中,由客户端和服务器(命名空间 New.Xrm.Entities
)引用
现在,包含这些类型的物理 CS 文件非常大 (7MB),而且几乎无法编辑。所以也许我在某处缺少 DataContract
装饰,但我不想在这个文件中添加它。
还有其他方法可以实现吗?
实现此目的的另一种方法是在您的父数据合同 class 上使用 KnownTypes 属性,您的子 class 是从中派生的。
The KnownTypeAttribute class allows you to specify, in advance, the types that should be included for consideration during deserialization.
(https://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx)
KnownTypes 属性可以采用类型参数,或者您可以在 class returns 有效类型数组中提供方法名称。
例如:
[DataContract]
[KnownTypes(typeof(InvoiceDetail)]
public class Invoice
{
[DataMember]
public string SomeProperty {get; set; }
}
[DataContract]
public class InvoiceDetail : Invoice
{
}
我最终让 WCF 服务在 CRM 中自行查找。 ImportCrmInvoices 方法接受 GUID 列表并获取有关它们本身的数据。