Being simple

Any fool can write code that computer can understand. Good programmers write codes that humans can understand.

导航

公告

统计

web service notes

  1. 性能提高
    压缩传输数据, 减少多次调用, XML解析器的优化和选择, 简化标签, 缓存机制
    ref: http://www.ibm.com/developerworks/cn/webservices/0710_wangyun/index.html?S_TACT=105AGX52&S_CMP=NL&ca=dnl-cn-11212007
  2. in class library project use  WebServiceHandlerFactory to create Web service, http://www.codeproject.com/useritems/wsinaclasslibrary.asp
    write a class WebServiceBase to implements webserviceHandlerFactory, then implement your web service inherit from this class, and copy this compoment into your web app bin directory, then add <add path="WSTest.asmx" verb="*" type="WSLibrary.WSTest" validate="false"/> into your web.config's httphandler section.
  3. [WebMethod(EnableSession=true)]XXX 此声明可以在WS中引用session对象, 某种程度上可以提高性能; 但需要客户端在调用时支持,默认情况下browser中调用WS时要有SESSION的,而在普通的EXE或程序中调用WS时,需要显示声明要使用cookie,否则服务器端的session无效..net 中声明如下:
    WebService1 srv = new WebService1();
    srv.CookieContainer = new CookieContainer();
    只有经过这样声明,调用的WS才会传递SESSIONID,建立在一个应用中只有一个地方可获取WS,即采用singleton模式,这样所有地方调用WS时都有sessionID支持.
  4. 利用WS上传文件,可以在CLIENT中将文件中的内容读取到byte[]中,然后调用WS接口,然后WS再将byte写入到文件中
    转换FileStream fileStream = File.OpenRead(path + fileName);为MemoryStream,再转换为byte[].
    public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
            {
                int b1;
                System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
                while ((b1 = theStream.ReadByte()) != -1)
                {
                    tempStream.WriteByte(((byte)b1));
                }
                return tempStream.ToArray();
            }
    如果是将byte[]写入到文件,则代码如下:
    {
                    MemoryStream memoryStream = new MemoryStream(fs);
                    FileStream fileStream = new FileStream(path + fileName, FileMode.Create);
                    memoryStream.WriteTo(fileStream);
                    memoryStream.Close();
                    fileStream.Close();
                    fileStream = null;
                    memoryStream = null;

posted on 2007-11-22 08:32 margiex 阅读(82) 评论(0) 编辑 收藏