zoukankan      html  css  js  c++  java
  • .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 }
    
    
    
     
  • 相关阅读:
    SharePoint 2013 安装.NET Framework 3.5 报错
    SharePoint 2016 配置工作流环境
    SharePoint 2016 站点注册工作流服务报错
    Work Management Service application in SharePoint 2016
    SharePoint 2016 安装 Cumulative Update for Service Bus 1.0 (KB2799752)报错
    SharePoint 2016 工作流报错“没有适用于此应用程序的地址”
    SharePoint 2016 工作流报错“未安装应用程序管理共享服务代理”
    SharePoint JavaScript API in application pages
    SharePoint 2016 每天预热脚本介绍
    SharePoint 无法删除搜索服务应用程序
  • 原文地址:https://www.cnblogs.com/asnjudy/p/4026818.html
Copyright © 2011-2022 走看看