C # simple example of creating, deploying, and calling WebService,

Source: Internet
Author: User

C # simple example of creating, deploying, and calling WebService,

Webservice can be used for interaction between distributed applications and between different programs.

There are not many conceptual things. The following is an example of creating a simple webservice. Here I am using Visual Studio 2015 development tools.

First, create an empty Web application.

Right-click the project and choose add> new item.

Select a Web service and click Add. A simple webservice is created. Next, write two simple methods.

PS: if the method needs to be called through the webservice address, you must add the [WebMethod] feature tag on the method; otherwise, the method cannot be accessed through webservice. Description is the Description of the method.

 

Then run the command and you will see the method. When you access webservice, you can call the webservice method according to the address in the address bar.

 

But how can other applications be accessed over the network? You need to deploy it to the server.

The following example shows that I deployed on the local machine, but the server deployment is the same. IIS is required for website deployment, and I have enabled it here. If it is not enabled, you can enable or disable the windows function through the Start Menu> Control Panel> program>. You can search for it on the Internet. I will not introduce it too much here.

 

After enabling IIS, publish the webservice and then open the IIS manager.

Select custom, enter the configuration file name, and then click Next.

Select the release method and path, and then click Next.

You do not need to change the settings. continue to the next step. Click Publish.

Now the release is complete. Open the IIS manager, select a website, and click Add website.

Enter the website name, IP address (the local machine can not enter the IP address), and port, select the file path, and then click OK to create the website.

 

Of course, you may not be able to use it immediately. There will be many errors. The following are some of the errors and solutions I have encountered.

This error is caused by insufficient file access permissions. The solution is to click Edit permission.

Select the security bar and click Edit.

Click Add a user and set the permission to allow. Or use the following method:

Select a website, select authentication, and click Basic settings.

Select connection.

Select a specific user, click Settings, enter the user name and password of the local machine, and click OK. Then you can click test settings to see the effect.

 

This error occurs when IIS is enabled after Framework v4.0 is installed. As a result, the Framework is not fully installed.

Solution: Start Menu> command prompt (Administrator), and then enter and execute the % windir % \ Microsoft. NET \ Framework \ v4.0.30319 \ aspnet_regiis.exe-I command line.

 

This problem occurs because the application pool is set. When you create a website, the application pool is not selected. The default value is v2.0, and Framework 4.5 is not supported.

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

 

The cause of this error is that the Temp directory in the system directory has no corresponding permissions.

The solution is to go to the Temp directory under the windows directory of drive C, right-click Properties> Security bar> Edit> IIS_IUSRS and set the permission to full control.

 

The reason for this error is that no default document is configured for the website.

The solution is to select a website, click the default document, and add a default page, that is, the starting page of the application.

 

Then, after handling many problems, the deployment was successful.

 

Next, we will provide a simple webservice call example.

Add a console application, right-click the application, and select add service reference.

Click "advanced", and then click "add web reference" on the displayed page.

 

In the URL field, enter the deployed website address and click "go" to view the following method content. Then, enter the web reference name and click "add reference.

After adding the reference, webservice is called.

The result is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.