Self Hosting Code Snippet

The following code illustrates self hosting code snippet.

 1 using System;
 2 using System.ServiceModel;
 3 using System.ServiceModel.Channels;
 4 using System.ServiceModel.Description;
 5 
 6 [ServiceContract]
 7 public interface IMyService
 8 {
 9   [OperationContract]
10   string Say(string name);
11 }
12 
13 public class MyService : IMyService
14 {
15   public string Say(string name)
16   {
17     return name;
18   }
19 }
20 
21 class Program
22 {
23   static void Main(string[] args)
24   {
25     // Specify host to two address endpoints.
26     var baseAddress = new Uri("http://localhost:8732/hello");
27     var netTcpBaseAddress = new Uri("net.tcp://localhost:8733/");
28 
29     using (var host = new ServiceHost(typeof(MyService),
30                                       baseAddress,
31                                       netTcpBaseAddress))
32     {
33 
34       // Enable service discovery
35       var meta = new ServiceMetadataBehavior();
36       meta.HttpGetEnabled = true;
37       //meta.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
38       host.Description.Behaviors.Add(meta);
39 
40       host.Open();
41       Console.ReadLine();
42 
43       host.Close();
44     }
45   }
46 }