WCF A2Z Hands On Lab (HOL) - Tutorial/Lab # 01 (a) - Service

Published Sun, Jan 4 2009 5:41 | abu

Step 1. Created one Console Project

 

Step 2. Created one Project in the same Solution IMyFirstService.csproj for declaring/designing Service contact

 

2.1- Added reference to System.ServiceModel

using System;

using System.Runtime.Serialization;

using System.ServiceModel;

 

namespace KolkataNETWCFHelloWorld

{

    [ServiceContract(Namespace="http://KolkataNET.WCF.HOL")]

    public interface IMyFirstService

    {

        [OperationContract]

        string MyFirstMethod();

    }

}

2.2 Design Simple Service Contract with one

    [ServiceContract(Namespace="http://KolkataNET.WCF.HOL")]

    public interface IMyFirstService

 

2.3 Design Simple Operation [Method/Function] using Attribute [OperationContract] by declaring the method name MyFirstMethod

 

Step 3. Created one Project in the same Solution MyFirstService.csproj for declaring/designing Service

 

3.1- Added reference to System.ServiceModel

using System;

using System.Runtime.Serialization;

using System.ServiceModel;

 

namespace KolkataNETWCFHelloWorld

{

    public class MyFirstService : IMyFirstService

    {

        public string MyFirstMethod()

        {

            return string.Format("Hello World. Welcome KolkataNET!!!");

        }

    }

}

3.2 Design Simple Service implementing the above service Contract

    public class MyFirstService : IMyFirstService

 

3.3 Design Simple Operation [Method/Function]

    public class MyFirstService : IMyFirstService

    {

        public string MyFirstMethod()

        {

            return string.Format("Hello World. Welcome KolkataNET!!!");

        }

    }

 

Step 4. Modified the initial main Console Project to Self Host the Service

 

4.1- Added reference to System.ServiceModel - For hosting the service and exposing MEX - Metadata Exchange of the Service

using System;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Description;

 

namespace KolkataNETWCFHelloWorld

{

    class Program

    {

        static void Main(string[] args)

        {

            Uri svcBaseAddress = new Uri("http://abu:8080/WCFKolkataNET/HOL");

            ServiceHost svcHost = new ServiceHost(typeof(MyFirstService), svcBaseAddress);

            svcHost.AddServiceEndpoint(

                               typeof(IMyFirstService),

                               new BasicHttpBinding(),

                               "MyService");

            ServiceMetadataBehavior svcMetaBehav = new ServiceMetadataBehavior();

            svcMetaBehav.HttpGetEnabled = true;

            svcHost.Description.Behaviors.Add(svcMetaBehav);

            svcHost.Open();

            Console.WriteLine("<ENTER> to stop the service - MyFirstService");

            Console.WriteLine();

            Console.ReadLine();

            svcHost.Close();

        }

    }

}

4.2 Defining Base Address to host the Service

Uri svcBaseAddress = new Uri("http://abu:8080/WCFKolkataNET/HOL");

            ServiceHost svcHost = new ServiceHost(typeof(MyFirstService), svcBaseAddress);

            svcHost.AddServiceEndpoint(

                               typeof(IMyFirstService),

                               new BasicHttpBinding(),

                               "MyService");

 

4.3 Allowing to expose MEX of the Service

            ServiceMetadataBehavior svcMetaBehav = new ServiceMetadataBehavior();

            svcMetaBehav.HttpGetEnabled = true;

            svcHost.Description.Behaviors.Add(svcMetaBehav);

 

4.3 Running the service until <ENTER> key has been pressed

svcHost.Open();

 

4.4 Closing the service when <ENTER> key has been pressed

svcHost.Close();

 

Running the Service - now we will create client to call it

 

Filed under:

Leave a Comment

Name:  
Website: