.NET 建立 WebService

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

伺服器端代碼
 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 WebService113 {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.WebService25     {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 ServiceClient13 {14     class Program15     {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 : SoapHttpClientProtocol33     {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 }

 

 

 

 

.NET 建立 WebService

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.