How to:Create wcf application without using config file

In this post i am going to show you how to create wcf application without using config file. Let's start the tutorial.
  • Open visual studio and create a new console application
  • Right click on the project and add reference to following assembly
        using System.ServiceModel;

  • Open Program.cs file and paste 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 following code inside 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 Client application
  • Right click on 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

1 comment:

  1. hai nice article...
    Here you get an startup article for creating WCF application using .netframework 4.0 and visual studio ide 2010.

    http://www.mindstick.com/Articles/a0e46f74-f76e-4804-a3ed-ccd0738c0d36/?Creating Hello World WCF Application

    Thanks.

    ReplyDelete