2

I have declared a RegionEndpoint on my app config like this

<add key="AWSRegion" value="EUWest1" xdt:Transform="Insert"/>

And on my program i'm tryna call this AWSRegion

 private static string RegionEndPoint = ConfigurationManager.AppSettings["AWSRegion"];

So when i declare my SQSClient i get an error that, Cannot convert from string to RegionEndPoint

var SQSClient = new AmazonSQSClient(credentials, RegionEndPoint);

i have tried making sense of this question here How to set the EndPoint / Region for the C# .NET SDK : EC2Client?

but its for EC2Client so it doesn't seem to work out for me.

i have also tried to declare the regionendpoint using the AWS SDK like this

private static RegionEndpoint RegionEndPoint = ConfigurationManager.AppSettings["AWSRegion"];

i also get an error which says cannot implicitly convert from type string to Amazon.RegionEndpoint

So if theres a way to add a RegionEndPoint in a config file and use it in an SQSClient please help me.

Kid_Nick
  • 185
  • 2
  • 15

1 Answers1

7

ConfigurationManager.AppSettings["AWSRegion"] returns string value EUWest1 but RegionEndpoint is a class.

AmazonSQSClient constructo expects an instance of class RegionEndpoint as second parameter but since you are passing string value, you are getting the error.

You can get the RegionEndpoint class instance from Configuration value using following approach.

RegionEndpoint class has a static method GetBySystemName. You need to pass the region name to this method and it would return appropriate instance of RegionEndpoint class.

But for this you need to have proper region name to be passed. You need to put that value in configuration. In your case, the configuration value should be eu-west-1. Configuration value EUWest1 will not work here.

You can find all the valid region names of AWS here

<add key="AWSRegion" value="eu-west-1" xdt:Transform="Insert"/>

With above configuration value you can use following code to get the Region based on it.

private static RegionEndpoint RegionEndPoint = 
      RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["AWSRegion"]);

Above code will give you RegionEndPoint instance for EU (Ireland) Region region and now you can use it for AmazonSQSClient as following.

var SQSClient = new AmazonSQSClient(credentials, RegionEndPoint);

This will help you resolve the issue you are facing.

Chetan Ranpariya
  • 5,895
  • 3
  • 16
  • 27