This article will give you a brief description of the 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.
Create a new service using Create new WCF service library and test using WCFTestClient
Add service host for hosting Product Service by right clicking on Project from solution explorer. Name the service host as ProductServiceHost.svc
Add below service host tag to ProductServiceHost.svc. Give a fully qualified name of service to the ServiceHost attribute.
<%@ ServiceHost Service="NorthwindServices.ProductService" %>
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
Go to your service application and right click on service project and select publish.
Enter target location as http://localhost:7741/
Open your browser and enter http://localhost:7741/ProductServiceHost.svc. You will see the link for WSDL.
Now add a console application to solution. Name it as NorthwindApp.
Add service reference to the NorthwindApp by right click on NorthwindApp project and click on Add Service Reference. The Add Service Reference 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 the Client application's App.config, it should have service endpoint and client details.
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