【发布时间】:2017-01-26 15:31:58
【问题描述】:
我有以下几点:
public String AttachService(string whereClauseParam)
{
//Get Client object here
Client c = new Client();
string cookieFromRequest = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Cookie];
tokenInfo.TryGetValue(cookieFromRequest, out c);
string[] arr = new string[] { };
c.AttachedServiceStatus += OnAttachedServiceStatus;
string whereClause = whereClauseParam.ToString();
//c.AttachService("binding.interface='query_em'", 8799989);
return string.Format("attached");
}
//处理程序代码如下:
public string OnAttachedServiceStatus(Client sender, ClientServiceAttachedStatus status)
{
if (status.AttachStatus == AttachedStatus.Connected && status.ServiceAttachStatus == ServiceAttachStatus.Attached)
{
//update the Client object in Dictionary
Client c = new Client();
var ou = tokenInfo.First(x => x.Value == sender);
tokenInfo.TryGetValue(ou.Key.ToString(), out c);
tokenInfo.TryRemove(ou.Key.ToString(), out c);
tokenInfo.TryAdd(ou.Key.ToString(), sender);
string[] statusInfoT = new string[200];
statusInfoT[0] = status.ServiceId.ToString();
statusInfoT[1] = status.AttachStatus.ToString();
statusInfoT[2] = status.ServiceAttachStatus.ToString();
statusInfoT[3] = status.VirtualServiceId.ToString();
statusInfoT[4] = status.AttachToken.ToString();
statusInfo.TryAdd(ou.Key.ToString(), statusInfoT);
//update the UI with a Dispatch - TO BE DONE
}
return "Connected";
}
上述 AttachService 方法有一个附加到事件“AttachedServiceStatus”的处理程序“OnAttachedServiceStatus”。 只要 OnAttachedServiceStatus 返回 void,一切正常。但是,我现在需要让处理程序 OnAttachedServiceStatus 返回一个字符串,但我无法正确附加处理程序。
我正在考虑使用 Func 委托,但不知道如何使用它。 请帮忙!
【问题讨论】:
-
返回到哪里,究竟是什么?事件处理程序无效是有原因的,原因是您的代码没有调用它们,因此没有代码可以返回值。
-
我希望处理程序“OnAttachedServiceStatus”将字符串返回给调用者“AttachService”,但我无法得到以下正确
-
c.AttachedServiceStatus += OnAttachedServiceStatus;