2010-10-14 97 views
4

我知道这已被解决之前,但我有服务,返回一个像这样的字符串。异步web服务调用。无(Begin ...)方法可用!

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService] 
public class MyService : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public string Hello() 
    { 
     System.Threading.Thread.Sleep(10000); 
     return "Hello User"; 
    } 
} 

我读过很多例子是说我需要调用的方法是这样的:

 MyService my = new MyService(); 
     AsyncCallback async = new AsyncCallback(callback); 
     my.BeginHello(); 
     Console.WriteLine("Called webservice"); 

的事情是,当我说我参考拿到coudn't的BeginHello方法。我看到的只是HelloAsync。所以我在我的控制台应用程序中使用它。

 MyService my = new MyService(); 
     AsyncCallback async = new AsyncCallback(callback); 
     my.HelloAsync(); 
     Console.WriteLine("Called webservice"); 

和定义如下

private void callback(IAsyncResult res) 
    { 
     Console.Write("Webservice finished executing."); 
    } 

私人回调方法,在此过程中,我得到这样的错误:

的对象引用需要 非静态字段,方法或 属性 'AsyncWebserviceCall.Program.callback(System.IAsyncResult)

为什么我不能得到BeginHello方法&为什么我得到如上的错误?

谢谢你的时间。

+0

请向我们展示整个类(回调和调用web服务的方法) – jgauffin 2010-10-14 11:06:59

回答

9

如果代码被你public static void Main(string[] args)函数内部运行,那么你需要做private void callback(IAsyncResult res)静态方法:

private static void callback(IAsyncResult res) 
{ 
    Console.Write("Webservice finished executing."); 
} 

这就是为什么你得到错误。

从ASP.NET 2.0开始,您对如何进行异步Web服务调用进行了一些更改。做到这一点,而不是:

MyService my = new MyService(); 
my.HelloCompleted += CallBack; 
my.HelloAsync(); 
Console.WriteLine("Called service."); 
Console.ReadLine(); // Wait, otherwise console app will just exit. 

你的回调方法的签名更改为:

private static void CallBack(object sender, HelloCompletedEventArgs e) 
{ 
    Console.WriteLine("Webservice finished executing."); 
} 

更多信息:

在Visual Studio 2005年起添加Web引用代理生成器不再创建BeginXXX/EndXXX方法。这些方法已被弃用,以支持XXXAsync/XXXCompleted模式。

如果你真的需要,你可以使用以下方法之一BeginXXX/EndXXX风格异步方法的工作:

  1. 使用WSDL.exe工具来创建代理。例如:

    wsdl.exe /out:MyService.cs http://somedomain.com/MyService.asmx?wdsl

    包括生成的MyService.cs文件在您的项目并使用它的Web引用。您需要为此打开Visual Studio命令提示符,以便.NET Framework SDK二进制文件位于您的路径中。

  2. 在Visual Studio中显然存在黑客攻击(它可能不再可用)。欲了解更多信息请参阅本MS连接情况:

Begin/End Async WebService Proxy Methods Not Generated in Web Application Projects

我的建议是去拥抱新的方法。

+0

谢谢。这解决了一个问题。那么缺少BeginHello方法呢? – user20358 2010-10-14 11:19:24

+0

谢谢凯夫。所以你说的是ASP.NET 2.0以后,他们将{Begin ...}前缀移除到异步调用? – user20358 2010-10-14 12:18:47

+0

我不认为这条线会起作用:my.HelloCompleted + = CallBack;我会添加一个让我感觉得到的东西。同时这个家伙有两种方法可用? http://www.codeproject.com/Articles/70441/Calling-Web-Service-Functions-Asynchronously-from-.aspx – user20358 2010-10-14 12:19:08

2

这是我在客户端更改以使其工作。

static void Main(string[] args) 
    { 
     MyService my = new MyService(); 
     my.HelloCompleted +=new HelloCompletedEventHandler(my_HelloCompleted); 
     my.HelloAsync(); 
     Console.WriteLine("Called webservice"); 
     Console.ReadKey(); 

    } 

    private static void my_HelloCompleted(object sender, HelloCompletedEventArgs e) 
    { 
     Console.Write("Webservice finished executing in my_HelloCompleted."); 
    } 
+0

很高兴你的工作。 :)从C#2.0开始,你不需要指定'new HelloCompletedEventHandler'。只要:'my.HelloCompleted + = my_HelloCompleted'。 – Kev 2010-10-15 12:13:55

+0

谢谢凯夫。将尝试一下。 :) – user20358 2010-10-19 11:30:01