[总结一下]最近的工作

    xiaoxiao2021-03-25  100

    一、如何写接口说明文档

    最近的工作是围绕接口进行第三方开发,其中一个是第三方给出接口,在系统中进行接入。主要的困难时接口说明不详细,需要 很多次的尝试。在这里学习到如何去写接口说明

    首先,给出接口地址以及使用方法
    其次,给出每个接口的详细说明
    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; } } }
    转载请注明原文地址: https://ju.6miu.com/read-12936.html

    最新回复(0)