WebService

WebService 是基于soap与http协议以xml数据格式进行通信

、 soap 协议

   soap 协议的全称是简单对象访问协议(simple object access protocol),soap 协议已XML的形式进行数据传输,soap协议时可以跨平台使用,只要规范对象的访问方式,但soap协议仍然是基于http协议,通过http去进行数据传输。

  soap 协议的组成部分:

  a、Soap 信封

  b、soap 编码规则

 c、soap 远程调用

d、soap 绑定

soap协议和http协议的关系,类似于网络分层的上下层协议。使用soap协议的双方 把soap 包绑定到http报文之中,并且通过http 进行实际的数据传输。

 总结: soap 协议提供了简单的数据传输机制,以XML的形式进行数据库传输。

  WSDL(Web Service Description Language) web 服务描述语言,它符合XML的语言规范,用来描述服务端webservice 方法,包含以下内容:

 a、方法名称

 b、方法参数

c、 各个参数的类型

d、返回的数据类型等

一个WSDL可以完整的描述服务器提供的Web Service

Web Service 中webmethod 包含以下属性:

BufferResponse:

    /// <summary>
        /// BufferResponse =true  时,服务端以16KB的块区 缓冲响应,响应在序列化的同事不断的发给客户端,不管响应是否完全结束
        ///  小批量数据 开启时  可显著提高性能
        /// </summary>
        /// <returns></returns>
        [WebMethod(BufferResponse =true)] 
       
        public string TestBufferResponse()
        {

            return "Hello World";
        }

 enableSession

   未开启session:

 #region   测试enableSession

        [WebMethod(enableSession:true)]
        public string TestWithSession()
        {
            return GetSession();
        }
        [WebMethod(enableSession: false)]
        public string TestWithNoSession()
        {
            // 当前方法禁止使用session
            return GetSession();
        }
        public string GetSession()
        {

            if (Session == null)
                return $"{nameof(TestWithSession)}__session 被禁用";
            if (Session["i"] == null)
                Session["i"] = 0;
                Session["i"] = (int)Session["i"] + 1;
            return Session["i"].ToString() ;

        }

        #endregion

  

 

 

 

描述 别名

  [WebMethod(CacheDuration =10,EnableSession =true,Description ="测试cacheduration",MessageName ="CacheTest")]
        
        public  string TestCachaDuration()
        {
            if (Session["i"] == null)
                Session["i"] = 0;
            Session["i"] = (int)Session["i"] + 1;
            return Session["i"].ToString();
        }

  

事务:

   //首先引用using System.EnterpriseServices;,然后设置属性TransactionOption = TransactionOption.Required。
    //设置TransactionOption.Disabled、TransactionOption.NotSupported、TransactionOption.Supported表示不参与事务。
    ///设置TransactionOption.Required、TransactionOption.RequiresNew表示创建一个新的事务。
    ///意思是说当TransactionOption的属性为Required或 RequiresNew的WEB服务方法调用另一个TransactionOption的属性为Required或RequiresNew的WEB服务方法时,
    ///每个WEB服务方法将参与他们自己的事务,因为Web Service方法只能用作事务中的根对象。
    //PS:WEB服务方法的TransactionOption默认属性为Disabled
    //提交事务ContextUtil.SetComplete();
    //回滚事务ContextUtil.SetAbort();
    [WebMethod(TransactionOption = TransactionOption.Required)]
    public string HelloWorlds()
    {
        try
        {
            SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;database=db.mdf;");
            SqlCommand cmd = new SqlCommand("update users set name = 'yangxing' where id = 5", con);
            con.Open();
            cmd.ExecuteNonQuery();
            cmd.CommandText = "update users1 set name = 'yangxing1' where id = 6";//users1表不存在,执行该语句报错
            cmd.ExecuteNonQuery();//抛出异常
            ContextUtil.SetComplete();//提交事务
            return "true";
        }
        catch
        {
            ContextUtil.SetAbort();//回滚事务
            return "false";
        }
    }

  

  [WebMethod(CacheDuration =10,EnableSession =true,Description ="测试cacheduration",MessageName ="CacheTest")]                public  string TestCachaDuration()        {            if (Session["i"] == null)                Session["i"] = 0;            Session["i"] = (int)Session["i"] + 1;            return Session["i"].ToString();        }

原文地址:https://www.cnblogs.com/hnzheng/p/12727310.html