[英]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();
右鍵單擊Visual Studio中的項目時,選擇“添加Web引用”。 然后,您可以在WinForm中實例化Web引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.