VS2010 Create WebService----The first day (build the project, and do not connect the database subtraction)

Source: Internet
Author: User
Tags visual studio 2010

The default framework for Visual Studio 2010 is . NET Framework4, where you can't find a template that directly creates a webservice. But VS2010 can create WebService is no doubt, then how to create WebService in VS2010, please see below for you to provide two methods, method has I test, can rest assured use!

method One:under the. NET Framework4.0 Framework, you can still create WebService with the following steps:

① Select ASP. NET empty Web application <asp.net Empty Web application> "template, set up a blank website or project ;

        

② Add New Item, in the "Add New Item " window, you will find WebService this project template.

       

       

  

Method Two: change the default framework used by VS2010 to use the framework . NET framework2.0\3.0\3.5, when you create a new project or create a new Web site You can find the "ASP. NET WebService template " to build the webservice.

      

adopted . net2.0\3.0\3.5 Create WebService, if you need to use a new feature of. NET Framework4.0 at this point, you can select. NET in the Build tab of the Project Properties window or Web Site Properties window . Framework4.0.

      

Here is the default code, you can implement the calling interface, print out "Hello world", and the following browser open interface

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;

Namespace Webservicetwo
{
<summary>
Summary description of Service1
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[System.ComponentModel.ToolboxItem (False)]
To allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
[System.Web.Script.Services.ScriptService]
public class Service1:System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld ()
{
Return "Hello World";
}

  }    }

Click on the "HelloWorld" call interface to go to the following interface:

Then click on the "Invoke" button to see the Web service results returned in XML format. Explains that our web service environment is not a problem, but also initial exposure to the simplest Web service.

Two: Write yourself a webservice, calculate subtraction

In the code file, if we write a function that we want this function to be an externally callable interface function, we must add a line of code [WebMethod (Description of the description= "function)] to the function, and if your function does not have this declaration, It will not be referenced by the user. Down we started writing an example of a simple Web Service.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;

Namespace Webservicetwo
{
<summary>
Summary description of Service1
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[System.ComponentModel.ToolboxItem (False)]
To allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
[System.Web.Script.Services.ScriptService]
public class Service1:System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld ()
{
Return "Hello World";
}
[WebMethod (Description = "method of summation")]
Public double addition (double I, Double j)
{return i + j;}
[WebMethod (Description = "Method of finding Difference")]
Public double subtract (double I, Double j)
{return i-j;}
[WebMethod (Description = "method of Quadrature")]
public double multiplication (double I, Double j)
{return i * j;}
[WebMethod (Description = "Method of Seeking quotient")]
Public Double division (double I, Double j)
{
if (J! = 0)
return i/j;
Else
return 0;
}
}
}

Run by:

Also click on the addition method to enter the call page of the addition method.

Enter the parameter i=6,j=5 on the parameter, for example, click Invoke to see the Web service results returned in XML format (the result of the addition of I and J)

Get here , we will find , actually WebService It's not that mysterious. , It's just an interface . , for our part , , the focus is on the writing of interface functions .Three: one of the errors

Causes of errors and solutions

The cause of the error, the class name of the default Webservise class is Service1, the following instantiation needs to instantiate this class, if not it will be an error
Please note <%@ WebService language= "C #" codebehind= "~/app_code/service.cs" class= "Service"% >
Class= "Service1", the Service1 must be the class name you defined below, and if you use a different class name there will be an error

VS2010 Create WebService----The first day (build the project, and do not connect the database subtraction)

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.