簡體   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