Articles → WCF → Hosting WCF Using Console Application

Hosting WCF Using Console Application






Software Requirement





Prerequisite Knowledge




  1. What is WCF service?
  2. What is a service contract?
  3. What is an operation contract?
  4. How to create projects using visual studio?

Steps Of Execution




  1. Create a WCF service
  2. Create a console application for hosting service
  3. Create a console client application



Create A WCF Service




Picture showing the sample wcf service project

Click to Enlarge



using System.ServiceModel;

namespace WCFService { 
  [ServiceContract(Name = "MyService", Namespace = "MyNamespace")]
	public interface IHello { 
    [OperationContract(Name = "HelloMethod")]
		string SayHello(string name);
	}
}






namespace WCFService {
	public class Hello: IHello {
		public string SayHello(string name) {
			return string.Format("Hello {0}", name);
		}
	}
}





Create A Console Application For Hosting Service






Picture showing adding the reference of WCF service and  System.ServiceModel assembly in the console application

Click to Enlarge



using System;
using System.ServiceModel;

namespace HostingServer {
	class Program {
		static void Main(string[] args) {
			ServiceHost host = new ServiceHost(typeof(WCFService.Hello));
			host.Open();
			Console.ReadLine();
			host.Close();
		}
	}
}




	<?xml version="1.0" encoding="utf-8" ?><configuration><system.serviceModel><services><service name="WCFService.Hello" behaviorConfiguration="MyBehavior"><host><baseAddresses><add baseAddress="http://localhost:9876/serviceURL"/></baseAddresses></host><endpoint address="" binding="basicHttpBinding" contract="WCFService.IHello"></endpoint><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint></service></services><behaviors><serviceBehaviors><behavior name="MyBehavior"><serviceMetadata/></behavior></serviceBehaviors></behaviors></system.serviceModel></configuration>





Create A Console Client Application








  1. Right click of the console application HostingServer and set the project as start up project
  2. Press Ctrl + F5 to run the application. See the screen shot below
  3. Picture showing running the console client application

    Click to Enlarge

  4. Now right click on the client console application and click on Add Service Reference…
  5. A pop up appears. Inside the Address textbox add the base address which we have added in app.config file of hostingServer console application i.e. http://localhost:9876/serviceURL..
  6. Click on Go. See the screen shot below
  7. Picture showing adding the reference of service to the console client application

    Click to Enlarge

  8. Once you find the service click on Ok. A service reference is added in the project.


using System;

namespace ClientApp {
	class Program {
		static void Main(string[] args) {
			ServiceReference1.MyServiceClient proxy = new ServiceReference1.MyServiceClient();
			Console.WriteLine(proxy.HelloMethod("Karan"));
			Console.ReadLine();
		}
	}
}





Output




Picture showing the output of the WCF service hosted on the console application

Click to Enlarge



  1. Click on the service reference in the client console application
  2. Click on Show all files.
  3. Expand the service reference
  4. Expand the file Reference.svcmap and click on reference.cs. See the screen shot below for proxy class.
Picture showing the Reference.svcmap file from reference.cs

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, July 18, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250