在Octopus Deploy中,如何正确添加C#独立脚本?

时间:2018-09-07 16:49:26

标签: c# octopus-deploy

因此,我正在考虑在部署过程中添加C#stand alone script作为步骤的一部分,但是真的很难找到一个参考来表明如何正确进行“格式化”八达通使用的脚本。

我发现的一件事是所有引用都必须明确。因此,例如,如果脚本使用HttpClient发出GET请求,则您不能依靠using语句来缩短引用,而必须使用“完全限定的名称空间”

所以基本上不能做到这一点: HttpClient client = new HttpClient();

您必须这样做: System.Net.Http.HttpClient client = new System.Net.HttpClient()

好的,所以我修改了脚本,以明确引用其给定名称空间中的任何类或方法。

现在,如果我有一个自定义班怎么办?我该如何处理?我将通过一个例子来说明我的意思。说我有以下内容:

using System;

namespace MyOctoScript
{
      class Person
      {
             public string name { get; set; }
      }

     class Script
     {
          static System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
          static System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
          public const string endpoint = "some_valid_endpoint";

           static async System.Threading.Tasks.Task Main(string [] args)
           {
                 MyOctoScript.Person person = null;

                // Use Http Client to fetch JSON from a given endpoint
                System.Net.Http.HttpResponseMessage response = await client.GetAsync(endpoint);

               // Parse JSON from response
               string jsonString = await response.Content.ReadAsStringAsync();

              // Store object in variable of type Person
              person = serializer.Deserialize<MyOctoScript.Person>(jsonString);
           }
     }
}

现在,此脚本可用作控制台应用程序。我想确保将其添加为Step一部分的C#脚本后即可正常工作。

要实现此目的,我需要对上面的代码进行哪些更改(如果有)?

提前感谢任何人!

1 个答案:

答案 0 :(得分:0)

医生说八达通通过ScriptCS支持C#

所以我认为(未经测试)您需要将其展平为这样的内容:

using System;

class Person
{
    public string name { get; set; }
}

static System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
static System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
public const string endpoint = "some_valid_endpoint";

Person person = null;

// Use Http Client to fetch JSON from a given endpoint
System.Net.Http.HttpResponseMessage response = await client.GetAsync(endpoint);

// Parse JSON from response
string jsonString = await response.Content.ReadAsStringAsync();

// Store object in variable of type Person
person = serializer.Deserialize<Person>(jsonString);

tbh,我不确定ScripCS如何处理Async,因此有一些工作要做。

ScriptCS可用于运行独立脚本或作为REPL,因此您可以在本地对其进行测试。