Wednesday, November 3, 2010

How to consume WCF Restful service in ASP.Net

Under Button Click write this code:

string baseAddress = "http://" + Environment.MachineName + ":8000/Service";

ServiceHost host = new ServiceHost(typeof(SampleImplementation), new Uri(baseAddress));

ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ISampleInterface), new WebHttpBinding(), "");

endpoint.Behaviors.Add(new WebHttpBehavior());

host.Open();

Console.WriteLine("Host opened");



ChannelFactory factory = new ChannelFactory(new WebHttpBinding(), new EndpointAddress(baseAddress));

factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

ISampleInterface proxy = factory.CreateChannel();

SampleDataContract sample = new SampleDataContract();
sample = proxy.GetSampleDataContract(TextBox1.Text);

Response.Write(sample.BranchName );


((IClientChannel)proxy).Close();

factory.Close();

host.Close();

No comments:

Post a Comment