2017-02-27 145 views
0

我是WCF服務的新手。我正在創建從Dynamics CRM獲取記錄的WCF服務。部署到IIS後,我測試了該服務。並且該服務在以xml格式返回數據時運行良好。這裏的是我的代碼WCF序列化和反序列化

Service.svc

public List<Presid> GetPresalesIdList(string userlogin) 
    { 
     List<Presid> idsales = new List<Presid>(); 
     Presid presales = new Presid(); 
     string cmb = userdomain + userlogin; 
     InitializeCRMService(userName, passWord, domain); 

     try 
     { 
      if (!string.IsNullOrEmpty(userlogin)) 
      { 
       QueryExpression qe = new QueryExpression("systemuser"); 
       string[] cols = { "businessunitid", "domainname", "systemuserid" }; 
       qe.Criteria = new FilterExpression(); 
       qe.Criteria.AddCondition("domainname", ConditionOperator.Equal, cmb); 
       qe.ColumnSet = new ColumnSet(cols); 

       var guid = _service.RetrieveMultiple(qe); 

       userid = ((EntityReference)guid[0].Attributes["businessunitid"]).Id; 
       systemid = (Guid)guid[0].Attributes["systemuserid"]; 


       QueryExpression query = new QueryExpression("opportunity"); 
       string[] cols2 = { "new_presalesid","createdon", "ownerid", "name", "parentaccountid", "statecode", "estimatedclosedate" }; 
       query.Criteria = new FilterExpression(); 
       query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, systemid); 
       query.ColumnSet = new ColumnSet(cols2); 
       EntityCollection preid = _service.RetrieveMultiple(query); 

       string prsId = string.Empty; 
       string optNm = string.Empty; 
       string cusNm = string.Empty; 
       string stscde = string.Empty; 
       DateTime closedate; 
       DateTime open; 


       foreach (Entity enty in preid.Entities) 
       { 

        presales = new Presid(); 
        if (enty.Attributes.Contains("new_presalesid")) 
        { 
         prsId = enty.GetAttributeValue<string>("new_presalesid"); 
         if (!string.IsNullOrEmpty(prsId)) 
         { 
          presales.PresalesID = prsId; 
         } 
         else 
         { 
          presales.PresalesID = null; 
         } 
        } 

        if (enty.Attributes.Contains("name")) 
        { 
         optNm = enty.GetAttributeValue<string>("name"); 
         if (!string.IsNullOrEmpty(optNm)) 
         { 
          presales.OptiName = optNm; 

         } 
         else 
         { 
          presales.OptiName = null; 
         } 
        } 

        //cusNm = enty.GetAttributeValue<EntityReference>("parentaccountid").Name; 

        if (enty.Attributes.Contains("parentaccountid")) 
        { 
         cusNm = ((EntityReference)enty["parentaccountid"]).Name; 

         if (!string.IsNullOrEmpty(cusNm)) 
         { 
          presales.CustomerName = cusNm; 
         } 
         else 
         { 
          presales.CustomerName = null; 
         } 
        } 

        if (enty.Attributes.Contains("statecode")) 
        { 
         stscde = enty.FormattedValues["statecode"]; 
         if (!string.IsNullOrEmpty(stscde)) 
         { 
          presales.Status = stscde; 
         } 
         else 
         { 
          presales.Status = null; 
         } 
        } 


        if (enty.Attributes.Contains("estimatedclosedate")) 
        { 
         closedate = enty.GetAttributeValue<DateTime>("estimatedclosedate"); 
         if (closedate != null) 
         { 
          presales.ClosedDate = closedate; 
         } 

        } 

        if (enty.Attributes.Contains("createdon")) 
        { 
         open = enty.GetAttributeValue<DateTime>("createdon"); 
         if (open != null) 
         { 
          presales.CreateOn = open; 
         } 
        } 

        idsales.Add(presales); 
       } 
       return idsales; 

      } 
     } 
     catch (Exception ex) 
     { 

     } 
     return idsales; 
    } 

Presid類:

namespace WCF_CRM_Multipolar 
{ 
[Serializable] 
[DataContract(Namespace = "http://mlpt-web.com/CRM/services")] 
public class Presid 
{ 
    [DataMember] 
    public string PresalesID 
    { 
     get; 
     set; 
    } 

    [DataMember] 
    public string OptiName 
    { 
     get; 
     set; 
    } 

    [DataMember] 
    public string CustomerName 
    { 
     get; 
     set; 
    } 

    [DataMember] 
    public string Status 
    { 
     get; 
     set; 
    } 

    [DataMember] 
    public Nullable<DateTime> ClosedDate 
    { 
     get; 
     set; 
    } 


    [DataMember] 
    public DateTime CreateOn 
    { 
     get; 
     set; 
    } 


} 

}

IService

[OperationContract] 
    [WebInvoke(Method = "GET", 
     ResponseFormat = WebMessageFormat.Xml, 
     RequestFormat = WebMessageFormat.Xml, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "/GetPresalesIdList/{userlogin}")] 
    List<Presid> GetPresalesIdList(string userlogin); 

這些代碼運行良好時,我從這個URL調用從我的電腦測試:

http://localhost:8076/Service1.svc/Getpresalesidlist/user 

但是,當其他部門要調用由反序列化這個服務,他們說他們不能做它。所以他們要求我搜索是什麼原因造成問題。我認爲我的代碼還沒有序列化,所以他們無法反序列化。基於這個故事,我有問題:

  1. 這是真的,我的代碼是序列化呢?如果這是真的,如何使它可序列化? 我正在尋找在互聯網上可序列化的例子。使用一種叫做MemoryStream的東西。但是我無法在我的代碼中實現它。所以請告訴我讓代碼可序列化的方法。

回答

0

根據您的配置和您的正式聲明,您的List<Presid>將以xml格式顯示在回覆中。也許響應爲空{},或者發生錯誤,在這種情況下,純html可能會返回給調用者,錯誤描述取決於服務器配置。

要做的一件事就是刪除[Serializable]屬性,因爲[DataContract(Namespace = "http://mlpt-web.com/CRM/services")]將處理更加特定於您的配置的序列化。你不需要兩個。

+0

Hi @Ross Bush,當我運行服務時,它以xml格式返回數據。我的代碼是否可反序列化?如果是的話,那麼該方法的問題就是反序列化它。 –

+0

我更新了答案。你不需要使用上面的[Serializable]屬性。 –

+0

所以我只需要刪除''[Serializable]'?奧克,我會嘗試。我試過後會通知你。另一個問題,對於WCF中的每個類,我應該爲所有類使用相同的命名空間,還是爲每個類使用不同的命名空間? –