内容目录
首先在要被执行的 类型 加上参数:
[Option(Description = "CloudEvents 'source' (default: urn:example-com:mysource:abc)", LongName = "source", ShortName = "s")]
private string Source { get; } = "urn:example-com:mysource:abc";
[Option(Description = "CloudEvents 'type' (default: com.example.myevent)", LongName = "type", ShortName = "t")]
private string Type { get; } = "com.example.myevent";
[Required,Option(Description = "HTTP(S) address to send the event to", LongName = "url", ShortName = "u"),]
private Uri Url { get; }
可以加上模型验证规则,例如
Required
表示次参数是必填的。
例如,参数 url
,命令行输入:
--url=xxx
--url xxx
被执行的类型,需要定义一个 OnExecuteAsync
方法。
private async Task OnExecuteAsync()
{
var cloudEvent = new CloudEvent
{
Id = Guid.NewGuid().ToString(),
Type = Type,
Source = new Uri(Source),
DataContentType = MediaTypeNames.Application.Json,
Data = JsonConvert.SerializeObject("hey there!")
};
var content = cloudEvent.ToHttpContent(ContentMode.Structured, new JsonEventFormatter());
var httpClient = new HttpClient();
// Your application remains in charge of adding any further headers or
// other information required to authenticate/authorize or otherwise
// dispatch the call at the server.
var result = await httpClient.PostAsync(Url, content);
Console.WriteLine(result.StatusCode);
}
执行:
public static int Main(string[] args)
{
var result = CommandLineApplication.Execute<MyProgram>(args);
return 0;
}
文章评论