簡體   English   中英

WCF序列化和反序列化

[英]WCF Serialization and Deserialization

我是WCF服務的新手。 我正在創建WCF服務,該服務從Dynamics CRM記錄下來。 部署到IIS后,我測試了該服務。 並且在以xml格式返回數據時,該服務運行良好。 這是我的代碼

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

主席班:

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在PC上進行測試時,這些代碼運行良好:

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

但是,當其他部門想通過反序列化來調用此服務時,他們說他們不能這樣做。 因此,他們要求我搜索導致問題的原因。 我認為我的代碼尚未序列化,因此它們無法反序列化。 基於這個故事,我有疑問:

  1. 我的代碼要序列化是真的嗎? 如果是這樣,如何使其可序列化? 我正在尋找互聯網上可序列化的示例。 使用稱為MemoryStream東西。 但是我無法在我的代碼中實現它。 因此,請向我展示使代碼可序列化的方法。

根據您的配置和正式聲明,您的List<Presid>在響應中將為xml格式。 響應可能為空{},或者發生錯誤,在這種情況下,純HTML可能會返回給調用方,即錯誤說明,具體取決於服務器配置。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM