Friday, February 14, 2014

Create Simple Web Service in Visual Studio

STEP 1 : Create a simple Web service

When creating a New Project, under the language of your choice, select "Web" and then change to .NET Framework to your choice and you will get the option to create an ASP.NET WEB Service Application.



I am naming my WEB Service as MyWebService.
 
Now you can see the "Service1.asmx.cs" file and also a "[WebMethod] HelloWorld()" in it.

You can add your methods as I show below.


Service1.asmx.cs

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

namespace Myservice
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string CheckOddandEvenMethods(int a)
        {
            string results;
            if (a % 2 == 0)
            {
                return results = a + "_" + "Is a Even Number";
            }
            else
            {
                return results = a + "_" + "Is a odd Number";
            }
        }
    }

}

After add WebMethods of your choice, you can run your Service.

STEP 2 : Create Client Program

To create client program select NewProject and select "Console Application" in "Windows" Tab.




I am naming my client "ClientAccess".
Now you need to add a Service Reference so that you can access your web service.
To add service reference:
  1. In Solution Explorer, right-click the name of the project that you want to add the service to, and then click Add Service Reference.
    The Add Service Reference dialog box appears.
  2. Click Discover.
    All services in the current solution are added to the Service list. If not available then manually add them by clicking Advance Tab.
  3. In the Service list, expand the node for the service that you want to use and select a service contract.
  4. In the Namespace box, enter the namespace that you want to use for the reference.
  5. Click OK to add the reference to the project.
  6. A service client (proxy) is generated, and metadata describing the service is added to the app.config file.
After adding service reference write program to use service.


Program.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

using ClientAccess.Mytestapp; // Here you need to write namespace Name and its WebReference Name //

namespace ClientAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            // To call Webservice Here
            Service1 Myapp = new Service1();
            string Result = Myapp.CheckOddandEvenMethods(10);
            Console.WriteLine(Result2);
            Console.ReadLine();
        }
    }
} 

Finally Build Project and Run the Project.
Now you can publish your WebApplication in IIS.

For any query regarding this page Comment below.

0 comments:

Post a Comment

Freemarket.com Marketplace