C# ultra-detailed WebService creation, publishing and calling (VS2022)

This article was created with vs2022, which supplements and enriches the content. If you want to see the 2019 version, you can refer to it. Visual Studio 2019 creates WebServiceCSDN

1.Write interface

Open VS2022 and create a new project. Here I choose "ASP.NET Web Application (.NET Framework)".

There is a note here: If "ASP.NET Web Application (.NET Framework)" does not exist in your vs option, you need to re-add the corresponding reference. There is no ASP.NET Web application (.NET Framework) when creating a new project in VS2022

Fill in the project name, select the project location and the framework used. Here I use ".NET Framework 4.7.2" Frame and click Create.

Open Solution Explorer-right-click the created web project-Add-New Item-Add web service (AMSX).

The default is the HelloWorld method, you can add several methods yourself.

​Then we start the project. Above we can see the five methods we wrote. I select one of them and click on it. After clicking on the call, we can see that "7" is output.

What needs to be noted here is that if the localhost is reported to be intercepted after starting locally. You need to pay attention to whether https in advanced is turned on when creating the project. If you don't remember, it doesn't matter. Just open the properties of the project directly, modify the project URL in the Web, change https to http, and restart the project.

2. Release interface

Right-click the project, select Publish, select a folder, click Next, and select the folder location for publishing. You can choose it yourself, but be sure to remember the location you choose. Click Finish, and then we click Publish.

Note: There is a small detail here. When we update the content in the Web service, we need to pay attention when publishing it again.

Open the location where the project is located, and the content inside is the content we published.

Then we open the IIS manager (Build our own local IIS server).

Note: At this time we found that there was an error in the website we opened.

404.3 error. The reason for this problem is that the IIS service is not configured. The simplest and crudest solution is to install IIS. The steps are as follows:

Control Panel-"Turn Windows Functions On or Off-"Internet Information Services-"World Wide Web Services-"Application Development Functions, check ".net Extensibility" and "ASP.NET", save and restart the IIS server.

403.14 error, we just need to enable directory browsing.

At this point, we have completed the steps of publishing the website. Next, we will call the publishing method in the program and obtain the corresponding value.

3. Call the interface

Right-click the project-Add-Service Reference, open the website you just started, copy and paste this address into the service reference.

Next clickAdvanced,Add Web Reference (W) - Enter the URL you just copied in the URL in the opened interface - click Blue arrow - add reference, you can see the service reference we added in the solution explorer.

Next, we simply add a console application project to implement the method call. Create a new console program, right-click the reference in the control diagram application, and use the reference in the program.

In this way, we have completed the creation, publishing and use of a web service.

Guess you like

Origin blog.csdn.net/VS18703761631/article/details/133794140