简体   繁体   English

如何将WebService添加到C#WinForm?

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

How I can add Webservice to WinForm ? 如何将Webservice添加到WinForm?

I do not have this Option, why ? 我没有这个选项,为什么?

thank's in advance 提前致谢

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. 如果要使用Web服务,请将WebReference添加为billb建议。

If you want to host a web service, it is not possible to host an ASMX web service. 如果要托管Web服务,则无法承载ASMX Web服务。 However, it is possible to host a WCF web service. 但是,可以托管WCF Web服务。

(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 然后启动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();

Follow these steps 跟着这些步骤

  1. Right click on the Project in Visual Studio 右键单击Visual Studio中的项目
  2. Select Add Web Reference 选择“添加Web引用”
  3. Enter URL & proceed 输入网址并继续

When you don't see that option 当你没有看到那个选项

  1. Right click on the Project in Visual Studio 右键单击Visual Studio中的项目
  2. Select Add Service Reference 选择添加服务引用
  3. Press "Advanced" Button 按“高级”按钮
  4. Press "Add Web Reference" Button 按“添加Web引用”按钮
  5. Enter URL & proceed 输入网址并继续

When you right click on the Project in Visual Studio, select Add Web Reference. 右键单击Visual Studio中的项目时,选择“添加Web引用”。 You can then instantiate the web reference in your WinForm. 然后,您可以在WinForm中实例化Web引用。

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

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