如何将WebService添加到C#WinForm? [英] How to add WebService to C# WinForm?

查看:83
本文介绍了如何将WebService添加到C#WinForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Web服务添加到WinForm?

How I can add Webservice to WinForm ?

我没有此选项,为什么?

I do not have this Option, why ?

先谢谢

推荐答案

您是说要使用Web服务?还是托管网络服务?

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

如果您要使用Web服务,请按照建议的方式添加WebReference.

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

如果要托管Web服务,则不能托管ASMX Web服务.但是,可以托管WCF Web服务.

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.)

声明您的合同

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

履行合同

[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;
    }

}

然后启动一个WebServiceHost来服务该呼叫

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();

这篇关于如何将WebService添加到C#WinForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆