如何正确添加 C# 独立脚本作为 Octopus 部署中的一个步骤?



因此,我正在考虑添加C#独立脚本作为部署过程中步骤的一部分,但是很难找到显示如何正确"格式化"脚本以供Octopus使用的参考。

我确实发现的一件事是所有引用都需要明确。因此,例如,如果脚本使用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);
}
}
}

现在,此脚本用作控制台应用程序。我想确保在将其添加为作为步骤一部分的 C# 脚本后它就可以工作。

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

提前感谢任何人!

文档说Octopus通过ScriptCS支持C#

  • https://octopus.com/docs/deployment-examples/custom-scripts#supported-script-types
  • https://github.com/scriptcs/scriptcs

所以我假设(未经测试(您需要将其扁平化为如下所示:

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,因此您可以在本地对其进行测试。

最新更新