全部产品
云市场

快速入门

更新时间:2019-07-19 19:39:54

本节介绍如何快速使用OOS C# SDK完成常见操作,如创建模板、启动执行、查询执行等。

创建模板

以下代码用于创建模板:

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Core.Profile;
  5. using Aliyun.Acs.oos.Model.V20190601;
  6. namespace oosDemo
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
  13. DefaultAcsClient client = new DefaultAcsClient(profile);
  14. // 创建模板
  15. var request = new CreateTemplateRequest();
  16. request.TemplateName = "MyTemplate";
  17. request.Content = "{\n"
  18. + " \"FormatVersion\": \"OOS-2019-06-01\",\n"
  19. + " \"Description\": \"Descirbe instances of given status\",\n"
  20. + " \"Parameters\": {\n"
  21. + " \"Status\": {\n"
  22. + " \"Type\": \"String\",\n"
  23. + " \"Description\": \"(Required) The status of the Ecs instance.\"\n"
  24. + " }\n"
  25. + " },\n"
  26. + " \"Tasks\": [\n"
  27. + " {\n"
  28. + " \"Properties\": {\n"
  29. + " \"Parameters\": { \"Status\": \"{{ Status }}\" },\n"
  30. + " \"API\": \"DescribeInstances\",\n"
  31. + " \"Service\": \"Ecs\"\n"
  32. + " },\n"
  33. + " \"Name\": \"foo\",\n"
  34. + " \"Action\": \"ACS::ExecuteApi\"\n"
  35. + " }\n"
  36. + " ]\n"
  37. + "}";
  38. // 发起请求并处理应答或异常
  39. try
  40. {
  41. var response = client.GetAcsResponse(request);
  42. Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
  43. }
  44. catch (ServerException e)
  45. {
  46. Console.WriteLine(e);
  47. }
  48. catch (ClientException e)
  49. {
  50. Console.WriteLine(e);
  51. }
  52. }
  53. }
  54. }

启动执行

以下代码用于启动执行:

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Core.Profile;
  5. using Aliyun.Acs.oos.Model.V20190601;
  6. namespace oosDemo
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
  13. DefaultAcsClient client = new DefaultAcsClient(profile);
  14. // 启动执行
  15. var request = new StartExecutionRequest();
  16. request.Parameters = "{\"Status\": \"Running\"}";
  17. request.TemplateName = "MyTemplate";
  18. // 发起请求并处理应答或异常
  19. try
  20. {
  21. var response = client.GetAcsResponse(request);
  22. Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
  23. }
  24. catch (ServerException e)
  25. {
  26. Console.WriteLine(e);
  27. }
  28. catch (ClientException e)
  29. {
  30. Console.WriteLine(e);
  31. }
  32. }
  33. }
  34. }

查询执行

以下代码用于查询执行:

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Core.Profile;
  5. using Aliyun.Acs.oos.Model.V20190601;
  6. namespace oosDemo
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
  13. DefaultAcsClient client = new DefaultAcsClient(profile);
  14. // 查询执行
  15. var request = new ListExecutionsRequest();
  16. request.ExecutionId = "<ExecutionId>";
  17. // 发起请求并处理应答或异常
  18. try
  19. {
  20. var response = client.GetAcsResponse(request);
  21. Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
  22. }
  23. catch (ServerException e)
  24. {
  25. Console.WriteLine(e);
  26. }
  27. catch (ClientException e)
  28. {
  29. Console.WriteLine(e);
  30. }
  31. }
  32. }
  33. }