BlogServiceHost.Create()

WCF & Azure - Il blog di Fabio Cozzolino

Recent Posts

Tags

My

Twitter

Community

Archives

Email Notifications

Social Bar Widget

WCF Self-hosting

Ieri, durante l’evento Connected Open Web, ho mostrato la possibilità di fare self-hosting con WCF. Per chi me lo ha poi chiesto, riporto qui il codice completo:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         var host = new ServiceHost(typeof(MyService), new Uri("http://localhost:10101/dotnetside"));
   6:         host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
   7:         host.AddServiceEndpoint(typeof (MyService), new BasicHttpBinding(), "");
   8:         host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
   9:         
  10:         host.Open();
  11:  
  12:         Console.ReadLine();
  13:  
  14:         host.Close();
  15:     }
  16: }
  17:  
  18: [ServiceContract]
  19: public class MyService
  20: {
  21:     [OperationContract]
  22:     public string Get(string name)
  23:     {
  24:         return "Hi, " + name;
  25:     }
  26: }

Questo è il risultato dal WcfTestClient:

image

Fabio