C# create WCF service programmatically

In this post, I am going to show you how to create a WCF application without using the config file. Let’s start the tutorial.

  • Open visual studio and create a new console application
  • Right-click on the project and add a reference to the following assembly

using System.ServiceModel;

  • Open Program. cs file and paste the following code
[ServiceContract]
    interface IService
    {
        [OperationContract]
        string HelloWorld(string strMessage);
    }
    public class ServiceImp : IService
    {
        [OperationBehavior]
        public string HelloWorld(string strMessage)
        {
            return ("Hello " + strMessage + "!");
        }
    }
  • Now add the following code inside the main method
static void Main(string[] args)
        {
            //Create a URI to serve as the base address
            Uri httpUrl = new Uri("http://localhost:8090/MyService");
            //Create ServiceHost
            ServiceHost host = new ServiceHost(typeof(ServiceImp), httpUrl);
            //Add a service endpoint
            host.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "");
            //Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);
            Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
            //Adding metadata exchange endpoint
            host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
            //Start the Service
            host.Open();
            Console.WriteLine("Host is running... Press  key to stop");
            Console.ReadLine();
}

Now press F5 and see the message. If everything is fine then you will see a message “Host is running …Press key to stop”

Now it’s time to create the Client application

  • Right-click on the solution and create a new console application named WcfClient
  • Open the Program.cs file and paste the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WCFClient
{
    [ServiceContract]
    interface IService
    {
        [OperationContract]
        string HelloWorld(string strMessage);


    }
    class Program
    {
        static void Main(string[] args)
        {
            //Create instance of Binding
            WSHttpBinding myBinding = new WSHttpBinding();
            //Instance of Endpoint address
            EndpointAddress myEndpoint = new EndpointAddress("http://localhost:8090/MyService");
            //Create instance of channel factory
            ChannelFactory<IService> myChannelFactory = new ChannelFactory<IService>(myBinding, myEndpoint);
            // Create a channel.
            IService wcfClient = myChannelFactory.CreateChannel();

            string s = wcfClient.HelloWorld("Wcf");
            Console.WriteLine(s.ToString());
            ((IClientChannel)wcfClient).Close();
            Console.Read();

        }
    }
}

Click below to download complete source code

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru