如何将 WebService 添加到 C# WinForm?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add WebService to C# WinForm?
提问by Gold
How I can add Webservice to WinForm ?
如何将 Webservice 添加到 WinForm ?
I do not have this Option, why ?
我没有这个选项,为什么?
thank's in advance
提前致谢
回答by billb
When you right click on the Project in Visual Studio, select Add Web Reference. You can then instantiate the web reference in your WinForm.
在 Visual Studio 中右键单击项目时,选择添加 Web 引用。然后,您可以在 WinForm 中实例化 Web 引用。
回答by Jason Coyne
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 服务,请按照 billb 的建议添加 WebReference。
If you want to host a web service, it is not possible to host an ASMX web service. However, it is possible to host a WCF web service.
如果要托管 Web 服务,则无法托管 ASMX Web 服务。但是,可以托管 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();
回答by Prasanna
Follow these steps
按着这些次序
- Right click on the Project in Visual Studio
- Select Add Web Reference
- Enter URL & proceed
- 在 Visual Studio 中右键单击项目
- 选择添加 Web 引用
- 输入网址并继续
When you don't see that option
当您没有看到该选项时
- Right click on the Project in Visual Studio
- Select Add Service Reference
- Press "Advanced" Button
- Press "Add Web Reference" Button
- Enter URL & proceed
- 在 Visual Studio 中右键单击项目
- 选择添加服务引用
- 按“高级”按钮
- 按“添加 Web 参考”按钮
- 输入网址并继续