如何调用Web服务方法? [英] How to call a Web Service Method?

查看:130
本文介绍了如何调用Web服务方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含此方法的Web服务:

I have a web service that contains this method:

[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.

我想要做的,是调用从WinForm应用程序此WebService方法。我加入到web服务的.dll文件的引用,这是我调用上面的方法:

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:

private void Form1_Load(object sender, EventArgs e)
{
    List<string> files = TestUploaderWebService.Service1.GetFileListOnWebServer();
}



上面的代码不工作 - 当它进入的方法,路径Web应用程序为null,以及大量从HostingEnvironment类的属性同样为空。哪里是我的错误,试图从另一个调用winform应用程序的Web服务方法?

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服务在Visual Web Developer速成造的,WinForm的在Visual C#快递;这就是为什么我不得不Web服务的DLL添加为winform应用程序的参考。我没有Visual Studio的全面,这将让我用这两个项目的单一的解决方案。

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服务。

I am new to web services.

PS - 我爱的文本格式上的即时位置:)

PS - i love the formatting of text on-the-fly here :)

推荐答案

在视觉工作室,使用添加Web引用功能,然后在您的Web服务的URL输入。

In visual studio, use the "Add Web Reference" feature and then enter in the URL of your web service.

通过添加对DLL的引用,你不引用它作为一个Web服务,而只是作为一个组件。

By adding a reference to the DLL, you not referencing it as a web service, but simply as an assembly.

当您添加Web引用它在项目中创建一个代理类具有相同或类似的方法/参数作为您的Web服务。该代理类通过SOAP与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服务方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆