C# 如何在异步WCF请求的回调中访问初始参数?
我目前正在更新一个客户端应用程序,该应用程序利用WCF web服务从同步调用升级为异步调用。主服务器和客户端位于同一个本地网络上,但应用程序在等待响应时挂起太不可靠 该应用程序在两台服务器上使用4个相同的端点(因此,如果一个实例崩溃或一台服务器脱机,应该还有一些东西可以调用) 客户端有一个层负责调用web服务。我最初的同步设计是为了调用活动端点,如果抛出异常,我们将移动到下一个端点并递归调用相同的方法。这将一直执行,直到耗尽所有端点 我现在已经做了修改,使之成为异步的,但有一个问题。一旦进入回调,参数就会丢失。因此,当再次递归调用Begin方法时,无法再次传入参数 将参数从Begin方法传递到callback方法的最佳方式是什么?它们是否存储在客户机对象中的任何位置?它可以通过事件完成,还是应该在类级别存储它们C# 如何在异步WCF请求的回调中访问初始参数?,c#,.net,wcf,asynchronous,C#,.net,Wcf,Asynchronous,我目前正在更新一个客户端应用程序,该应用程序利用WCF web服务从同步调用升级为异步调用。主服务器和客户端位于同一个本地网络上,但应用程序在等待响应时挂起太不可靠 该应用程序在两台服务器上使用4个相同的端点(因此,如果一个实例崩溃或一台服务器脱机,应该还有一些东西可以调用) 客户端有一个层负责调用web服务。我最初的同步设计是为了调用活动端点,如果抛出异常,我们将移动到下一个端点并递归调用相同的方法。这将一直执行,直到耗尽所有端点 我现在已经做了修改,使之成为异步的,但有一个问题。一旦进入回调
public delegate void GetUserInfoCompletedEventHandler(UserInfo e);
public static event GetUserInfoCompletedEventHandler GetUserInfoCompleted;
public delegate void GetUserInfoFaultedEventHandler(string errorMessage);
public static event GetUserInfoFaultedEventHandler GetUserInfoFaulted;
public static void BeginGetUserInfo(string fobID)
{
MyClient client = new MyClient(availableEndpoints[activeEndpointIndex].Name);
client.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(client_GetUserInfoCompleted);
client.GetUserInfoAsync(fobID);
}
static void client_GetUserInfoCompleted(object sender, GetUserInfoCompletedEventArgs e)
{
// Get the instance of the client
MyClient client = (MyClient)sender;
if (null == e.Error)
{
// Close the client instance if there was no error
try { client.Close(); }
catch { }
if ((null != GetUserInfoCompleted) && (null != e.Result))
{
// Report as successful and raise the event
ServiceActionSuccessful();
GetUserInfoCompleted(e.Result);
}
}
else
{
// Abort the client as there was an error
try { client.Abort(); }
catch { }
if (e.Error is FaultException<WebServiceError>)
{
FaultException<WebServiceError> fault = (FaultException<WebServiceError>)e.Error;
if (null != GetUserInfoFaulted)
{
// A fault occurred in the web service
GetUserInfoFaulted(fault.Detail.ErrorMessage);
}
}
else
{
// Assume this was problem in connection so test if there any more endpoints to attempt
bool isNextEndpointAvaialble = ServiceActionFailure();
if (isNextEndpointAvaialble)
{
// If there are more endpoints to try, call the method to run again
BeginGetUserInfo(); // Need parameters here
}
else
{
if (null != GetUserInfoFaulted)
{
// No more endpoints to try
GetUserInfoFaulted(Errors.GetUserFriendlyMessage(e.Error));
}
}
}
}
}
public委托void GetUserInfoCompletedEventHandler(UserInfo e);
公共静态事件GetUserInfoCompletedEventHandler GetUserInfoCompleted;
公共委托void GetUserInfoFaultedEventHandler(字符串错误消息);
公共静态事件GetUserInfoFaultedEventHandler GetUserInfoFaulted;
公共静态void BeginGetUserInfo(字符串fobID)
{
MyClient客户端=新的MyClient(可用的Endpoints[activeEndpointIndex].Name);
client.GetUserInfoCompleted+=新事件处理程序(client_GetUserInfoCompleted);
client.GetUserInfoAsync(fobID);
}
静态无效客户端\u GetUserInfoCompleted(对象发送方,GetUserInfoCompletedEventArgs e)
{
//获取客户端的实例
MyClient客户端=(MyClient)发送方;
if(null==e.Error)
{
//如果没有错误,请关闭客户端实例
请尝试{client.Close();}
捕获{}
if((null!=GetUserInfoCompleted)&&(null!=e.Result))
{
//报告成功并提出事件
ServiceActionSuccessful();
GetUserInfoCompleted(如结果);
}
}
其他的
{
//由于出现错误,中止客户端
尝试{client.Abort();}
捕获{}
如果(例如,错误为FaultException)
{
FaultException fault=(FaultException)e.错误;
if(null!=GetUserInfoFaulted)
{
//web服务中发生错误
GetUserInfoFaulted(fault.Detail.ErrorMessage);
}
}
其他的
{
//假设这是连接中的问题,所以测试是否还有其他端点要尝试
bool isnextendpointavailable=ServiceActionFailure();
如果(是否下一个可用点)
{
//如果有更多端点要尝试,请调用该方法以再次运行
BeginGetUserInfo();//此处需要参数
}
其他的
{
if(null!=GetUserInfoFaulted)
{
//没有更多端点可供尝试
GetUserInfoFaulted(Errors.GetUserFriendlyMessage(e.Error));
}
}
}
}
}
如果MyClient
是一个生成的类,那么应该有第二个函数调用
MyClient.GetUserInfoAsync(string fobID, object userState);
userState
参数的内容直接传递给client\u GetUserInfoCompleted
接收的eventargs中的GetUserInfoCompletedEventArgs.userState
属性
所以你可以这样做:
public static void BeginGetUserInfo(string fobID)
{
MyClient client = new MyClient(availableEndpoints[activeEndpointIndex].Name);
client.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(client_GetUserInfoCompleted);
client.GetUserInfoAsync(fobID, fobID);
}
static void client_GetUserInfoCompleted(object sender, GetUserInfoCompletedEventArgs e)
{
string fobID = e.UserState as string;
// handle the event here...
}
如果
MyClient
是一个生成的类,那么应该有第二个调用的函数
MyClient.GetUserInfoAsync(string fobID, object userState);
userState
参数的内容直接传递给client\u GetUserInfoCompleted
接收的eventargs中的GetUserInfoCompletedEventArgs.userState
属性
所以你可以这样做:
public static void BeginGetUserInfo(string fobID)
{
MyClient client = new MyClient(availableEndpoints[activeEndpointIndex].Name);
client.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(client_GetUserInfoCompleted);
client.GetUserInfoAsync(fobID, fobID);
}
static void client_GetUserInfoCompleted(object sender, GetUserInfoCompletedEventArgs e)
{
string fobID = e.UserState as string;
// handle the event here...
}
正是我想要的。谢谢正是我想要的。谢谢