2014-02-22 50 views
0

我正在從CRM 2011中檢索一些數據。我有一個帶有對我的組織的服務引用的Windows窗體應用程序。CRM 2011:更新實體時的序列化例外

  query = new QueryByAttribute(); 
      query.EntityName = "myentityname"; 
      cset = new ColumnSet(); 
      cset.AllColumns = true; 
      cset.Columns = new string[] { "" }; 
      query.ColumnSet = cset; 
      query.Attributes = new string[] { "col1", "col2" }; 
      query.Values = new object[] { col1.value, col2.value }; 
      result = (EntityCollection)_service.RetrieveMultiple(query); 

的 「retrieveMultiple」 拋出下面的異常:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter 
http://schemas.microsoft.com/xrm/2011/Contracts/Services:RetrieveMultipleResult. The InnerException message was 'Error in line 1 position 833. 
Element 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:value' contains data from a type that maps to the name 'http://schemas.microsoft.com/xrm/2011/Contracts:OptionSetValue'. 
The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding 
to 'OptionSetValue' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. 
Please see InnerException for more details. 

我做了一些網上搜索,並最終移除「cset.AllColumns = TRUE;並更換 「」 在query.columns與。該實體的列名的一個這個工作很好,但隨後我的代碼的其餘部分將導致錯誤

  if (result.Entities.Length > 0) 
      { 
       Entity myentity= new Entity(); 
       myentity.LogicalName = "EntityName"; 
       myentity.Attributes = new AttributeCollection(); 
       //record exists, update 
       myentity.Id = (result.Entities[0]).Id; 
       myentity.Attributes.Add(new KeyValuePair<string, object> ("myentityid", ((Entity)result.Entities[0]).Id)); 
       _service.Update(myentity); 

      } 

這一次「更新」方法拋出這個異常:

 There was an error while trying to serialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity. 
    The InnerException message was 'Type 'CRM_Populi_Integration.CRMSer.EntityReference' with data contract name 'EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts' 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. 

我不明白這個serielization錯誤背後的原因,我不知道如何解決它。

注意: 此應用程序在另一臺服務器上工作正常,我只是將應用程序複製並粘貼到另一臺服務器中,並更新了服務引用,並且這些異常開始出現。

你能幫我嗎?

感謝

回答

1

我發現了另一個論壇上,這些解決方案和它的工作對我來說:

解決方案1:在你reference.cs,搜索 「實體類」, 「一流的EntityReference」

在這兩個部分類之上,添加這兩行 [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]

現在搜索「class OrganizationRequest」。

添加這些行上面:

[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))] 
[Syst em.Runtime.Serialization.KnownTypeAttribute(typeof(PrincipalAccess))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 

現在,轉到您的實際代碼和查找字段使用這樣的:

myAttColl.Add(new KeyValuePair<string,object>("parentcustomerid", new EntityReference() {Id = t.Id, LogicalName= t.LogicalName})); 

構建解決方案,它現在應該工作。

解決方案2: 正如您所預料的那樣,更新reference.cs並不是一個很好的方法。 1.用你喜歡的名字創建一個新的班級。 2.保持這個類類似於您Reference.cs的命名空間(這是重要的,所以不要忘記它) 3.現在創建部分類,如下

[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))] 
public partial class Entity { } 


[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))] 
public partial class EntityCollection { } 


[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(PrincipalAccess))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 
public partial class OrganizationRequest { } 
  1. 現在去你的實際代碼,並且查找字段使用是這樣的:

    myAttColl。Add(new KeyValuePair(「parentcustomerid」,new EntityReference(){Id = t.Id,LogicalName = t.LogicalName}));

  2. 生成解決方案

參考:http://www.datazx.cn/Forums/en-US/48d57f16-6bd2-4470-8059-b75a0d8cb16c/action?threadDisplayName=how-to-create-a-record-in-crm-2011-using-web-service&forum=crmdevelopment

相關問題