ASP.NET发布WebService

来源:互联网 发布:js格式工具 编辑:程序博客网 时间:2024/05/17 05:00

ASP.NET发布WebService

一.发布

  1. 建立WebApplication的工程。
  2. 在刚才工程TestWebService中添加WebService文件(后缀为asmx),填写名称。

 打开刚才添加的文件可以看到代码如下:

 

代码
using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

using System.Web.Script.Services;



namespace Sample

{

/// <summary>

/// Summary description for Service1

/// </summary>

[WebService(Namespace
= "http://tempuri.org/")]

[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]

[ToolboxItem(
false)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]

public class Service1 : System.Web.Services.WebService

{



[WebMethod]

public string HelloWorld()

{

return "Hello World";

}



[WebMethod]

public int Add(int a, int b)

{

return (a + b);

}



[WebMethod]

public int Minus(int a, int b)

{

return (a - b);

}

}

}

 

 

 

以上代码需要注意的地方有:

(1).发现该页面中生成了一个类的代码,该类继承自System.Web.Services.WebService

例如:

 

public class Service1 : System.Web.Services.WebService

 

(2).如果要使用asp.net ajax[System.Web.Script.Services.ScriptService]

 

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]

 

 

(3).如果需要将一个函数发布成WebService,需要在public的函数的开头加上[WebMethod]

 

二.测试

直接运行该页面,出现以下页面:

可以看到 Service1是该类的名称,所有发布的WebService名称列在页面首部,下面的文字是对WebService的说明,如果您在asmx的代码中没有修改命名空间,则建议您修改该命名空间。

Service1

 

支持下列操作。有关正式定义,请查看服务说明

  • Add
  • HelloWorld
  • Minus

 Web 服务使用 http://tempuri.org/ 作为默认命名空间。 

建议公开 XML Web services 之前,请更改默认命名空间。 

每个 XML Web services 都需要一个唯一的命名空间,以便客户端应用程序能够将它与 Web 上的其他服务区分开。http://tempuri.org/ 可用于处于开发阶段的 XML Web services,而已发布的 XML Web services 应使用更为永久的命名空间。

应使用您控制的命名空间来标识 XML Web services。例如,可以使用公司的 Internet 域名作为命名空间的一部分。尽管有许多 XML Web services 命名空间看似 URL,但它们不必指向 Web 上的实际资源。(XML Web services 命名空间为 URI。)

使用 ASP.NET 创建 XML Web services 时,可以使用 WebService 特性的 Namespace 属性更改默认命名空间。WebService 特性适用于包含 XML Web services 方法的类。下面的代码实例将命名空间设置为“http://microsoft.com/webservices/”:

C#

[WebService(Namespace="http://microsoft.com/webservices/")]

public class MyWebService {

    // 实现

}

Visual Basic

<WebService(Namespace:="http://microsoft.com/webservices/")> Public Class MyWebService

    ' 实现

End Class

C++

[WebService(Namespace="http://microsoft.com/webservices/")]

public ref class MyWebService {

    // 实现

};

有关 XML 命名空间的更多详细信息,请参阅 Namespaces in XML (XML 命名空间)上的 W3C 建议。

有关 WSDL 的更多详细信息,请参阅 WSDL Specification (WSDL 规范)

有关 URI 的更多详细信息,请参阅 RFC 2396

 

点击Add连接可以打开Add的测试页面,输入参数,单击[调用]按钮即可得到值。

Service1

 

单击此处,获取完整的操作列表。

Add

测试

若要使用 HTTP POST 协议对操作进行测试,请单击“调用”按钮。

参数

a:

b:

 

SOAP 1.1

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Add"
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Add xmlns="http://tempuri.org/">
      <a>int</a>
      <b>int</b>
    </Add>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddResponse xmlns="http://tempuri.org/">
      <AddResult>int</AddResult>
    </AddResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <Add xmlns="http://tempuri.org/">
      <a>int</a>
      <b>int</b>
    </Add>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <AddResponse xmlns="http://tempuri.org/">
      <AddResult>int</AddResult>
    </AddResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP POST

以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service1.asmx/Add HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
 
a=string&b=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://tempuri.org/">int</int>

在IE中您应当看到以下表格中的如果您在Google Chrome中访问会发现只有3,需要查看源代码才能看到以下内容。

<?xml version="1.0" encoding="utf-8" ?>

  <int xmlns="http://tempuri.org/">3</int>