This article will give you brief description about required steps to host your wcf
service in IIS and test it using console application.
For creating and hosting WCF service in IIS follow below steps.
See more details about Hosting WCF service in IIS with SSL and Transport Security
Download source code with Service Library, Service Host and client application.
For creating and hosting WCF service in IIS follow below steps.
-
Create WCF service
Create a new service using Create new WCF service library and test using WCFTestClient -
Add Service Host
Add service host for hosting Product Service by right clicking on Project from solution explorer. Name the service host as ProductServiceHost.svc
-
ServiceHost tag
Add below service host tag to ProductServiceHost.svc. Give fully qualified name of service to ServiceHost attribute.<%@ ServiceHost Service="NorthwindServices.ProductService" %>
-
Create Web Site in IIS
Open IIS manager by clicking Windows start -> Run -> enter inetmgr -> click ok If IIS is not installed on your machine click here to install.
Go to IIS manager and right click on sites and select Add Web site.
Enter details as shown in below picture
- Enter site name as NorthwindServices
- Change the application pool to ASP.net V4.0
- Select the physical path of folder containing ProductServiceHost.svc
- Enter port number on you wish to host service.
-
Publish Services
Go to your service application and right click on service project and select publish. -
Test the WSDL
Open your browser and enter http://localhost:7741/ProductServiceHost.svc. You will see the link for WSDL. -
Add Client Application
Now add console application to solution. Name it as NorthwindApp. -
Add Service Reference
Add service reference to NorthwindApp by right click on NorthwindApp project and click on Add Service Reference. Below window should appear. Enter the address of service endpoint which you have added in service app.config.
Enter ProductServiceRef for Namespace and click on OK. Service reference is added to your client application.
Check Client application's App.config, it should have service endpoint and client details. -
Client Implementation
Service reference is now available for Northwind. We can call service operations. Add the below code to NorthwindApp -> Program.cs file.class Program { static void Main(string[] args) { ShowOperations(); } private static void ShowOperations() { ConsoleKeyInfo cki; do { Console.WriteLine(); Console.WriteLine("Enter Product ID or press Esc to quit : "); cki = Console.ReadKey(); if (!char.IsNumber(cki.KeyChar)) { Console.WriteLine(" Invalid number"); } else { Int32 number; if (Int32.TryParse(cki.KeyChar.ToString(), out number)) { Console.WriteLine(); GetProductName(number); GetProductQty(number); GetCategoryName(number); } else { Console.WriteLine("Unable to parse input"); } } } while (cki.Key != ConsoleKey.Escape); Console.Read(); } private static void GetProductName(int ProductID) { ProductsClient client = new ProductsClient(); string ProductName = client.GetProductName(ProductID); Console.WriteLine(string.Format("Product name {0} for Product ID {1}", ProductName, ProductID)); } private static void GetProductQty(int ProductID) { ProductsClient client = new ProductsClient(); int ProductQty = client.GetProductQty(ProductID); Console.WriteLine(string.Format("Product qty {0} for Product ID {1}", ProductQty, ProductID)); } private static void GetCategoryName(int ProductID) { ProductsClient client = new ProductsClient(); string CategoryName = client.GetCategoryName(ProductID); Console.WriteLine(string.Format("Category name {0} for Product ID {1}", CategoryName, ProductID)); } }
See more details about Hosting WCF service in IIS with SSL and Transport Security
Download source code with Service Library, Service Host and client application.
No comments:
Post a Comment