如何将WebService添加到C#WinForm?

Gol*_*old 4 c# web-services winforms

如何将Webservice添加到WinForm?

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

提前致谢

Jas*_*yne 12

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

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

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

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

宣布你的合同

[ServiceContract]
public interface  IWebGui
{
    [OperationContract]
    [WebGet(UriTemplate= "/")]
    Stream GetGrid();
}
Run Code Online (Sandbox Code Playgroud)

执行你的合同

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

}
Run Code Online (Sandbox Code Playgroud)

然后启动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();
Run Code Online (Sandbox Code Playgroud)


Pra*_*nna 7

跟着这些步骤

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

当你没有看到那个选项

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