[總結一下]最近的工作

一、如何寫接口說明文檔

最近的工作是圍繞接口進行第三方開發,其中一個是第三方給出接口,在系統中進行接入。主要的困難時接口說明不詳細,需要 很多次的嘗試。在這裏學習到如何去寫接口說明

首先,給出接口地址以及使用方法
其次,給出每個接口的詳細說明
    1、接口名稱:功能描述
    2、參數說明:
    3、返回結果說明:
最後,在寫接口的時候應該進行異常判斷,給出準確的返回信息,而不是給出“錯誤”,“請檢查數據”這些不精確的信息

二、接口常用到的方法

1、生成代理類
Vs在工具中有命令提示cmd窗口工具,打開以後使用下面的語句生成代碼類,將代理類添加到項目中即可使用項目接口
wsdl /language:c# /n:XYS.MISS.Photo.PhotoAdmin /out:d:\Temp\TestService.cs D:\Temp\TestService.wsdl
2、如何處理返回的xml信息

xml作爲Webservice的一種返回信息的常見格式,經常可以在接口開發中遇到。

  • xml生成xsd架構

xsd d:\TDDOWNLOAD\atom-author-link.xml /c /language:C# /outputdir:d:\

  • xml架構生成對應的類

xsd d:\TDDOWNLOAD\atom-author-link.xsd /c /language:C# /outputdir:d:\

將xml信息轉換爲實體對象即xml序列化與反序列化
        public static T Desrialize<T>(string xml)
        {
            T obj = default(T);
            XmlSerializer xs = new XmlSerializer(typeof(T));
            TextReader tr;
            tr = new StringReader(xml);
            using (tr)
            {
                obj = (T)xs.Deserialize(tr);
            }
            return obj;
        }
       public static string XMLSerialize<T>(T entity)
        {
            StringBuilder buffer = new StringBuilder();

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (TextWriter writer = new StringWriter(buffer))
            {
                serializer.Serialize(writer, entity);
            }

            return buffer.ToString();

        }
4、特殊說明
  • ICollection不能序列化與反序列化可以採伐List
  • 不應有 。這個錯誤一般都是xml不能反序列化爲目標對象類型造成的,我的這個原因是因爲:xml的根節點(xml)和對象名(wxp_message)不一樣導致的不能反序列化
5、Code First模式

使用該模式進行數據庫映射的時候,如果出現命名空間不一樣,類名一樣的情況會報錯,需改更改類名,此外可以使用DataAnnonation進行xmlfan反序列化的映射,如下面的LicensesJDS對應xml文檔中Licenses節點名

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "Licenses")]
    public partial class LicensesJDS
    {
        [Key]
        public int ID { get; set; }
        private List<LicensesLicenseJDS> licenseField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("license")]
        public List<LicensesLicenseJDS> license
        {
            get
            {
                return this.licenseField;
            }
            set
            {
                this.licenseField = value;
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章