繁体   English   中英

如何将WebService添加到C#WinForm?

[英]How to add WebService to C# WinForm?

如何将Webservice添加到WinForm?

我没有这个选项,为什么?

提前致谢

你的意思是你想要使用网络服务吗? 或者托管网络服务?

如果要使用Web服务,请将WebReference添加为billb建议。

如果要托管Web服务,则无法承载ASMX Web服务。 但是,可以托管WCF Web服务。

(示例不包含任何错误处理或您想要的内容。)

宣布你的合同

[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来提供呼叫

        WebGui webGui = new WebGui();

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

        host.AddServiceEndpoint(typeof(IWebGui), bindings, "");
        host.Open();

跟着这些步骤

  1. 右键单击Visual Studio中的项目
  2. 选择“添加Web引用”
  3. 输入网址并继续

当你没有看到那个选项

  1. 右键单击Visual Studio中的项目
  2. 选择添加服务引用
  3. 按“高级”按钮
  4. 按“添加Web引用”按钮
  5. 输入网址并继续

右键单击Visual Studio中的项目时,选择“添加Web引用”。 然后,您可以在WinForm中实例化Web引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM