Transferred from: http://blog.csdn.net/u012995964/article/details/54562111
This document records WebServices service creation, deployment, and static call methods in C # with SoapHeader validation, Windows8.1
Visual Studio 2013
IIS8
based on, and environment implementation.
WebServices Service Creation
Create WebServices in Visual Studio 2013
- To create an empty
ASP.NET Web 应用程序
:
- Create a program (ASMX format) file for the WebService service:
The first time you right-click Add, you don't see the Web service (ASMX) shown in the diagram, and you can click New Item (W) to create it:
- By the completion of this WebService service creation, you can see the following base code:
- Shortcut keys
F5
or ctrl + F5
run the program as follows:
At this point you can click on the page Hello World
to jump to the HTTP POST
protocol-based call test page:
Click "Invoke" to see the returned results on the new page:
Add SoapHeader Validation
Once the underlying WebService service is created, sometimes the service requires permissions to ensure security, as needed, and is implemented by adding SoapHeader authentication (that is, SOAP header information validation).
1. First of all we need to implement a class with authentication information, this class inherits from System.Web.Services.Protocols.SoapHeader
the following code:
/// <summary> ///Custom Mysoapheader class/// </summary> Public classMySoapHeader:System.Web.Services.Protocols.SoapHeader {Private stringUserName; Private stringPassWord; PublicMysoapheader () {} PublicMysoapheader (stringUserName,stringPassWord) { This. UserName =UserName; This. PassWord =PassWord; } Public stringUserName {Set{userName=value; } Get { returnUserName; } } Public stringPassWord {Set{PassWord=value; } Get { returnPassWord; } } }
2. Modify the WebService class
/// <summary> ///Summary description of WebService1/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)] //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService] Public classWebService1:System.Web.Services.WebService { PublicMysoapheader SoapHeader; [WebMethod (Description="SoapHeader Verification")] [System.Web.Services.Protocols.SoapHeader ("SoapHeader")] Public stringHelloWorld () {//Simple verification of user information//can be verified by database or by other means if("Admin". Equals (soapheader.username) &"admin123". Equals (Soapheader.password)) {return "User authentication passed! "; } Else { return "Sorry, you do not have access rights! "; } } }
This enables the addition of SoapHeader validation, which is noted here to add SoapHeader attributes to the methods in the WebService class . That is, in the code above[System.Web.Services.Protocols.SoapHeader("soapHeader")]
Here is a simple implementation, an advanced implementation, which can be referenced in the tutorial documentation provided by MSDN
WebService Service Deployment
WebService the release of the service program
A well-written web program or service, etc., can be deployed directly to the server via publishing. There is no remote server, so use the local IIS server to run the WebService service. The publishing method is as follows:
The following two configuration defaults, click the Publish button, waiting for the console to display the following prompt, that is, the publishing success:
You can now see the following files in the published directory:
Local IIS Service deployment
The deployment of the on-premises IIS can be described in the previous section of IIS service installation and site deployment in site configuration in Windows8.1.
Post-deployment browsing results are as follows:
Call of WebService
Create client
Creates a console application to invoke the test.
Add Reference
After you create a new project, you need to refer to the WebService service for calling WebService
After you add the reference, enter the following statement in the main method of opening the "Program.cs" file:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacetestservice{classProgram {Static voidMain (string[] args) { //Create a WebService service instanceMywebservices.webservice1soapclient Service =Newmywebservices.webservice1soapclient (); //To Create a custom SoapHeader object instanceMywebservices.mysoapheader Header =NewMywebservices.mysoapheader (); //Service call not set for SoapHeaderConsole.WriteLine ("Service call not set for SoapHeader:"+service. HelloWorld (header)); Console.WriteLine (); //the user name and password are deposited soapheader;Header. UserName ="Admin"; Header. PassWord="admin123"; ////Set service invocation for SoapHeaderConsole.WriteLine ("Service call not set for SoapHeader:"+service. HelloWorld (header)); Console.read (); } }}
After running, the test results are as follows:
Source: C # static invocation of webservices with SoapHeader validation
References and recommendations
For Web services learning, here's what you can see:
- W3school offers a series of tutorials
- MSDN ASP. NET XML Web Services Basics
For some introduction to the IIS Express and local IIS services, you can see here
C # static invocation of webservices with SoapHeader validation