Tuesday, August 6, 2013

Easy way to test WCF and REST services - use WebServiceHost class

  static void Main(string[] args)
        {
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
            try
            {
                ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "");
                host.Open();
                using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new BasicHttpBinding(), "http://localhost:8000"))
                {
                   // cf.Endpoint.Behaviors.Add(new bas WebHttpBehavior());
                    IService channel = cf.CreateChannel();
                    herby h = channel.getHerby();
                    Console.WriteLine("   i: {0}", h.i );
                    Console.WriteLine("   s: {0}", h.s);
                    Console.WriteLine("");

                    Boris b = channel.getBoris();
                    Console.WriteLine("   i: {0}", h.i);
                    Console.WriteLine("   s: {0}", h.s);
                    Console.WriteLine("");
                }
                Console.WriteLine("Press <ENTER> to terminate");
                Console.ReadLine();
              
                host.Close();
            }
            catch (CommunicationException cex)
            {
                Console.WriteLine("An exception occurred: {0}", cex.Message);
                host.Abort();
            }
        }


No comments:

Post a Comment