This article mainly introduces in detail a simple example of creating, deploying and calling WebService in C#, which has certain reference value. Interested friends can refer to it
webservice can be used for distributed Interaction between applications, and interaction between different programs.
I won’t talk too much about conceptual things. Let’s start with creating a simple webservice example. Here I am using Visual Studio 2015 development tools.
First create an empty web application.
Then right-click the item and select Add>New Item.
Select the Web service and click Add. A simple webservice has been created. Next, write two simple methods.
PS: If the method needs to be called through the address of the webservice, the [WebMethod] attribute tag must be marked on the method, otherwise it cannot be accessed through the webservice. Description is the description of the method.
Then run it and you will see the method in the picture above. When accessing the webservice, you can call it according to the address in the address bar. webservice method.
But how do you let other applications access it through the network? Then it needs to be deployed to the server.
The following demonstration is deployed on this machine, but the server deployment is the same. Website deployment requires the use of IIS, which has been turned on here. If it is not turned on, you can turn it on through Start Menu > Control Panel > Programs > Enable or turn off windows functions. You can search online for details, so I won’t go into too much detail here.
After opening IIS, first publish the webservice just now, and then open the IIS manager.
Select Custom, enter a profile name, and click Next.
Then select the publishing method and publishing path, and then click Next.
No need to change the settings, continue to the next step. Then click Publish.
The publishing is complete. Now open the IIS Manager, then select the website and click Add Website.
Enter the name of the website, IP address (this machine does not need to enter the IP address) and port, select the path to the file, and then click OK, the website is created.
Of course, it may not be available immediately. There will be many errors. Here are some errors I encountered and some solutions.
This error is due to insufficient access rights to the file. The solution is to click Edit Permissions.
Then select the security bar and click Edit.
Then click to add a user and set the permissions to Allow. Or use the following method:
Select the website, then select Authentication, and click Basic Settings.
Select the connection as.
Then select a specific user, click Settings, enter the username and password of this machine, and click OK. Then you can click Test Settings to see the effect.
The reason for this error is that after installing Framework v4.0, IIS is enabled, resulting in the Framework not being fully installed.
The solution is: Start Menu>Command Prompt (Administrator), then enter and execute %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe - i command line.
This problem is due to the application pool setting. When starting to create a new website, the application pool was not selected. The default is v2.0 version, which does not support Framework. 4.5.
The solution is to select the application pool, then change to v4.0 version, and set the pipeline mode to integrated.
The reason for this error is that the Temp directory under the system directory does not have the corresponding permissions.
The solution is to enter the Temp directory under the Windows directory of the C drive, right-click Properties > Security Bar > Edit > Set the permissions of IIS_IUSRS to Full Control.
The reason for this error is that the default document of the website is not configured.
The solution is to select the website, click Default Document, and then add a default page, which is the start page of the application.
Then, after dealing with many problems, the deployment was finally successful.
Next, let’s take a simple example of calling webservice.
#First add a console application, then right-click the reference and select Add Service Reference.
Click Advanced, then click Add web reference on the pop-up page.
Enter the deployed website address in the URL bar, then click Go, you can see the following method content, and then enter Web citation name, click Add citation.
The reference is added and the webservice is called.
The results are as follows:
The above is the detailed content of Demonstration of how to create, deploy and call WebService instances using C#. For more information, please follow other related articles on the PHP Chinese website!