[英]Webhook callback getting unexpected response 301 from Azure Function
[英]Getting callback from class function
我試圖弄清楚如何等待來自 class function 的回調(我猜)。
我的 class 看起來像這樣:
public class DataLogOut
{
public delegate void OnLogoutResponse(ResponseData data);
public event OnLogoutResponse onLogoutResponse;
public static void LogoutPlayer()
{
new EndSessionRequest().SetDurable(true).Send((response) => {
if (!response.HasErrors)
{
GS.Reset();
if (onLogoutResponse != null)
{
onLogoutResponse(new ResponseData()
{
//data = response
});
}
}
else
{
if (onLogoutResponse != null)
{
onLogoutResponse(new ResponseData()
{
errors = response.Errors,
hasErrors = response.HasErrors
});
}
}
});
}
}
現在我這樣稱呼“LogOutPlayer”:
public void LogOut()
{
DataLogOut.LogoutPlayer();
DataLogOut.onLogoutResponse += onLogoutResponse;
}
現在我在兩個腳本中都遇到了錯誤。 首先我得到:委托調用可以簡化
...在第二個腳本中,我不允許這樣做:
DataLogOut.onLogoutResponse += onLogoutResponse;
真的希望有人可以幫助我,並在此先感謝:-)
您的代碼中有幾個問題。 請看評論:
public class DataLogOut
{
// No need for this, we will use "EventHandler"
// public delegate void OnLogoutResponse(ResponseData data);
//public event OnLogoutResponse onLogoutResponse; -> replaced by
public event EventHandler<ResponseData> onLogoutResponse;
// Convenience Method to fire the event
protected virtual void OnLogoutResponse( ResponseData data )
{
var handler = onLogoutResponse;
if( handler != null ){
handler( this, data );
}
}
// Let's simplify it by making it non-static
//public static void LogoutPlayer()
public void LogoutPlayer
{
new EndSessionRequest().SetDurable(true).Send((response) => {
if (!response.HasErrors)
{
GS.Reset();
OnLogoutResponse(new ResponseData()
{
//data = response
});
}
else
{
OnLogoutResponse(new ResponseData()
{
errors = response.Errors,
hasErrors = response.HasErrors
});
}
});
}
}
用法:
public void LogOut()
{
// I made it non-static, so we need an instance ...
var logout = new DataLogout();
// first register for the event, then have it fired.
logout.onLogoutResponse += onLogoutResponse;
// ^-- You tried to register the handler on the class. Which failed,
// because the event was not static.
logout.LogoutPlayer();
}
// the handler's signature now must look like this:
public void onLogoutResponse( object sender, ResponseData data ){
// your code here
}
如果您想保留它static
,那么也制作事件 static :
public static event EventHandler<ResponseData> onLogoutResponse;
那么你也需要讓便利事件觸發 static
protected static void OnLogoutResponse( ResponseData data )
{
var handler = onLogoutResponse;
if( handler != null ){
handler( typeof(DataLogout), data ); // cannot use "this", of course in static context.
}
}
然后可以像在您的示例中一樣使用它:
public void LogOut()
{
// first register for the event, then have it fired.
DataLogout.onLogoutResponse += onLogoutResponse;
// ^-- You tried to register the handler on the class. Which failed,
// because the event was not static.
DataLogout.LogoutPlayer();
}
// the handler's signature now must look like this:
public void onLogoutResponse( object sender, ResponseData data ){
// your code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.