2016-07-15 50 views
-3

私はいくつかのデータを引き出し、別のメソッドで使用するためにグローバル変数に割り当てる非同期メソッドがあります。変数GNAMEとgEmailは既に宣言されているasyncメソッドからグローバル変数を代入するC#

private async void getgoogleplususerdataSer(string access_token) 
    { 
     try 
     { 
      HttpClient client = new HttpClient(); 
      var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token; 

      client.CancelPendingRequests(); 
      HttpResponseMessage output = await client.GetAsync(urlProfile); 

      if (output.IsSuccessStatusCode) 
      { 
       string outputData = await output.Content.ReadAsStringAsync(); 
       GoogleUserOutputData serStatus = JsonConvert.DeserializeObject<GoogleUserOutputData>(outputData); 

       if (serStatus != null) 
       { 
        gName = serStatus.name; 
        gEmail = serStatus.email; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      //catching the exception 
     } 
    } 

: は、以下の方法です。 私は次のロジックを使用してユーザーを登録するには、変数を使用したい:

以下
protected void RegisterGoogleUser() 
    { 
     string strCon = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; 
     string sql5 = "SELECT Email FROM Members where [email protected]"; 
     using (SqlConnection con5 = new SqlConnection(strCon)) 
     { 
      using (SqlCommand cmd5 = new SqlCommand(sql5, con5)) 
      { 
       con5.Open(); 
       cmd5.Parameters.AddWithValue("@gEmail", gEmail); 
       Object result = cmd5.ExecuteScalar(); 
       con5.Close(); 
       if (result != null) 
       { 
        lblMessage.Text = "This e-mail is already registered. If you forgot your password, use forgot password link to recover it."; 
        return; 
       } 
       else 
       { 
        string providerName = "Google"; 
        string conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; 
        string query = "INSERT INTO Members(Name, Email, ProviderName)values(@gName,@gEmail,@providerName)"; 
        using (SqlConnection connection = new SqlConnection(conn)) 
        { 
         using (SqlCommand cmd1 = new SqlCommand(query, connection)) 
         { 
          cmd1.Parameters.AddWithValue("@gName", gName); 
          cmd1.Parameters.AddWithValue("@gEmail", gEmail); 
          cmd1.Parameters.AddWithValue("@providerName", providerName); 
          connection.Open(); 
          cmd1.ExecuteNonQuery(); 
          Session.Add("GEmail", gEmail); 
          Session.Add("CurrentUserName", gName); 
          Response.Redirect("ExternalRegistration.aspx"); 
          connection.Close(); 
         } 
        } 
       } 
      } 
     } 
    } 

がGoogleUserOutputDataクラスであり、Googleのログイン方法

public class GoogleUserOutputData 
    { 
     public string id { get; set; } 
     public string name { get; set; } 
     public string given_name { get; set; } 
     public string email { get; set; } 
     public string picture { get; set; } 
    } 
protected void btnGoogleLogin_Click(object sender, System.EventArgs e) 
    { 
     var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + googleplus_redirect_url + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + googleplus_client_id; 
     Session["Provider"] = "Google"; 
     Response.Redirect(Googleurl); 
    } 

私は取得しています例外がある:「パラメータ化クエリ「( @gEmail nvarchar(4000))EmailからのSELECT Email From Email = @ g 'は、' @ gEmail 'パラメータが指定されていません。

+2

だから、問題は何ですか?コンパイル時エラーまたは実行時エラーが発生しますか?もしそうなら、それを述べてください。 – user3185569

+0

まず、これは間違っています: 'catch(Exception ex){}'。 *例外のあるものを実行します。何か他には、何が間違っているのか分からない。変数を設定していませんか?例外が発生しましたか? –

+2

また、 'async void'を使用しないでください。これはイベントハンドラでない限り' async Task'を使用してください... –

答えて

1

実際にAsynchronous Programmingの使用方法の詳細を読む必要があります。

awaitキーワードを使用しないでgetgoogleplususerdataSer()を呼び出すと、コードブロックの実行はメソッドの終了前でも継続されます。

ので:

private async Task callingMethod() 
{ 
    getgoogleplususerdataSer(); 
    Console.WriteLine(gName); // maybe null, runs before the end of previous method. 
    Console.WriteLine(gEmail); // maybe null 
} 

代わり:

private async Task callingMethod() 
{ 
    await getgoogleplususerdataSer(); // wait for this to end, then continue. 
    Console.WriteLine(gName); // null 
    Console.WriteLine(gEmail); // null 
} 
関連する問題