How to Host an ASP Net Web Service Outside of IIS
Prior to the introduction of WCF, web services developed in .Net had to be hosted within Internet Information Server (IIS). In .Net 3.0, the hosting options for .Net web services increased substantially. WCF includes a class named ServiceHost that allows services to be hosted within an application easily. ServiceHost abstracts many the technological complexity away so developers can focus on the service logic instead of the plumbing involved in hosting services. Hosting a service outside of IIS requires the WCF runtime and a managed .NET application to act as the host. It is the developer's responsibility to write the code that starts and stops the host.
Instructions
-
-
1
Select "New" then "Project" in the File menu. The New Project dialog is displayed.
-
2
Select Visual C# then Windows in from the project template list on the New Project dialog.
-
-
3
Select Console Application from the list of Windows project types. Specify a name for the project in the Name box. The project name should be something related to self hosting a web service if possible.
-
4
Type a location in the location box on the New Project dialog. Alternatively, click "Browse" to navigate to the location where the project will be stored. Click "OK." Visual Studio will create a new console application.
-
5
Right-click SelfHost in Solution Explorer. Then, select Add Reference. Locate System.ServiceModel in the .NET tab and click "OK." This will add a reference to the System.ServiceModel assembly that is required to develop or host WCF services.
-
6
Double-click the class file that was added when the project was created. In most cases it will be named Program.cs. The code for the class will be displayed.
-
7
Right-click on the project name and select "Add New Class." Name the class "HelloWorld" and enter the following code:
using System.ServiceModel;
using System.ServiceModel.Description;
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string SayHello(string name);
}
public class HelloWorld : IHelloWorld
{
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
}
This code is the implementation of a sample "HelloWorld" web service that has one operation, "SayHello."
-
8
Double-click on the class that was automatically added when the project was created. In most cases, the class will be named Program.cs.
-
9
Enter the following code at the top of the class:
using System.ServiceModel;
using System.ServiceModel.Description;
-
10
Enter the following code in the Main method of the class:
//defines the base address for the service on the local machine
Uri baseAddress = new Uri("http://localhost:8080/hello");
// Create the Service Host.
using (ServiceHost host = new ServiceHost(typeof(HelloWorld), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages.
host.Open();
Console.WriteLine("The service is running and can receive request {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
This code starts the service host and creates an instance of the service. The service can be called from a client until the console application is closed.
-
1
Tips & Warnings
WCF services can be hosted in application types other than console applications. Any application such a windows service or forms application that supports WCF can be used to self-host a WCF service.