Piemonte Lab

Esperienze di programmazione

Collegarsi ad un webservice svc e richiamare una funzione a runtime direttamente da codice visual studio.

WebServiceHost host = new WebServiceHost(typeof(ServiceReference2.ServiceClient), new Uri("http://www.piemontelab.com/test.svc/"));
            ServiceEndpoint ep = host.AddServiceEndpoint(typeof(ServiceReference2.Service), new WebHttpBinding(), "");

            using (ChannelFactory<ServiceReference2.Service> cf = new ChannelFactory<ServiceReference2.Service>(new WebHttpBinding(), "http://www.piemontelab.com/test.svc"))
            {
                cf.Endpoint.Behaviors.Add(new WebHttpBehavior());

                ServiceReference2.Service channel = cf.CreateChannel();

                string sjson = "{'par1':'value_of_par1'}";
                MemoryStream memStream = new MemoryStream();
                StreamWriter writer = new StreamWriter(memStream);
                writer.Write(sjson);
                writer.Flush();
                memStream.Position = 0;

                var s = channel.function1(memStream);
            }
host.Close();

Il riferimento a 'ServiceReference2' naturalmente è stato ottenuto mediante la funzione 'aggiungi riferimento web'.

Add comment

biuquote
  • Comment
  • Preview
Loading