简体   繁体   English

.Net AWS 服务器管理器

[英].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:有几种可以返回实例状态,以下是针对不同用例的一些建议:

  • If you intend to return instance metadata as well, or are otherwise unsure what all info you may need and want a full RunningInstance object, use DescribeInstances.如果您还打算返回实例元数据,或者不确定您可能需要哪些所有信息并想要一个完整的 RunningInstance 对象,请使用 DescribeInstances。
  • If you just need the state, DescribeInstanceStatus will return a smaller response.如果您只需要状态,DescribeInstanceStatus 将返回一个较小的响应。

DescribeInstanceStatus Example DescribeInstanceStatus 示例

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;
    }
}

Error Handling错误处理

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 服务不可用,您的应用程序同样将无法做很多事情,因此请确保您捕获该服务并通过优雅的错误处理将其传达给您的用户。

Further Reading进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google跟踪代码管理器服务器端跟踪.NET - Google Tag Manager server side tracking .NET 如何使用 .NET Core 中的 VPC 端点连接 AWS 机密管理器? - How to connect AWS secret manager using VPC endpoint in .NET Core? .NET Core 2.1 上 AWS Secrets Manager GetSecretValueAsync 上的套接字异常 - Socket exception on AWS Secrets Manager GetSecretValueAsync on .NET Core 2.1 使用 AWS Systems Manager Parameter Store 构建 ASP.NET Core 2.2 Docker 映像 - Build ASP.NET Core 2.2 Docker Image with AWS Systems Manager Parameter Store 使用 AWS 中的 SQL Server 数据库发布 ASP.NET WEB API 2 - Publish ASP.NET WEB API 2 with SQL Server database in AWS GRPC .net 框架 4.6.2 连接 AWS 中托管的服务器的问题 - GRPC .net framework 4.6.2 problems connecting server hosted in AWS .NET软件更新管理器 - .NET Software Update Manager 在不使用脚本管理器或页面方法的情况下,从asp.net 2.0中的javascript调用服务器端函数 - Call a server side function from javascript in asp.net 2.0 without using script manager or pagemethods 学习ASP.Net MVC并在&#39;/&#39;应用程序中构建用户管理器服务器错误 - Learning ASP.Net MVC and building a user manager Server Error in '/' Application 将大文件上传到.Net中的aws S3服务器(asp.net核心) - Upload large file to aws S3 server in .Net (asp.net core)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM