Create WebService in VS2013 and publish and use them in IIS

Source: Internet
Author: User
Create WebService

Step One: Open VS2013, create a new empty project, and pay attention to the version of the. NET Framework. Here I chose the. NET Framework 4


After you create a new item, add a WebService item to your project


Open this file and we can see that the code is as follows

Using System;  
Using System.Collections.Generic;  
Using System.Linq;  
Using System.Web;  
Using System.Web.Services;  
  
Namespace Calculateservice  
{  
    ///<summary>  
    ///WebService1 Summary description  
    ///</summary>  
      
  
    [ WebService (Namespace = "http://tempuri.org/")]  
    [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)]  
    [System.ComponentModel.ToolboxItem (false)]  
    //To allow the use of ASP.net AJAX to invoke this Web service from script, uncomment the line.   
    //[System.Web.Script.Services.ScriptService] public  
    class Calculservice: System.Web.Services.WebService  
    {  
  
        [WebMethod] public  
        string HelloWorld ()  
        {return  
            "Hello World ";}}}  
  

Delete the Hellowworld code and modify it as follows

public class CalCulService:System.Web.Services.WebService  
    {        
        [WebMethod] public  
        int Sum (int a, int b)  
        {return  
            a + b;  
        }  
  
        [WebMethod]  
        public int Sub (int a, int b)  
        {return  
            a-b;  
        }  
  
        [WebMethod]  
        Public double mult (double A, double b)  
        {return  
            a * b;  
        }  
  
        [WebMethod]  
        Public double Div (double A, double) {return a  
            /b  
        }  
    }  

At this point, we have completed this WebService Code section, build the project, run Webservice1.asmx, you can see the service method.

View detailed information (methods, parameters, etc.)

Next we are going to package this webservice for release.
Publish to IIS:

Right-click on the item and click on the publication in the menu

Click the Drop-down menu to select New Profile CONFIG1


Target Location: E:\calcul


Next, don't move.


After completing the steps above, we can see the following files generated in the E:\calcul folder



Configuring IIS

Add a virtual directory under the Web site (the default Web site or your own site Huyweb)


7. Bottom right Corner-Browse the website

8. Possible errors: Directory issues

Solve


Issue 2: Error opening webservice1.asmx, parser error message: Failed to create type ' Webservice1.service1 '.

Workaround: First of all, you must create a virtual directory in IIS (this is needless to say), if this time directly in the browser to call the WebService will appear above the error. The reason is that you must create a separate application for the virtual directory created above, by right-clicking the virtual directory and clicking "Add Application ...", the directory is the same as the physical path to the virtual directory just now, and the webservice can be used normally after the addition is complete.


Then set its default document to Webservice1.asmx. using WebService

Open vs, New project


Add a Web form to your project.

Right-click on the item to add a service reference,


Click Advanced-Add a Web reference.


In the URL, enter the URL address of our published WebService, the system will automatically search for the service. and set a reference name for the Web reference. This reference is the namespace in which we reference this service in our project.

Click the Add reference to complete the reference.

Next, we write the following code in the WEBFORM1 CS file

Huyservice.webservice1 cal = new Huyservice.webservice1 ();
            int a =;
            int b = 5;
            Response.Write (Cal. Sum (A, B). ToString ());

Run.

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.