[英].Net AWS Server Manager
I am trying to build a simple app to start, stop, and change instance types of an AWS Instance.我正在尝试构建一个简单的应用程序来启动、停止和更改 AWS 实例的实例类型。
End goal: Instance Type - Increase or Decrease the Instance Type (t1.micro, t1.large etc) Start/Stop instances -最终目标:实例类型 - 增加或减少实例类型(t1.micro、t1.large 等)启动/停止实例 -
My question at the moment is regarding the code below:我目前的问题是关于下面的代码:
InstanceState Start(RunningInstance instance)
{
using (var client = CreateClient())
{
var confirmStart = MessageBox.Show("Are you sure you want to START this server?", "Start Server", MessageBoxButtons.YesNo);
if (confirmStart == DialogResult.Yes)
{
var doubleCheck = MessageBox.Show("Start the Server?", "Are you sure?", MessageBoxButtons.YesNo);
if (doubleCheck == DialogResult.Yes)
{
var request = new StartInstancesRequest();
request.InstanceId.Add(instance.InstanceId);
var response = client.StartInstances(request);
return response.StartInstancesResult.StartingInstances[0].CurrentState;
}
else
return null;
}
else
return null;
}
}
Basically, double checking if you want to "Start" the server.基本上,仔细检查是否要“启动”服务器。 If the user decides they dont want to start a server, I need it to "Do Nothing" Returning null "Works" for the aspect that nothing changes;
如果用户决定他们不想启动服务器,我需要它“什么都不做”,对于没有任何变化的方面返回空“工作”; however, I am getting a NullReferenceException (Expected).
但是,我收到了 NullReferenceException(预期)。 What can I return in order for this to "Do Nothing" or keep the current state" without using Null?
为了在不使用 Null 的情况下“什么都不做”或保持当前状态,我可以返回什么?
On a side note - I am still new to AWS .Net SDK so any tips would be appreciated.附带说明 - 我仍然是 AWS .Net SDK 的新手,所以任何提示都将不胜感激。
It sounds to me like you could simply just retrieve the current state of the instance in all cases, even if you don't start it.在我看来,即使您没有启动它,您也可以在所有情况下简单地检索实例的当前状态。
To get the instance state of an instance you don't intend to start, you'll need to call another API that describes that instance.要获取您不打算启动的实例的实例状态,您需要调用另一个描述该实例的 API。 There are several that can return the instance state, here are a couple of suggestions for different use cases:
有几种可以返回实例状态,以下是针对不同用例的一些建议:
Here is a minimal example function that you could call from both of your else statements.这是您可以从两个 else 语句调用的最小示例函数。 You would use this to supply the InstanceState in cases where you aren't starting the instance.
在您没有启动实例的情况下,您将使用它来提供 InstanceState。
private InstanceState getInstanceState(string instanceId)
{
using (IAmazonEC2 client = new AmazonEC2Client())
{
var statusResponse = client.DescribeInstanceStatus(new DescribeInstanceStatusRequest()
{
InstanceIds = new List<string>() { instanceId }
});
return statusResponse?.InstanceStatuses?.FirstOrDefault(
x => x.InstanceId == instanceId).InstanceState;
}
}
Note that DescribeInstanceStatus will throw an AmazonEC2Exception with status code 400 BadRequest if the input InstanceId is invalid.请注意,如果输入的 InstanceId 无效,DescribeInstanceStatus 将抛出状态代码为 400 BadRequest 的AmazonEC2Exception 。 You seem like you're already validating the instance's existence, but if intend to have a layer working directly with the AWS API like this you may want to wrap this layer in a try/catch, catch on AmazonEC2Exception, and gracefully handle failures from the AWS API in that manner.
您似乎已经在验证实例的存在,但是如果打算让一个层像这样直接使用 AWS API,您可能希望将该层包装在 try/catch 中,捕获 AmazonEC2Exception,并优雅地处理来自AWS API 以这种方式。
For example, calling to DescribeInstanceStatus could simply be because the InstanceId is invalid, but it could also just be a transient failure or even be due to the EC2 service being down in your region.例如,调用 DescribeInstanceStatus 可能仅仅是因为 InstanceId 无效,但也可能只是暂时性故障,甚至可能是由于您所在地区的 EC2 服务出现故障。 If the EC2 service is not available, your application is likewise not going to be able to do much, so make sure you catch that and communicate it to your users through graceful error handling.
如果 EC2 服务不可用,您的应用程序同样将无法做很多事情,因此请确保您捕获该服务并通过优雅的错误处理将其传达给您的用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.