C# 如何调用 Web 服务方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224672/
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 call a Web Service Method?
提问by Amc_rtty
I have a web service that contains this method:
我有一个包含此方法的 Web 服务:
[WebMethod]
public static List<string> GetFileListOnWebServer()
{
DirectoryInfo dInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/UploadedFiles/"));
FileInfo[] fInfo = dInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly);
List<string> listFilenames = new List<string>(fInfo.Length);
for(int i = 0; i < fInfo.Length; i++)
{
listFilenames.Add(fInfo[i].Name);
}
return listFilenames;
}
This returns a list of filenames in a folder. When i debug the application, it works fine.
这将返回文件夹中的文件名列表。当我调试应用程序时,它工作正常。
What I want to do, is to call this webservice method from a winform application. I added a reference to the .dll of the webservice, and this is how I call the above method:
我想要做的是从 winform 应用程序调用这个 webservice 方法。我添加了对 webservice 的 .dll 的引用,这就是我调用上述方法的方式:
private void Form1_Load(object sender, EventArgs e)
{
List<string> files = TestUploaderWebService.Service1.GetFileListOnWebServer();
}
The above code does not work - when it enters the method, the path of the web app is null, and lots of properties from HostingEnvironment class are also null. Where is my mistake, in trying to call a web service method from another winform app?
上面的代码不起作用 - 当它进入方法时,Web 应用程序的路径为空,并且 HostingEnvironment 类的许多属性也为空。在尝试从另一个 winform 应用程序调用 Web 服务方法时,我的错误在哪里?
Please note that the web service is made in Visual Web Developer Express, and the winform in Visual C# express; this is why I had to add the web service dll as a reference in the winform app. I do not have Visual Studio full, which would have allowed me a single solution with both projects.
请注意,Web 服务是在 Visual Web Developer Express 中制作的,而 winform 是在 Visual C# express 中制作的;这就是为什么我必须在 winform 应用程序中添加 Web 服务 dll 作为参考的原因。我没有完整的 Visual Studio,这可以让我有两个项目的单一解决方案。
I am new to web services.
我是网络服务的新手。
PS - i love the formatting of text on-the-fly here :)
PS - 我喜欢这里即时的文本格式:)
采纳答案by James Conigliaro
In visual studio, use the "Add Web Reference" feature and then enter in the URL of your web service.
在 Visual Studio 中,使用“添加 Web 引用”功能,然后输入 Web 服务的 URL。
By adding a reference to the DLL, you not referencing it as a web service, but simply as an assembly.
通过添加对 DLL 的引用,您不会将其作为 Web 服务引用,而只是作为程序集引用。
When you add a web reference it create a proxy class in your project that has the same or similar methods/arguments as your web service. That proxy class communicates with your web service via SOAP but hides all of the communications protocol stuff so you don't have to worry about it.
当您添加 Web 引用时,它会在您的项目中创建一个代理类,该类具有与您的 Web 服务相同或相似的方法/参数。该代理类通过 SOAP 与您的 Web 服务通信,但隐藏了所有通信协议内容,因此您不必担心。
回答by Steven Sudit
James' answer is correct, of course, but I should remind you that the whole ASMX thing is, if not obsolete, at least not the current method. I strongly suggest that you look into WCF, if only to avoid learning things you will need to forget.
詹姆斯的回答当然是正确的,但我应该提醒你,整个 ASMX 的东西,如果不是过时的,至少不是当前的方法。我强烈建议你研究 WCF,如果只是为了避免学习你需要忘记的东西。
回答by John Saunders
The current way to do this is by using the "Add Service Reference" command. If you specify "TestUploaderWebService
" as the service reference name, that will generate the type TestUploaderWebService.Service1
. That class will have a method named GetFileListOnWebServer
, which will return an array of strings (you can change that to be a list of strings if you like). You would use it like this:
当前执行此操作的方法是使用“添加服务引用”命令。如果您指定“ TestUploaderWebService
”作为服务引用名称,则会生成类型TestUploaderWebService.Service1
。该类将有一个名为 的方法GetFileListOnWebServer
,它将返回一个字符串数组(如果您愿意,可以将其更改为字符串列表)。你会像这样使用它:
string[] files = null;
TestUploaderWebService.Service1 proxy = null;
bool success = false;
try
{
proxy = new TestUploaderWebService.Service1();
files = proxy.GetFileListOnWebServer();
proxy.Close();
success = true;
}
finally
{
if (!success)
{
proxy.Abort();
}
}
P.S. Tell your instructor to look at "Microsoft: ASMX Web Services are a “Legacy Technology”", and ask why he's teaching out of date technology.
PS 告诉您的教师查看“ Microsoft:ASMX Web 服务是一种“遗留技术””,并询问他为什么教授过时的技术。