异步调用Web Service

VS2008、C#

今天发现,在.NET 3.5下,异步调用Web Service与以往似乎有些不同。现在生成的代理类少了 BeginXXX 方法,Why?

1. Web Service

[WebMethod]
        
public   string  GetUserName()  {
            
return "zhenglanzhen";
        }


2. 同步调用

class  Program  {
        
static void Main(string[] args) {
            Console.WriteLine(
"step1");
            NameServiceProxy.NameService n 
= new NameServiceProxy.NameService();
            Console.WriteLine(n.GetUserName());
            Console.WriteLine(
"step2");
            Console.Read();
        }

    }


输出:
step1

zhenglanzhen

step2



3. 异步调用

class  Program  {
        
static void Main(string[] args) {
            Console.WriteLine(
"step1");
            NameServiceProxy.NameService n 
= new NameServiceProxy.NameService();
            n.GetUserNameCompleted 
+= delegate(object sender, GetUserNameCompletedEventArgs e) {
                Console.WriteLine(e.Result);
            }
;
            n.GetUserNameAsync();
            Console.WriteLine(
"step2");
            Console.Read();
        }

    }


输出:

step1

step2

zhenglanzhen

你可能感兴趣的:(Web,.net,String,service,C#)