C# 使用对WCF的异步调用进行负载测试

C# 使用对WCF的异步调用进行负载测试,c#,visual-studio-2010,wcf,unit-testing,C#,Visual Studio 2010,Wcf,Unit Testing,说到使用VS2010的单元测试,我是新手。我试着做一个单元测试,调用托管的WCF。代码如下所示: ... [TestMethod] public void TestMethod1() { WcfClient client = new WcfClient("BasicHttpBinding_IWcf"); client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(OnGetDataCompl

说到使用VS2010的单元测试,我是新手。我试着做一个单元测试,调用托管的WCF。代码如下所示:

...
[TestMethod]
public void TestMethod1()
{
   WcfClient client = new WcfClient("BasicHttpBinding_IWcf");
   client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(OnGetDataCompleted);
   client.GetDataAsync(arg1, arg2);
}

void OnGetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
   Assert.IfNull(e.Error);
}

...
。。。
[测试方法]
公共void TestMethod1()
{
WcfClient client=新的WcfClient(“BasicHttpBinding_IWcf”);
client.GetDataCompleted+=新的EventHandler(OnGetDataCompleted);
GetDataAsync(arg1,arg2);
}
void OnGetDataCompleted(对象发送方,GetDataCompletedEventArgs e)
{
Assert.IfNull(即错误);
}
...
当我运行它时,它似乎从未启动或完成。我正在考虑把它添加到负载测试中。我是否缺少任何东西来测试对WCF的异步调用?我听说过codeplex中的WCF负载测试,但我将把它留到下一次


peer答案的一个变体:

以下代码将测试您的异步方法,您必须在main thead中等待并在那里进行断言:

[TestMethod]
public void TestMethod1()
{
  WcfClient client = new WcfClient("BasicHttpBinding_IWcf");

  AutoResetEvent waitHandle = new AutoResetEvent(false); 

  GetDataCompletedEventArgs args = null;
  client.GetDataCompleted = (s, e) => {
    args = e.Error;
    waitHandle.Set(); 
  };

  // call the async method
  client.GetDataAsync(arg1, arg2);

  // Wait until the event handler is invoked
  if (!waitHandle.WaitOne(5000, false))  
  {  
    Assert.Fail("Test timed out.");  
  }  

  Assert.IfNull(args.Error);
}

嗨,对不起。当我在这里输入代码时,这是一个错误。我已经在调用client.GetDataAsync()。无论如何,所做的测试仍然是0。我会再试一次。