发布到IIS7.0时,GoogleWebAuthorizationBroker.AuthorizeAsync超时

时间:2015-07-06 11:28:33

标签: c# asp.net async-await

将我的代码发布到IIS7.0后,我遇到了GoogleWebAuthorizationBroker.AuthorizeAsync超时问题。

通过我的开发环境,一切都按预期工作。我会看到Google权限屏幕,代码会在App_data中创建预期的令牌文件。然后我可以插入和更新日历条目。 但是,当我发布代码时,我收到来自GoogleWebAuthorizationBroker.AuthorizeAsync的请求超时错误。

我手动将从我的开发环境创建的令牌文件复制到Web服务器上,代码工作正常。我可以在日历中创建活动。

我监控了防火墙,没有任何东西被阻止。

如果我删除Web服务器上的令牌文件的内容。我没有被提示重新授权并且我回到请求超时。

任何人都有任何关于什么出错的建议?

ClientSecrets client = new ClientSecrets
{
    ClientId = SessionState.SystemSettings[SystemSettingKey.GoogleAccountClientId].Value,
    ClientSecret = SessionState.SystemSettings[SystemSettingKey.GoogleAccountClientSecret].Value

};

IList<string> scopes = new List<string>();
scopes.Add(CalendarService.Scope.Calendar);
var credPath = HttpContext.Current.Server.MapPath("~/App_Data/");          
var credential =
    GoogleWebAuthorizationBroker.AuthorizeAsync(client, scopes, account, CancellationToken.None, new FileDataStore(credPath, true))
        .Result;

// Create the calendar service using an initializer instance
BaseClientService.Initializer initializer = new BaseClientService.Initializer
{
    HttpClientInitializer = credential,
    ApplicationName = "App1"
};

1 个答案:

答案 0 :(得分:1)

我会在这里感受到一种痛苦的感觉,并说你有一个经典的死锁场景,因为你在这里blocking on an async method

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                              client, 
                              scopes, 
                              account,                         
                              CancellationToken.None,
                              new FileDataStore(credPath, true)).Result;

您不应该在异步方法上使用.Result,您应该始终使用await异步等待:

public async Task AuthorizeAsync()
{
   ClientSecrets client = new ClientSecrets
   {
       ClientId = SessionState.
                        SystemSettings[SystemSettingKey.GoogleAccountClientId].Value,
       ClientSecret = SessionState.
                        SystemSettings[SystemSettingKey.GoogleAccountClientSecret].Value

   };

   IList<string> scopes = new List<string>();
   scopes.Add(CalendarService.Scope.Calendar);
   var credPath = HttpContext.Current.Server.MapPath("~/App_Data/");  

   var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                                 client, 
                                 scopes, 
                                 account,
                                 CancellationToken.None,
                                 new FileDataStore(credPath, true));

   // Create the calendar service using an initializer instance
   BaseClientService.Initializer initializer = new BaseClientService.Initializer
   {
      HttpClientInitializer = credential,
      ApplicationName = "App1"
   };
}