本文将演示如何在Web服务中使用SoapHeader,同时介绍在VS2010 IDE中不同的两种引用方式。

1)创建空的ASP.NET网站SoapHeaderTest

2)创建类MyCustomSoapHeader继承自SoapHeader,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services.Protocols;

/// <summary>

/// My Custom SoapHeader

/// </summary>

publicclassMyCustomSoapHeader:SoapHeader

{

    /// <summary>

    /// user name for validation

    /// </summary>

    publicstring UserName;

    /// <summary>

    /// user password for validation

    /// </summary>

    publicstring UserPwd;

}

3)新建Web服务MyWebService.asmx;为服务添加自定义字段CutomHeader,并且添加WebMethod Test,设置SoapHeader,最终代码如下:

using System.Web.Services;

using System.Web.Services.Protocols;

 

/// <summary>

/// Summary description forMyWebService

/// </summary>

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

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// Toallow this Web Service to be called from script, using ASP.NET AJAX, uncommentthe following line. 

//[System.Web.Script.Services.ScriptService]

publicclassMyWebService : System.Web.Services.WebService {

    publicMyWebService () {

        //Uncommentthe following line if using designed components 

        //InitializeComponent();

    }

    /// <summary>

    /// input custom header

    /// </summary>

    public MyCustomSoapHeader header;

    /// <summary>

    /// Test method that return user name and password.

    /// </summary>

    /// <returns></returns>

    [WebMethod]

    [SoapHeader("header",Direction=SoapHeaderDirection.In)]

    publicstring Test()

    {

        if(header == null) return "Header is null";

        else return string.Format("Username is {0} and password is {1}.",header.UserName,header.UserPwd);

    }

}

4)新建一个网页Default.aspx

包含一个TextBoxButton控件,当点击Button后,引用Web服务的方法,并将返回值显示在TextBox中,Web页面标记如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>SoapHeader Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="TextBoxReturnString" runat="server" Height="80px"
            TextMode="MultiLine" Width="600px"></asp:TextBox>
        <br />
        <asp:Button ID="ButtonTest" runat="server" Text="Invoke Web Method"
            onclick="ButtonTest_Click" />
    </div>
    </form>
</body>
</html>

5)添加对Web Service的引用:

(点击Discover按钮即可查找到本解决方案的Web服务)

修改ButtonTest_Click方法为:

protected void ButtonTest_Click(object sender, EventArgs e)

    {

       MyWebServiceSoapClient client = new MyWebServiceSoapClient();

       MyServiceReference.MyCustomSoapHeader header = new MyServiceReference.MyCustomSoapHeader();

       header.UserName = "Raymond";

       header.UserPwd = "123";

       this.TextBoxReturnString.Text = client.Test(header);

    }

运行结果:

6)亦可以添加Web服务而非Service Reference:

对于高版本的VSIDE,比如2010,添加Web服务按照如下方式:



相应的方法ButtonTest_Click修改为:

protectedvoidButtonTest_Click(object sender, EventArgs e)

    {

        MyWebServiceReference.MyWebService service = newMyWebServiceReference.MyWebService();

        service.MyCustomSoapHeaderValue = new MyWebServiceReference.MyCustomSoapHeader();

       service.MyCustomSoapHeaderValue.UserName = "Raymond";

        service.MyCustomSoapHeaderValue.UserPwd= "123";

        this.TextBoxReturnString.Text= service.Test();

    }

运行结果与之前一致。



About author
Disclaimer
The opinions and comments expressed herein are my own personal opinions and do not represent my employer's view in any way.
Comments
No comments.
Add comment
Title
Title is required.
Name
Name is required.
Email
Please input your personal email with valid format.
Comments
Please input comment content.
Captcha Refresh
Input captcha:

Subscription

Statistics

Locations of visitors to this page