【问题标题】:How to add WebService to C# WinForm?如何将 WebService 添加到 C# WinForm?
【发布时间】:2009-04-16 14:22:10
【问题描述】:

如何将 Web 服务添加到 WinForm 中?

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

先谢谢了

【问题讨论】:

    标签: c# winforms web-services


    【解决方案1】:

    你的意思是你想使用一个网络服务?还是托管网络服务?

    如果您想使用 Web 服务,请按照 billb 的建议添加 WebReference。

    如果您想托管 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();
    

    【讨论】:

      【解决方案2】:

      按照这些步骤操作

      1. 在 Visual Studio 中右键单击项目
      2. 选择添加网络参考
      3. 输入网址并继续

      当您没有看到该选项时

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我知道这已经有点老了,但因为我是通过搜索找到这里的。有关如何在 VS2008 中访问“添加 Web 引用”UI 选项的详细信息,请参阅 stackoverflow.com/a/12364468/6112
        猜你喜欢
        • 1970-01-01
        • 2012-02-04
        • 2021-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-06
        • 2016-11-10
        相关资源
        最近更新 更多