我如何将Web服务添加到WinForm中?
我没有这个选项,为什么?
提前感谢!
您是指要使用Web服务还是托管Web服务?
如果您想使用Web服务,请像billb建议的那样添加Web引用。
如果您想托管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 引用。