Loading

Windows Communication Foundation (WCF)

How to hosted WCF service is in a console application?. The Complete Windows Communication Foundation (WCF) Developer Course 2023 [Videos].

Here, the WCF service is hosted in a console application. Given below is the process with suitable steps in a sequential manner that explains the entire process.

Step 1 âˆ’ First, lets create the Service contract and its implementation. Create a console application and name it as MyCalculatorService. This is a simple service to return the addition of two numbers.

Wcf Hosting Services Self 1

Step 2 âˆ’ Now, right click on the References in the Solution Explorer and click Add References. The following window opens; add System.ServiceModel reference to the project.

Wcf Hosting Services Self 2

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()]
   Public 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 {
      Public int Add(int num1, int num2) {
         return num1 + num2;
      }
   }
}

Step 6 âˆ’ Now, we are ready with the service. Lets go for implementing the hosting process. Create a new console application and name it as MyCalculatorWCFServiceHost.

Wcf Hosting Services Self 5

Step 7 âˆ’ Add the reference of system.servicemodel and the project MyCalculatorWCFService.

Wcf Hosting Services 6

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();
      }
   }
}

Wcf Hosting Services Self 8


See All

Comments (746 Comments)

Submit Your Comment

See All Posts

Related Posts

Windows Communication Foundation (WCF) / Blog

What is WCF?

WCF stands for Windows Communication Foundation. It is basically used to create a distributed and interoperable Application. WCF Applications came into the picture in .Net 3.0 Framework. This is a framework, which is used for creating Service oriented Applications. You can send the data asynchronously from one end point to another. I think you all know about Web Service and are thinking, if we already have Web Services and accessing on a remote basis then why did WCF come into picture?
22-Apr-2022 /50 /746

Windows Communication Foundation (WCF) / Blog

Major differences that exist between WCF and a Web service.

WCF (Windows Communication Foundation): WCF, as the name suggests, is a unified .NET framework that is used to develop service-oriented applications. It allows you to develop applications that can communicate using different communication mechanisms.
22-Apr-2022 /50 /746

Windows Communication Foundation (WCF) / Blog

Difference between WCF and Web API.

WCF is used to create a distributed and interoperable Applications. It provides a framework which is used for building service-oriented-connected applications for the transmission of the data as an asynchronous, from one service-point to other service-point. Previously known as Indigo and is a framework for building, configuring, and deploying network-distributed services.
22-Apr-2022 /50 /746