Self Hosting - (WCF)
Self hosting is the process of hosting a Windows Communication Foundation (WCF) service within a process that is not managed by a Windows service. In this way, the WCF service can run independently of IIS or any other web server.
Syntax
To self-host a WCF service in an application, you will need to create a service class that implements a WCF service contract, and then use a ServiceHost
class to expose the service over a specific endpoint. Here is an example of what the code might look like:
using System;
using System.ServiceModel;
namespace MyNamespace
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
void MyMethod();
}
public class MyService : IMyService
{
public void MyMethod()
{
Console.WriteLine("MyMethod was called.");
}
}
class Program
{
static void Main(string[] args)
{
var host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8000"));
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "MyService");
host.Open();
Console.WriteLine("Service started. Press <Enter> to stop.");
Console.ReadLine();
host.Close();
}
}
}
Example
Here's an example of how to self-host a WCF service in an application:
using System;
using System.ServiceModel;
namespace MyNamespace
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
void MyMethod();
}
public class MyService : IMyService
{
public void MyMethod()
{
Console.WriteLine("MyMethod was called.");
}
}
class Program
{
static void Main(string[] args)
{
var host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8000"));
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "MyService");
host.Open();
Console.WriteLine("Service started. Press <Enter> to stop.");
Console.ReadLine();
host.Close();
}
}
}
Output
When you run the code to self-host your WCF service, you should see output similar to this:
Service started. Press <Enter> to stop.
Explanation
In the example code, we define the service contract and implementation as C# classes, and then use a ServiceHost
object to expose the service over a specific endpoint. We specify the binding and address of the endpoint using the AddServiceEndpoint
method, and then open the ServiceHost
object to start listening for incoming requests. We also provide a prompt for stopping the service using the Console.ReadLine
method.
Use
Self hosting is useful when you need to host a WCF service in a process that is not managed by IIS or any other web server. For example, you might use self hosting to create a standalone console application that exposes a WCF service. You can then use the console application as a simple host for your WCF service.
Important Points
- Self hosting is the process of hosting a WCF service within a process that is not managed by IIS or any other web server.
- A
ServiceHost
object can be used to expose a WCF service over a specific endpoint. - You can use self hosting to create a standalone console application that exposes a WCF service.
Summary
In this page, we discussed self hosting in the context of WCF. We covered the syntax, example, output, explanation, use, important points, and summary of self hosting a WCF service in an application. Self hosting is useful when you need to host a WCF service in a process that is not managed by IIS or any other web server. By using a ServiceHost
object, you can expose a WCF service over a specific endpoint and use it in a standalone console application or other process.