• .NET 创建 WebService


    服务器端代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Services;
     6 using System.Xml.Serialization;
     7 using System.Web.Services.Protocols;
     8 using System.IO;
     9 using System.Xml;
    10 
    11 
    12 namespace WebService1
    13 {
    14     /// <summary>
    15     /// Service1 的摘要说明
    16     /// </summary>
    17 
    18     [WebService(
    19         Namespace = "http://asn.test.cn/", 
    20         Description="this is a test service!")]
    21     [System.ComponentModel.ToolboxItem(false)]
    22     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    23     // [System.Web.Script.Services.ScriptService]
    24     public class Service1 : System.Web.Services.WebService
    25     {
    26 
    27         [WebMethod]
    28         [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Wrapped)]
    29         public void PurchaseOrder(
    30             [XmlAttribute] String ID,
    31             DateTime Date,
    32             int Amount,
    33             out String ReceiptID)
    34         {
    35             ReceiptID = "12345";
    36             return;
    37         }
    38 
    39 
    40 
    41         [WebMethod]
    42         [return: XmlElement("PurchaseOrderRecipt")]
    43         [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare, OneWay = true)]
    44         public void PurchaseOrderStyleBare(PO pOrder)
    45         {
    46             FileStream fileStream = new FileStream("c:/aa.txt",FileMode.Append ,FileAccess.Write);
    47             StreamWriter writer = new StreamWriter(fileStream);
    48             writer.WriteLine(pOrder.ID);
    49             writer.WriteLine(pOrder.Date.ToString());
    50             writer.Close();
    51             return ;
    52         }
    53 
    54 
    55         [WebMethod]
    56         [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Wrapped)]
    57         public double Divide(double x, double y)
    58         {
    59             if (y == 0)
    60             {
    61                 XmlDocument doc = new XmlDocument();
    62                 doc.LoadXml("<BadStuff>you shouldn't try to divide by zero. </BadStuff>");
    63                 XmlQualifiedName code = new XmlQualifiedName("Sample", "http://sample");
    64                 SoapException ex = new SoapException("Can not divide by zero", code, "TheActor", doc);
    65 
    66                 throw ex;
    67             }
    68             return x / y;
    69         }
    70     }
    71 }
    
    
    



    客户端调用代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using ServiceClient.ServiceTest;
     6 using System.Web.Services;
     7 using System.Xml.Serialization;
     8 using System.Web.Services.Protocols;
     9 
    10 
    11 
    12 namespace ServiceClient
    13 {
    14     class Program
    15     {
    16         static void Main(string[] args)
    17         {
    18 
    19 
    20             Service1SoapClient server = new Service1SoapClient();
    21             double resl = server.Divide(4, 3);
    22             Console.WriteLine(resl);
    23 
    24 
    25             ServiceTestClient client = new ServiceTestClient();
    26             double resl1 =  client.Divide(5, 3);
    27             Console.WriteLine(resl1);
    28         }
    29     }
    30 
    31     [WebServiceBinding("Service1Soap", "http://asn.test.cn/")]
    32     public class ServiceTestClient : SoapHttpClientProtocol
    33     {
    34         public ServiceTestClient()
    35         {
    36             this.Url = "http://localhost:49559/Service1.asmx";
    37         }
    38 
    39         [SoapDocumentMethod("http://asn.test.cn/Divide"]
    40         public double Divide(double x, double y)
    41         {
    42             Object[] args = { x, y};
    43             Object[] responseMsg = this.Invoke("Divide", args);
    44 
    45             return (double)responseMsg[0];
    46         }
    47     }
    48 }
    
    
    
     
  • 相关阅读:
    Mybatis——逆向工程
    Mybatis——Spring整合
    Mybatis——缓存机制
    Mbatis——动态SQL
    Mybatis_映射文件配置
    Mybatis XML 配置文件
    渣渣小本求职复习之路每天一博客系列——数据结构与常用算法(2)
    渣渣小本求职复习之路每天一博客系列——数据结构与常用算法(1)
    渣渣小本求职复习之路每天一博客系列——数据库基础(MySQL)(5)
    渣渣小本求职复习之路每天一博客系列——数据库基础(MySQL)(4)
  • 原文地址:https://www.cnblogs.com/asnjudy/p/4026818.html
Copyright © 2020-2023  润新知