Net environment development WebService process record

Right-click the project, [Add] - [New Item], in the pop-up dialog box, select " Web Service (ASMX) ", fill in the name of the new service, and click the [Add] button, as shown below:



 
2. After step 1 , code files will be generated in the project, which are *.asmx and *.cs files respectively, and the services provided by Web Service will be implemented in the *.cs file . The *.asmx file defines the configuration information of the Web Service , such as the development language, and the implementation class that provides the service, such as: 

<%@WebServiceLanguage="C#"CodeBehind="AttachmentWS.cs"Class="SmartTMP.AttachmentWS"%>

3. The Web Service implementation class needs to inherit System.Web.Services.WebService .

4. In the service, implement the provided service functions, such as adding an accessory service.

Add a method, annotated with [WebMethod] , as shown below:



 

         There are quite a lot of attributes for WebMethod , and the Description attribute is used here to describe the functions provided by the method, similar to comments.

5. After the service is written, it can be published on the IIS server separately, or it can be deployed with the project. Here, according to the way of deploying the project, after starting the entire project, the WebService service can be used.

6. You can use the following address to access to check whether the release is successful.

http://ip:port/Attachment.asmx或者http://ip:port/Attachment.asmx?wsdl



 

Among them, the above figure lists the service methods that the service can provide to the outside. The following figure provides the wsdl file, including the parameters, parameter types, return values ​​and other information that need to be passed to call the service.



 7. After the service is published, the service can be added to the project through [Add Service Reference] in the project, as shown in the following figure:


 8. After the addition is complete, the functions provided by the service can be used in the project.

9. If you need to use SoapHeader verification to ensure the security of service calls, you can create a new service class that inherits SoapHeader . In the service class, define the attributes to be verified, as shown in the following figure:



 

The above code shows that when authenticating the user called by the service, it is necessary to pass the user's ID , user name, and user password.

10. In the service that requires user authentication, declare the user authentication instance variable defined in step 9 , and add a comment to the method that requires authentication, indicating that the method requires user authentication. As shown below:



 11. In the above code, the user who invokes the service is authenticated at AuthenLoginUser(soapHeader);. The authentication method is customized in AuthenLoginUser , and the user can reasonably implement this part of the function according to the verification method. As shown below:



 
12. After the writing is completed, along with the project release, after adding the service reference in step 7 , it can be used.

13. The simple calling method is as follows:

// Define the WebService service call client

SmartTMP.AttachmentWS.AttachmentWSSoapClient ws = new SmartTMP.AttachmentWS.AttachmentWSSoapClient();
//
Define user authentication instance

SmartTMP.AttachmentWS.AuthenSoapHeader soapHeader = new SmartTMP.AttachmentWS.AuthenSoapHeader();
//
User authentication, pass the information required for authentication, corresponding to step 9 .

soapHeader.CGuid = "xxxxxxx";
soapHeader.UserName = "xxxxxxx";
soapHeader.PassWord = "0000";
// When calling
webService through the client , you need to pass the user authentication instance soapHeader  as a parameter
string strAdd = ws.AddFile(soapHeader , "D:\\a.jpg", "xxxxxx", "xxxxxxx");


The above is just the definition of a simple Web Service service, the process of calling it, and it can be used as you learn. The limited ability is only the skin of the technical link. I hope you can correct me and learn together, thank you.

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154540&siteId=291194637