2016-08-30 20 views
0

RESTful APIを使用してプールを作成しようとしています。私は、バッチサービスのC#ライブラリがあることを知っていますが、プログラミングでサブネットIDを指定するには、RESTful APIを使用してthis MSDN articleで作成してください。 {"Id":"DotNetPool","vmSize":"small"}Azureバッチを使用してAzureバッチプールを作成するRestful API、例外が発生する

System.Net.WebClient.UploadDataInternalで(ウリアドレス:

マイポストURI形式

https://{account-name}.{region-id}.batch.azure.com/pools?api-version={api-version}

コード

using (var client = new WebClient()) 
{ 
    client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
    client.Headers[HttpRequestHeader.Authorization] = "SharedKey <AccountName>:<Signature>"; 
    client.Headers[HttpRequestHeader.Date] = DateTime.UtcNow.ToString(); 
    try 
    { 
     result = client.UploadString(baseURI, "POST", json); 
    } 
    catch(Exception ex) 
    { 
     Console.WriteLine(ex.StackTrace); 
    } 
    Console.WriteLine(result); 
} 

私が送信されたJSON

に従ってください、Stringメソッド、Byte []データ、WebRequest at System.Net.WebClient.UploadString(Uriアドレス、Stringメソッド、Stringデータ)
at System.Net.WebClient.UploadString(String address、Stringメソッド、Stringデータ) at batchServer.Program.createPool(Stringパラメーターpoolid、文字machineSize、文字osFamily、列サブネット識別子、文字列コマンドライン、のInt32 numberOfMachine、List`1 resourceFiles)C中:batchServer \ Program.csを\ \ユーザー\ fange \ダウンロード\ ALMTestマスタ:ライン61

誰か助けてくれますか?

+0

この問題は解決しましたか?どんな更新? –

答えて

2

あなたが提供したコードに基づいて、私は私の側でテストし、この問題を再現しました。あなたがコードをデバッグする場合は、次のように詳細なエラーを見つけることができる:

私の知る限りでは、いくつかの共通のヘッダーが制限みなされ、システムによって保護されており、Aに設定または変更することはできませんWebHeaderCollectionオブジェクトの場合は、このtutorialに従うことができます。

簡単な方法として、目的を達成するためにWebClientの代わりにHttpWebRequestを使用することをお勧めします。ここでは、RESTful APIを使用してプールを作成するためのテストコードを示します。

public static void CreatePoolViaRestAPI(string baseUrl, string batchAccountName, string batchAccountKey,string jsonData) 
{ 
    string verb = "POST"; 
    string apiVersion= "2016-07-01.3.1"; 
    string ocpDate= DateTime.UtcNow.ToString("R"); 
    string contentType = "application/json; odata=minimalmetadata; charset=utf-8"; 
    string reqUrl = string.Format("{0}/pools?api-version={1}", baseUrl, apiVersion); 

    //construct the request 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqUrl); 
    request.Method = verb; 
    //Set ContentType 
    request.ContentType = contentType; 
    //Set ocp-date 
    request.Headers.Add("ocp-date", ocpDate); 
    var buffer = Encoding.UTF8.GetBytes(jsonData); 
    request.ContentLength = buffer.Length; 

    #region generate the signature 
    string CanonicalizedHeaders = string.Format("ocp-date:{0}", ocpDate); 
    string CanonicalizedResource = string.Format("/{0}/pools\napi-version:{1}", batchAccountName, apiVersion); 
    string stringToSign = string.Format("{0}\n\n\n{1}\n\n{2}\n\n\n\n\n\n\n{3}\n{4}", 
     verb, 
     buffer.Length, 
     contentType, 
     CanonicalizedHeaders, CanonicalizedResource); 
    //encode the stringToSign 
    string signature = EncodeSignStringForSharedKey(stringToSign, batchAccountKey); 
    #endregion 

    //Set Authorization header 
    request.Headers.Add("Authorization", string.Format("SharedKey {0}:{1}", batchAccountName, signature)); 
    using (var rs = request.GetRequestStream()) 
    { 
     rs.Write(buffer, 0, buffer.Length); 
    } 

    //send the request and get response 
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    { 
     Console.WriteLine("Response status code:{0}", response.StatusCode); 
    } 
} 

:cloudServiceConfigurationとvirtualMachineConfiguration特性が相互に排他的であり、特性のいずれか一方のみを指定することができます。いずれも指定されていない場合、バッチ・サービスはBad Request(400)を返します。したがって、次のように、上記の関数でjsonDataパラメータは次のようになります。

"{\"id\":\"DotNetPool\",\"vmSize\":\"small\",\"cloudServiceConfiguration\":{\"osFamily\":\"4\"}}" 

UPDATE:

public string EncodeSignStringForSharedKey(string stringToSign, string accountKey) 
{ 
    HMACSHA256 h = new HMACSHA256(Convert.FromBase64String(accountKey)); 
    var byteArray = h.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); 
    string signature = Convert.ToBase64String(byteArray); 
    return signature; 
} 

詳細をあなたは次のようにstringToSignを符号化するための

方法は次のようになりますはAuthentication via Shared Keyに続く可能性があります。

+0

"'' EncodeSignStringForSharedKey"関数はどこにありますか? – spevilgenius

+0

私はちょうどコードスニペットで私の答えを更新しました、あなたはそれを参照することができます。 –

1

5.0.0以降のAzure Batch C#Client SDKには、Windowsクラウドサービスベースのインスタンス用の仮想ネットワークに参加する機能があります。 RESTエンドポイントを直接呼び出す必要はありません。

- Added support for joining a CloudPool to a virtual network on using the NetworkConfiguration property.

現在地5.0.0はChangeLogを表示することができます。https://www.nuget.org/packages/Azure.Batch/5.0.0が、最新バージョンを使用してください。

関連する問題