A simple example of creating, deploying and invoking WebService in C ා

Time:2020-10-14

Web service can be used for the interaction between distributed applications and between different programs. Let’s not say too much about conceptual things. Let’s start by creating a simple example of web service.

1The creation and development of webservice

Create a new project first

Continue to add the protagonist of this article to the project, select web services, and click Add. A simple web service is created.

Next, write two simple methods.

PS: if the method needs to be called through the address of WebService, it must be marked with [webmethod] feature tag, otherwise it cannot be accessed through WebService.

Description is the description of the method.

 

Then run it, you can see the method in the figure above. When you visit the web service, you can call the method of web service according to the address in the address bar.

But how can other applications access it over the network? Then you need to deploy to the server.

2: Deploy the WebService service to the server

The following demo is deployed locally, but the same is true for server deployment. IIS is required for website deployment, and I have enabled it here. If it is not enabled, you can start it through Start Menu > Control Panel > programs > enable or disable windows functions. Specific can search on the Internet, here do not do too much introduction.

After opening IIS, first publish the WebService and then open the IIS manager.

Select custom, enter the profile name, and then click next.

Then select the publishing method and path, and then click next.

Do not change the settings. Continue to the next step. Then click publish.

Now open IIS manager, select website, and click Add website.

 

Enter the website name, IP address (this machine can not input IP address) and port, select the file path, and then click OK, the website is created.

Of course, it doesn’t have to be used immediately. There will be a lot of mistakes. Here are some of the mistakes I encountered and some solutions.

This error is due to insufficient access to the file. The solution is to click Edit permissions.

Then select the security bar and click Edit.

 

 

Then click add a user and set the permissions to allow. Or use the following method:

Select the website, then check authentication and click basic settings.

Select connection as.

 

Then select a specific user, click settings, enter the user name and password of the machine, and click OK. You can then click test settings to see the effect.

The reason for this error is that IIS is not fully installed after the framework v4.0 is installed.

The solution is: Start Menu > command prompt (administrator), then type and execute% windir%\ Microsoft.NET \Framework\v4.0.30319\aspnet_ regiis.exe -I) command line.

The problem is that due to the application pool settings, the application pool was not selected when creating a new website. The default version is V2.0, which does not support framework 4.5.

The solution is to select the application pool, then change it to v4.0, and set the pipeline mode to integration.

The reason for this error is that the temp directory in the system directory does not have the corresponding permissions.

The solution is to enter the temp directory under the windows directory of Disk C and right-click Properties > security bar > Edit > IIS_ The permissions of iusrs are set to full control.

The reason for this error is that the default document for the site is not configured.

The solution is to select the site, click on the default document, and then add a default page, the start page of the application.

 

 

Then, after dealing with many problems, the deployment was successful.

3: Calling webservice

Next, let’s take a simple example of calling web service.

First build the simplest application, and then add the reference

Click Advanced, and then click Add web reference on the pop-up page.

Enter the deployed website address in the URL bar, and then click go to to view the following method content. Then enter the web reference name and click Add Reference.

With the addition of reference, the following call is invoked in the form button click event. WebService is called.

private void btnOk_Click(object sender, EventArgs e)
        {
            string  name=txt1.Text;
            WebReference.WebService1 wb = new WebReference.WebService1();
            string str = wb.SelectName(name);
            lblJg.Text = str;
        }

Call result: input Shuke to return Shuke

4: Precautions

When calling between two different machines, theAdd this Code:

The whole process is over. Of course, there are many forms of calling. Here, I only use the simplest one. You can study the rest together.

This oneThe establishment and call test of web service are all on the local machine. If necessary, I can test it in the distributed environment.