4

How I can add Webservice to WinForm ?

I do not have this Option, why ?

thank's in advance

Jason Coyne
  • 6,509
  • 8
  • 40
  • 70
Gold
  • 60,526
  • 100
  • 215
  • 315

3 Answers3

12

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();
Jason Coyne
  • 6,509
  • 8
  • 40
  • 70
7

Follow these steps

  1. Right click on the Project in Visual Studio
  2. Select Add Web Reference
  3. Enter URL & proceed

When you don't see that option

  1. Right click on the Project in Visual Studio
  2. Select Add Service Reference
  3. Press "Advanced" Button
  4. Press "Add Web Reference" Button
  5. Enter URL & proceed
Community
  • 1
  • 1
Prasanna
  • 4,583
  • 2
  • 22
  • 29
3

When you right click on the Project in Visual Studio, select Add Web Reference. You can then instantiate the web reference in your WinForm.

billb
  • 3,608
  • 1
  • 32
  • 36
  • I know this is kind of old now, but since I ended up here from a search. See http://stackoverflow.com/a/12364468/6112 for details on how to get to the 'Add Web Reference' UI option in VS2008. – Peter Bernier Oct 31 '14 at 20:26