Step 6: ServiceHost is the core class use to host the WCF service. It will accept implemented contract class and base address as contractor parameter. You can register multiple base addresses separated by commas, but address should not use same transport schema.
Multiple end points can be added to the Service using AddServiceEndpoint() method. Host.Open() will run the service, so that it can be used by any client.
Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator"); Uri tcpUrl = new Uri("net.tcp://localhost:8090/MyService/SimpleCalculator"); ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl, tcpUrl);
Step 7: Below code show the implementation of the host process.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; namespace MyCalculatorServiceHost { class Program { static void Main(string[] args) { //Create a URI to serve as the base address Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator"); //Create ServiceHost ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl); //Add a service endpoint host.AddServiceEndpoint(typeof(MyCalculatorService.ISimpleCalculator) , new WSHttpBinding(), ""); //Enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 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 <Enter> key to stop"); Console.ReadLine(); } } }
Step 8: Service is hosted, now we need to implement the proxy class for the client. There are different ways of creating the proxy
MyCalculatorServiceProxy.cs
- Using SvcUtil.exe, we can create the proxy class and configuration file with end points.
- Adding Service reference to the client application.
- Implementing ClientBase<T> class
MyCalculatorServiceProxy.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using MyCalculatorService; namespace MyCalculatorServiceProxy { public class MyCalculatorServiceProxy : //WCF create proxy for ISimpleCalculator using ClientBase ClientBase<ISimpleCalculator>, ISimpleCalculator { public int Add(int num1, int num2) { //Call base to do funtion return base.Channel.Add(num1, num2); } } }
Step 9: In the client side, we can create the instance for the proxy class and call the method as shown below. Add proxy assembly as reference to the project.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace MyCalculatorServiceClient { class Program { static void Main(string[] args) { MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy ; proxy= new MyCalculatorServiceProxy.MyCalculatorServiceProxy(); Console.WriteLine("Client is running at " + DateTime.Now.ToString()); Console.WriteLine("Sum of two numbers... 5+5 ="+proxy.Add(5,5)); Console.ReadLine(); } } }
Step 10 : End point (same as service) information should be added to the configuration file of the client application.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint address ="http://localhost:8090/MyService/SimpleCalculator" binding ="wsHttpBinding" contract ="MyCalculatorService.ISimpleCalculator"> </endpoint> </client> </system.serviceModel> </configuration>
No comments:
Post a Comment