zoukankan      html  css  js  c++  java
  • WebService 之 实例学习一

      新建一个空网站项目,添加新建项 “ Web 服务 ”。

    一、WebServiceDemo.asmx 文件,默认内容如下:

    <%@ WebService Language="C#" CodeBehind="WebServiceDemo.asmx.cs" Class="WebServie.WebServiceDemo" %>

    二、WebServiceDemo.asmx.cs 文件,默认内容如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    namespace WebServie
    {
        /// <summary>
        /// WebServiceDemo 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        // [System.Web.Script.Services.ScriptService]
        public class WebServiceDemo : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
        }
    }

      如上源码中,黄色标记 “ using System.Web.Services; ” 为必须引入的命名空间。

      继承层次:WebServiceDemo 继承自 System.Web.Services 命名空间下的 WebService 类 → 继承自 System.Web.Services 命名空间下的MarshalByValueComponent 类 → 继承自 System.ComponentModel 命名空间下的 IComponent, IDisposable, IServiceProvider 三个接口。

      WebService 主要包含 WebService 、SoapDocumentService、WebServiceBinding三个属性。若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,需取消对ScriptService 属性的注释。

      1、WebService 属性:用于向 XML Web services 添加附加信息,如描述其功能的字符串。

    using System;
    
    namespace System.Web.Services
    {
        // 用于向 XML Web services 添加附加信息,如描述其功能的字符串。
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
        public sealed class WebServiceAttribute : Attribute
        {
            // Namespace的默认值
            // property.此字段为常数。
            public const string DefaultNamespace = "http://tempuri.org/";
    
            // 初始化 System.Web.Services.WebServiceAttribute 类的新实例。
            public WebServiceAttribute();
    
            // XML Web services 的描述性消息。
            // 对 XML Web services 的功能进行描述的文本。
            public string Description { get; set; }
            
            // 获取或设置 XML Web services 的名称。
            // 默认值是实现 XML Web services 的类的名称。
            public string Name { get; set; }
            
            // 获取或设置用于 XML Web services 的默认 XML 命名空间。
            // 默认值在 System.Web.Services.WebServiceAttribute.DefaultNamespace属性中指定。
            public string Namespace { get; set; }
        }
    }

      2、SoapDocumentServiceAttribute 属性:

    using System;
    using System.Web.Services.Description;
    
    namespace System.Web.Services.Protocols
    {
        // 设置在 XML Web services 内发送到 XML Web services 方法的 SOAP 请求和从 XML Web services 方法发回的 SOAP 响应的默认格式。
        [AttributeUsage(AttributeTargets.Class)]
        public sealed class SoapDocumentServiceAttribute : Attribute
        {
            //初始化一个 SoapDocumentServiceAttribute 类的新实例,将所有属性都设置为其默认值。
            public SoapDocumentServiceAttribute();
            
            //初始化一个 SoapDocumentServiceAttribute 类的新实例,设置参数的格式设置。
            // 参数:
            // use:XML Web services 的参数格式设置。设置 SoapDocumentServiceAttribute.Use 属性。
            public SoapDocumentServiceAttribute(SoapBindingUse use);
            
            //初始化 SoapDocumentServiceAttribute 类的新实例,该类设置参数的格式设置并设置参数是否封装在 SOAP 消息中的 Body 元素下的单个 XML 元素中。
            // 参数:
    // use:参数格式设置样式。设置 SoapDocumentServiceAttribute.Use 属性。 // paramStyle:设置参数是否封装在 XML Web services 中发往和来自 XML Web services 方法的 SOAP 消息中的 Body 元素下的单个 // XML 元素中。设置 ParameterStyle 属性。 public SoapDocumentServiceAttribute(SoapBindingUse use, SoapParameterStyle paramStyle); //获取或设置用来控制参数是否封装在 XML Web services 的 XML Web services 方法的 SOAP 消息 XML 部分中 <Body> 元素之后的单个元素中的默认设置。 // 返回结果: // 在 XML Web services 中发往 XML Web services 方法的 SOAP 的请求和从 XML Web services 中的 // XML Web services 方法发回的 SOAP 响应的默认 SoapParameterStyle。如果未设置,则默认为 SoapParameterStyle.Wrapped。 public SoapParameterStyle ParameterStyle { get; set; } //获取或设置将 SOAP 消息发送到 XML Web services 的方式。 // 返回结果:SoapServiceRoutingStyle,表示将 SOAP 消息发送到 XML Web services 的方式。默认值为 SoapServiceRoutingStyle.SoapAction。 public SoapServiceRoutingStyle RoutingStyle { get; set; } //获取或设置 XML Web services 的默认参数格式设置。 // 返回结果:XML Web services 的默认 SoapBindingUse。如果未设置,则默认为 SoapBindingUse.Literal。 public SoapBindingUse Use { get; set; } } }

      3、WebServiceBinding 属性:声明定义一个或多个 XML Web services 方法的绑定。无法继承此类。

    using System;
    
    namespace System.Web.Services
    {
        //声明定义一个或多个 XML Web services 方法的绑定。无法继承此类。
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
        public sealed class WebServiceBindingAttribute : Attribute
        {
            // 初始化一个 WebServiceBindingAttribute 的实例
    public WebServiceBindingAttribute(); // 通过设置 XML Web services 方法正在实现的绑定的名称初始化 System.Web.Services.WebServiceBindingAttribute 类的新实例 // 参数: // name:XML Web services 方法为其实现操作的绑定的名称。设置 System.Web.Services.WebServiceBindingAttribute.Name 属性。 public WebServiceBindingAttribute(string name); //初始化 System.Web.Services.WebServiceBindingAttribute 类的新实例。 // 参数: // name:XML Web services 方法为其实现操作的绑定的名称。设置 System.Web.Services.WebServiceBindingAttribute.Name 属性。 // ns:与该绑定关联的命名空间。设置 System.Web.Services.WebServiceBindingAttribute.Namespace 属性。 public WebServiceBindingAttribute(string name, string ns); //初始化 System.Web.Services.WebServiceBindingAttribute 类的新实例。
    // 参数: // name:XML Web services 方法为其实现操作的绑定的名称。设置 System.Web.Services.WebServiceBindingAttribute.Name 属性。 // ns:与该绑定关联的命名空间。设置 System.Web.Services.WebServiceBindingAttribute.Namespace 属性。 // location:定义绑定的位置。 public WebServiceBindingAttribute(string name, string ns, string location); // 摘要:获取或设置绑定声称所符合的 Web 服务互操作性 (WSI) 规范。 // 返回结果:System.Web.Services.WsiProfiles 值之一,指示 WSI 规范。 public WsiProfiles ConformsTo { get; set; } // 摘要:获取或设置一个值,该值指示绑定是否发出一致性声称。 // 返回结果:如果绑定发出一致性声称,则为 true;否则为 false。 public bool EmitConformanceClaims { get; set; }
    // 摘要:获取或设置定义绑定的位置。 // 返回结果:定义绑定的位置。默认为此属性应用到的 XML Web services 的 URL。 public string Location { get; set; } // 摘要:获取或设置绑定的名称。 // 返回结果:绑定的名称。默认为追加了“Soap”的 XML Web services 名称。 public string Name { get; set; } // 摘要:获取或设置与该绑定关联的命名空间。
    // 返回结果:绑定的命名空间。默认为 http://tempuri.org/ public string Namespace { get; set; } } }

      4、ScriptServiceAttribute 属性:指示某个 Web 服务可从脚本调用。

    using System;
    using System.Runtime;
    
    namespace System.Web.Script.Services
    {
        //指示某个 Web 服务可从脚本调用。无法继承此类。
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
        public sealed class ScriptServiceAttribute : Attribute
        {
            //初始化 System.Web.Script.Services.ScriptServiceAttribute 类的新实例。
            [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
            public ScriptServiceAttribute();
        }
    }

      5、WebMethod 属性:

      WebService 通过 [WebMethod] 属性将方法暴露给调用者,有6个属性:Description、EnableSession、MessageName、TransactionOption、CacheDuration、BufferResponse。

    using System;
    using System.EnterpriseServices;
    
    namespace System.Web.Services
    {
        //向使用 ASP.NET 创建的 XML Web services 中的某个方法添加此特性后,就可以从远程 Web 客户端调用该方法。无法继承此类。
        [AttributeUsage(AttributeTargets.Method)]
        public sealed class WebMethodAttribute : Attribute
        {
            // 初始化一个 WebMethodAttribute 新实例。
            public WebMethodAttribute();
            
            // 初始化一个 WebMethodAttribute 新实例。
    // 参数: // enableSession:初始化是否为 XML Web services 方法启用会话状态。 public WebMethodAttribute(bool enableSession); //初始化 WebMethodAttribute 类的新实例。 // 参数: // enableSession:初始化是否为 XML Web services 方法启用会话状态。 // transactionOption:初始化 XML Web services 方法的事务支持。 public WebMethodAttribute(bool enableSession, TransactionOption transactionOption); // 初始化 WebMethodAttribute 类的新实例。 // 参数: // enableSession:初始化是否为 XML Web services 方法启用会话状态。 // transactionOption:初始化 XML Web services 方法的事务支持。 // cacheDuration:初始化响应的缓存秒数。 public WebMethodAttribute(bool enableSession, TransactionOption transactionOption, int cacheDuration); // 初始化 WebMethodAttribute 类的新实例。 // 参数: // enableSession:初始化是否为 XML Web services 方法启用会话状态。 // transactionOption:初始化 XML Web services 方法的事务支持。 // cacheDuration:初始化响应的缓存秒数。 // bufferResponse:初始化是否缓存该请求的响应。 public WebMethodAttribute(bool enableSession, TransactionOption transactionOption, int cacheDuration, bool bufferResponse); //获取或设置是否缓存该请求的响应。 // 返回结果:如果缓存该请求的响应,则为 true;否则为 false。默认为 true。 public bool BufferResponse { get; set; } //获取或设置响应应在缓存中保留的秒数。 // 返回结果:响应应在缓存中保留的秒数。默认值为 0,表示不缓存响应。 public int CacheDuration { get; set; } //描述 XML Web services 方法的描述性消息。 // 返回结果:描述 XML Web services 方法的描述性消息。默认值为 System.String.Empty。 public string Description { get; set; } //指示是否为 XML Web services 方法启用会话状态。 // 返回结果:如果为 XML Web services 方法启用会话状态,则为 true。默认为 false。 public bool EnableSession { get; set; } //在传递到 XML Web services 方法和从 XML Web services 方法返回的数据中用于 XML Web services 方法的名称。 // 返回结果: // 在传递到 XML Web services 方法和从 XML Web services 方法返回的数据中用于 XML Web services 方法的名称。默认值是XML Web services 方法的名称。 public string MessageName { get; set; } //指示 XML Web services 方法的事务支持。 // 返回结果:XML Web services 方法的事务支持。默认为 TransactionOption.Disabled。 public TransactionOption TransactionOption { get; set; } } }

    摘要: // Initializes a new instance of the System.Web.Services.WebMethodAttribute // class.

  • 相关阅读:
    Sendkeys 和 Sendmessage 使用技巧一例
    和菜鸟一起学算法之二分法求极值问题
    和菜鸟一起学算法之三分法求极值问题
    和菜鸟一起学证券投资之国内生产总值GDP
    和菜鸟一起学OK6410之Led字符驱动
    和菜鸟一起学OK6410之最简单驱动模块hello world
    和菜鸟一起学OK6410之交叉编译hello world
    和菜鸟一起学android4.0.3源码之touchscreen配置+调试记录
    和菜鸟一起学android4.0.3源码之红外遥控器适配
    和菜鸟一起学OK6410之最简单字符驱动
  • 原文地址:https://www.cnblogs.com/xinaixia/p/5881575.html
Copyright © 2011-2022 走看看