This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

[Web Service]在Web服务中使用SoapHeader

351 views 0 comments last modified about 8 years ago Raymond Tang

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



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services.Protocols;

/// <summary>

/// My Custom SoapHeader

/// </summary>



    /// <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 = "")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

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


publicclassMyWebService : System.Web.Services.WebService {

    publicMyWebService () {

        //Uncommentthe following line if using designed components 



    /// <summary>

    /// input custom header

    /// </summary>

    public MyCustomSoapHeader header;

    /// <summary>

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

    /// </summary>

    /// <returns></returns>



    publicstring Test()


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

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





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>SoapHeader Test</title>
    <form id="form1" runat="server">
       <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" />

5)添加对Web Service的引用:



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:



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();



Related pages

Set AttachDbFilename as Relative Path in .NET Core

317 views   0 comments last modified about 5 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

45 views   0 comments last modified about 5 months ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

ASP.NET Core 2.1 Error - 'Cyrillic' is not a supported encoding name

279 views   0 comments last modified about 5 months ago

After upgrading to ASP.NET Core 2.1 (.NET Core SDK 2.1.301), you may encounter the following error about encoding: System.ArgumentException &nbsp;&nbsp; HResult=0x80070057 &nbsp;&nbsp; Message='Cyrillic' is not a supported encoding name. For information on defining a custo...

View detail

SQLite in .NET Core with Entity Framework Core

173 views   0 comments last modified about 5 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Tuples in C# (4.x, 7.0, 7.1)

195 views   0 comments last modified about 9 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

C# version history and core features

149 views   0 comments last modified about 9 months ago

C# is my favourite programming language and has been evolving overtime. I have been programming with many other languages but C# is always my favourite because it is simple and easy to use. Visual Studio is the most powerful tool I’ve ever used. To me, programming efficiency and peformance are th...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.