Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add WebService to C# WinForm?

How I can add Webservice to WinForm ?

I do not have this Option, why ?

thank's in advance

like image 693
Gold Avatar asked Nov 29 '22 07:11

Gold


1 Answers

Do you mean you want to consume a webservice? Or Host a web service?

If you want to consume a web service, Add WebReference as billb suggested.

If you want to host a web service, it is not possible to host an ASMX web service. However, it is possible to host a WCF web service.

(Example Does not include any error handling or things that you would want.)

Declare your contract

[ServiceContract]
public interface  IWebGui
{
    [OperationContract]
    [WebGet(UriTemplate= "/")]
    Stream GetGrid();
}

Implement your contract

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class WebGui : IWebGui
{

    public Stream GetGrid()
    {

        string output = "test";


        MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(output));
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
        return ms;
    }

}

Then start a WebServiceHost to serve the call

        WebGui webGui = new WebGui();

        host = new WebServiceHost(webGui, new Uri("http://localhost:" + Port));
        var bindings = new WebHttpBinding();

        host.AddServiceEndpoint(typeof(IWebGui), bindings, "");
        host.Open();
like image 85
Jason Coyne Avatar answered Dec 05 '22 06:12

Jason Coyne