Skip to content

Commit

Permalink
Update C# samples (#278)
Browse files Browse the repository at this point in the history
- Use Microsoft.Azure.Batch 12.0.
  - Use Microsoft.Azure.Batch.FileStaging 8.3.
  - Use Microsoft.Azure.Batch.Conventions.Files 3.5.1.
  • Loading branch information
matthchr authored Aug 21, 2019
1 parent 2faacfc commit 3926274
Show file tree
Hide file tree
Showing 26 changed files with 68 additions and 88 deletions.
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can contribute to [Azure batch samples](https://github.com/Azure/azure-batch
#### Building the CSharp samples
To build all the CSharp samples without opening Visual Studio, you can use this simple script:
```
for /R %f in (*.sln) do (nuget restore "%f" & msbuild /m "%f")
for /R %f in (*.sln) do (dotnet build "%f")
```

If you want to write the output of the build to a file you can tack on ` >> mybuild.tmp` to the end of the command.
Expand All @@ -24,5 +24,4 @@ If you want to write the output of the build to a file you can tack on ` >> mybu
The Azure Batch samples should be kept up to date with the latest Azure.Batch NuGet package.

##### Using a text replace
The following commands can be used to perform the textual replace: `rep.exe -find:"<PackageReference Include=\"Microsoft.Azure.Batch\" Version=\"8.1.2\" />" -replace:"<PackageReference Include=\"Microsoft.Azure.Batch\" Version=\"9.0.0\" />" -r *.csproj`

The following commands can be used to perform the textual replace: `rep.exe -find:"<PackageReference Include=\"Microsoft.Azure.Batch\" Version=\"10.0.0\" />" -replace:"<PackageReference Include=\"Microsoft.Azure.Batch\" Version=\"12.0.0\" />" -r *.csproj`
4 changes: 2 additions & 2 deletions CSharp/AccountManagement/AccountManagement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Batch" Version="7.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="4.4.2" />
<PackageReference Include="Microsoft.Azure.Management.Batch" Version="9.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.0" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="2.0.0-preview" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.FileStaging" Version="8.2.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.FileStaging" Version="8.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.3.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion CSharp/ArticleProjects/ParallelTasks/ParallelTasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
</ItemGroup>

</Project>
28 changes: 11 additions & 17 deletions CSharp/ArticleProjects/PersistOutputs/BatchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Batch.Common;
using Conventions.Files;
using Microsoft.WindowsAzure.Storage;

public static class BatchExtensions
{
Expand Down Expand Up @@ -43,22 +44,20 @@ public static IEnumerable<CloudTask> CompletedTasks(this CloudJob job)

public static CloudTask WithOutputFile(
this CloudTask task,
CloudJob job,
string pattern,
string containerUrl,
JobOutputKind outputKind,
OutputFileUploadCondition uploadCondition)
{
Func<string> pathFunc = () =>
{
bool patternContainsWildcard = pattern.Contains("*");
return patternContainsWildcard ? $"${outputKind}" : $"${outputKind}\\{pattern}";
};
var outputBase = job.GetOutputStoragePath(outputKind);
bool patternContainsWildcard = pattern.Contains("*");
var path = patternContainsWildcard ? outputBase : $"{outputBase}/{pattern}";

return task.WithOutputFile(
pattern,
containerUrl,
pathFunc,
path,
uploadCondition);
}

Expand All @@ -69,34 +68,29 @@ public static CloudTask WithOutputFile(
TaskOutputKind outputKind,
OutputFileUploadCondition uploadCondition)
{
Func<string> pathFunc = () =>
{
bool patternContainsWildcard = pattern.Contains("*");
return patternContainsWildcard ? $"{task.Id}\\${outputKind}" : $"{task.Id}\\${outputKind}\\{pattern}";
};
var outputBase = task.GetOutputStoragePath(outputKind);
bool patternContainsWildcard = pattern.Contains("*");
var path = patternContainsWildcard ? outputBase : $"{outputBase}/{pattern}";

return task.WithOutputFile(
pattern,
containerUrl,
pathFunc,
path,
uploadCondition);
}

private static CloudTask WithOutputFile(
this CloudTask task,
string pattern,
string containerUrl,
Func<string> pathFunc,
string path,
OutputFileUploadCondition uploadCondition)
{
if (task.OutputFiles == null)
{
task.OutputFiles = new List<OutputFile>();
}

string path = pathFunc();

task.OutputFiles.Add(
new OutputFile(
filePattern: pattern,
Expand Down
12 changes: 6 additions & 6 deletions CSharp/ArticleProjects/PersistOutputs/OutputFilesExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ public static async Task<CloudBlobContainer> RunWithConventions(
private static async Task CreatePoolAsync(BatchClient batchClient, string poolId, int nodeCount)
{
Func<ImageReference, bool> imageScanner = imageRef =>
imageRef.Publisher == "MicrosoftWindowsServer" &&
imageRef.Offer == "WindowsServer" &&
imageRef.Sku.Contains("2012-R2-Datacenter");
imageRef.Publisher.Equals("MicrosoftWindowsServer", StringComparison.InvariantCultureIgnoreCase) &&
imageRef.Offer.Equals("WindowsServer", StringComparison.InvariantCultureIgnoreCase) &&
imageRef.Sku.IndexOf("2012-R2-Datacenter", StringComparison.InvariantCultureIgnoreCase) > -1;

SampleHelpers.SkuAndImage skuAndImage = await SampleHelpers.GetNodeAgentSkuReferenceAsync(batchClient, imageScanner);
ImageInformation imageInfo = await SampleHelpers.GetNodeAgentSkuReferenceAsync(batchClient, imageScanner);

// Create and configure an unbound pool.
CloudPool pool = batchClient.PoolOperations.CreatePool(poolId: poolId,
virtualMachineSize: "standard_d1_v2",
targetDedicatedComputeNodes: nodeCount,
virtualMachineConfiguration: new VirtualMachineConfiguration(
skuAndImage.Image,
skuAndImage.Sku.Id));
imageInfo.ImageReference,
imageInfo.NodeAgentSkuId));

// Commit the pool to the Batch service
await GettingStartedCommon.CreatePoolIfNotExistAsync(batchClient, pool);
Expand Down
4 changes: 2 additions & 2 deletions CSharp/ArticleProjects/PersistOutputs/PersistOutputs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.Conventions.Files" Version="3.3.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.Conventions.Files" Version="3.5.1" />

</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.Conventions.Files" Version="3.3.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.Conventions.Files" Version="3.5.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion CSharp/BatchMetrics/BatchMetrics/BatchMetrics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions CSharp/Common/Microsoft.Azure.Batch.Samples.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.FileStaging" Version="8.2.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.FileStaging" Version="8.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
<ItemGroup>
<None Update="accountsettings.json">
Expand Down
28 changes: 9 additions & 19 deletions CSharp/Common/SampleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static string ConstructContainerSas(
};

string sasString = container.GetSharedAccessSignature(sasPolicy);
return String.Format("{0}{1}", container.Uri, sasString);
return $"{container.Uri}{sasString}";
}

/// <summary>
Expand Down Expand Up @@ -135,7 +135,10 @@ await SampleHelpers.UploadResourcesAsync(
filePaths).ConfigureAwait(continueOnCapturedContext: false);

// Generate resource file references to the blob we just uploaded
string containerSas = SampleHelpers.ConstructContainerSas(cloudStorageAccount, blobContainerName);
string containerSas = SampleHelpers.ConstructContainerSas(
cloudStorageAccount,
blobContainerName,
permissions: SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List);

List<string> fileNames = filePaths.Select(Path.GetFileName).ToList();
List<ResourceFile> resourceFiles = new List<ResourceFile> { ResourceFile.FromStorageContainerUrl(containerSas) };
Expand Down Expand Up @@ -317,25 +320,12 @@ public static async Task<CloudJob> GetJobIfExistAsync(BatchClient batchClient, s
return jobs.FirstOrDefault();
}

public struct SkuAndImage
{
public SkuAndImage(NodeAgentSku sku, ImageReference image)
{
this.Sku = sku;
this.Image = image;
}

public NodeAgentSku Sku { get; }
public ImageReference Image { get; }
}

public static async Task<SkuAndImage> GetNodeAgentSkuReferenceAsync(BatchClient client, Func<ImageReference, bool> scanFunc)
public static async Task<ImageInformation> GetNodeAgentSkuReferenceAsync(BatchClient client, Func<ImageReference, bool> scanFunc)
{
List<NodeAgentSku> nodeAgentSkus = await client.PoolOperations.ListNodeAgentSkus().ToListAsync();
NodeAgentSku nodeAgentSku = nodeAgentSkus.First(sku => sku.VerifiedImageReferences.FirstOrDefault(scanFunc) != null);
ImageReference imageReference = nodeAgentSku.VerifiedImageReferences.First(scanFunc);
List<ImageInformation> imageInformationList = await client.PoolOperations.ListSupportedImages().ToListAsync();

return new SkuAndImage(nodeAgentSku, imageReference);
var result = imageInformationList.First(imageInfo => scanFunc(imageInfo.ImageReference));
return result;
}

public static string GetFailureInfoDetails(TaskFailureInformation failureInfo)
Expand Down
4 changes: 2 additions & 2 deletions CSharp/GettingStarted/01_HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ public async Task RunAsync()
// Get an instance of the BatchClient for a given Azure Batch account.
using (BatchClient batchClient = BatchClient.Open(credentials))
{
// add a retry policy. The built-in policies are No Retry (default), Linear Retry, and Exponential Retry
batchClient.CustomBehaviors.Add(RetryPolicyProvider.ExponentialRetryProvider(TimeSpan.FromSeconds(5), 3));

string jobId = null;

// Track the containers which are created as part of job submission so that we can clean them up later.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.FileStaging" Version="8.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Azure.Batch.FileStaging" Version="8.3.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions CSharp/TextSearch/Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions CSharp/TextSearch/JobSubmitter/JobSubmitter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Batch" Version="10.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.2.0" />
<PackageReference Include="Microsoft.Azure.Batch" Version="12.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 3926274

Please sign in to comment.