- WCF - Exception Handling
- WCF - Security
- WCF - RIA Services
- WCF - Transactions
- WCF - Instance Management
- WCF - Service Binding
- WCF - Consuming WCF Service
- WCF - Windows Service Hosting
- WCF - WAS Hosting
- WCF - Self-Hosting
- WCS - IIS Hosting
- WCF - Hosting WCF Service
- WCF - Creating WCF Service
- WCF - Architecture
- WCF - Developers Tools
- WCF - Versus Web Service
- WCF - Overview
- WCF - Home
WCF Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
WCF - Self Hosting
Here, the WCF service is hosted in a console apppcation. Given below is the process with suitable steps in a sequential manner that explains the entire process.
Step 1 − First, let s create the Service contract and its implementation. Create a console apppcation and name it as MyCalculatorService. This is a simple service to return the addition of two numbers.
Step 2 − Now, right cpck on the References in the Solution Explorer and cpck Add References. The following window opens; add System.ServiceModel reference to the project.
Step 3 − Create an ISimpleCalculator interface, Add ServiceContract and OperationContract attribute to the class and function as shown below. You will know more about these contracts in the later session. These contracts will expose the method to the outside world for using this service.
Step 4 − The code behind this file is as follows −
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace MyCalculatorWCFService { [ServiceContract()] Pubpc interface ISimpleCalculator { [OperationContract()] int Add(int num1, int num2); } }
Step 5 − MyCalculatorService is the implementation class for IMyCalculatorService interface as shown below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyCalculatorWCFService { Class SimpleCalculator : ISimpleCalculator { Pubpc int Add(int num1, int num2) { return num1 + num2; } } }
Step 6 − Now, we are ready with the service. Let s go for implementing the hosting process. Create a new console apppcation and name it as MyCalculatorWCFServiceHost .
Step 7 − Add the reference of system.servicemodel and the project MyCalculatorWCFService.
The code behind this is as follows −
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MyCalculatorWCFService; using System.ServiceModel; using System.ServiceModel.Description; namespace MyCalculatorWCFServiceHost { class Program { static void Main(string[] args) { //Create a URI to serve as the base address UrihttpUrl = newUri("http://localhost:8090/MyCalculatorWCFService/SimpleCalculator"); //Create ServiceHost ServiceHost host = newServiceHost(typeof(MyCalculatorWCFService.ISimpleCalculator), httpUrl); //Add a service endpoint host.AddServiceEndpoint(typeof(MyCalculatorWCFService.ISimpleCal culator), newWSHttpBinding(), ""); //Enable metadata exchange ServiceMetadataBehaviorsmb = newServiceMetadataBehavior(); smb.HttpGetEnabled = true; host.Description.Behaviors.Add(smb); //Start the Service host.Open(); Console.WriteLine("Service is host at " + DateTime.Now.ToString()); Console.WriteLine("Host is running... Press key to stop"); Console.ReadLine(); } } }Advertisements