Host and Run a Basic WCF service In this section we will host and run WCF service. To achieve it, first create a URI (Uniform Resource Identifier) instance for the base address of the service (http://localhost:8000/ServiceModelSamples/Service) and then to host service use the namespace 'System. ServiceModel.Description' and create 'ServiceHost' instance. Finally add an endpoint that exposes the service and enables Metadata Exchange. Following code snippet shows the same:
To use Service Model Metadata Utility Tool (SvcUtil.exe) open VS 2005 command prompt and move to the folder where SvcUtil.exe is placed. Make sure that service is running.
using System.ServiceModel.Description; class Program { static void Main(string[] args) { Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service"); ServiceHost selfHost = new ServiceHost(typeof(PCQuestService), baseAddress); try { selfHost.AddServiceEndpoint( typeof(IPCQuest), new WSHttpBinding(), "PCQuestService"); ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); selfHost.Open(); Console.WriteLine("The service is ready."); Console.WriteLine("Press to terminate service."); Console.WriteLine(); Console.ReadLine(); selfHost.Close(); } catch (CommunicationException ce) { Console.WriteLine("An exception occurred: {0}", ce.Message); selfHost.Abort(); } } } }
Create, Configure, and Run WCF Client To use the service created in previous steps we need a client. To create client, add new 'Console Application 'project to current project and then again add reference to 'System.ServiceModel.dll'. Now run the service created in previous steps and while that service is running use 'SvcUtil.exe' to automatically create client code and configuration file. Service Model Metadata Utility Tool (SvcUtil.exe) can be found at 'C:\Program Files \Microsoft SDKs\Windows\v6.0\Bin'. In case you are not able to find this file, download 'Windows SDK' from MSDN. This is how we used this tool:
To add 'sandeepProxy.cs' and 'sandeep.config' to created client app, right click on 'sandeep1client'. Click on Add>Existing Items.
Svcutil.exe /language:cs /out:sandeepProxy.cs /config:sandeep.config http://localhost:8000/ServiceModelSamples/service
Where '/language:cs' implies that we are using c# language,'/out' changes the name of the generated client proxy, and '/config' changes the name of client configuration file.This will create 'sandeepProxy.cs' and 'sandeep.config' files that need to be added to client program. Right click client project (sandeep 1client) in object explorer and then click on 'Add' and then click on 'Existing Items' and finally add these two files to client project.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.