.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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。