Step Over throws exception, but Step Into not Unity - c#

I am making a unity game and have one strange situation, which I will try to explain.
Here is my UserCreator class in which I want to return nativeCountry from another class (MySQLCountryManager):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class UserCreator : MonoBehaviour
{
public static PersonModel CreateUser(PersonModel model)
{
CountryModel nativeCountry = new CountryModel();
nativeCountry = MySQLCountryManager.GetCountryByName(model.NativeCountry);
<some other code here....>
}
}
And here is MySQLCountryManager class with GetCountryByName method:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class MySQLCountryManager : MonoBehaviour
{
public static CountryModel GetCountryByName(string countryName)
{
CountryModel country = new CountryModel();
WWWForm form = new WWWForm();
form.AddField("CountryName", countryName);
UnityWebRequestAsyncOperation getCountryByName = new UnityWebRequestAsyncOperation();
getCountryByName = UnityWebRequest.Post(WebReguests.getCountryByName, form).SendWebRequest();
List<string> results = new List<string>();
if (getCountryByName.webRequest.isNetworkError || getCountryByName.webRequest.isHttpError)
{
Debug.LogError(getCountryByName.webRequest.error);
}
else
{
var data = getCountryByName.webRequest.downloadHandler.text;
string[] result = data.Split(',');
for (int i = 0; i < result.Length; i++)
{
results.Add(result[i]);
}
int id;
bool idOk;
idOk = int.TryParse(results[0], out id );
if (idOk)
{
country.id = id;
}
country.CountryName = results[1];
byte[] flagBytes = Convert.FromBase64String(results[2]);
country.Flag = flagBytes;
byte[] shapeBytes = Convert.FromBase64String(results[3]);
country.Shape = shapeBytes;
byte[] woP = Convert.FromBase64String(results[4]);
country.WorldPosition = woP;
byte[] coa = Convert.FromBase64String(results[5]);
country.CoatOfArms = coa;
byte[] dishPicBytes = Convert.FromBase64String(results[6]);
country.DishPic = dishPicBytes;
byte[] curiosityBytes = Convert.FromBase64String(results[7]);
country.CuriosityPic = curiosityBytes;
country.Continent = results[8];
country.Population = results[9];
country.Capital = results[10];
country.Language = results[11];
country.Currency = results[12];
country.Religion = results[13];
country.DishName = results[14];
}
return country;
}
Now, the problem is, when I start debugging project in Visual Studio 2019 if I go Step Into on native country in UserCreator and then Step Into in MySQLCountryManager.GetCountryById, code works fine and returns nativeCountry as I expect. But when I go Step Over nativeCountry, it always throws some exceptions, like 'Index was out of range', 'Input string is not in a correct format' etc. And this is happening for all methods with UnityWebRequest called from UserCreator class.
I tried to google for this but nothing useful was found.
Any idea why this is happening?

tl;dr: Sounds like a race condition to me!
While stepping into the method you most probably give the asynchronous request just enough time to actually finish in the background.
I don't see where you are waiting for the results of the asynchronous download.
See the examples of UnityWebRequest.Post => as with any other asynchronous execution you have to wait until the results are actually back.
Usually you would use a Coroutine with callback like e.g.
public static void GetCountryByNameAsync(MonoBehaviour caller, string countryName, Action<CountryModel> whenDone)
{
caller.StartCoroutine(GetCountryByNameRoutine(countryName, whenDone))
}
private static IEnumerator GetCountryByNameRoutine(string countryName, Action<CountryModel> whenDone)
{
var form = new WWWForm();
form.AddField("CountryName", countryName);
using(var request = UnityWebRequest.Post(WebReguests.getCountryByName, form))
{
// WAIT until the request has either failed or succeeded
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error);
yield break;
}
var data = request.downloadHandler.text;
var results = data.Split(',');
var country = new CountryModel();
if (int.TryParse(results[0], out var id))
{
country.id = id;
}
country.CountryName = results[1];
var flagBytes = Convert.FromBase64String(results[2]);
country.Flag = flagBytes;
var shapeBytes = Convert.FromBase64String(results[3]);
country.Shape = shapeBytes;
var woP = Convert.FromBase64String(results[4]);
country.WorldPosition = woP;
var coa = Convert.FromBase64String(results[5]);
country.CoatOfArms = coa;
var dishPicBytes = Convert.FromBase64String(results[6]);
country.DishPic = dishPicBytes;
var curiosityBytes = Convert.FromBase64String(results[7]);
country.CuriosityPic = curiosityBytes;
country.Continent = results[8];
country.Population = results[9];
country.Capital = results[10];
country.Language = results[11];
country.Currency = results[12];
country.Religion = results[13];
country.DishName = results[14];
whenDone?.Invoke(country);
}
}
then later you would rather use it like e.g.
// caller will be whatever script is actually calling this method
// we will take that reference to make it also responsible for executing the according Coroutine
public static void CreateUserAsync(MonoBehaviour caller, PersonModel model, Action<PersonModel> whenDone)
{
MySQLCountryManager.GetCountryByName(caller, model.NativeCountry, nativeCountry =>
{
<some other code here....>
whenDone?.Invoke(theCreatedPersonModel);
});
}
Or if it gets more complex instead of Coroutines you might rather use an async - await approach and pass the final result back into the Unity main thread.

Related

How to have an AWS Lambda/Rekognition Function return an array of object keys

This feels like a simple question and I feel like I am overthinking it. I am doing an AWS project that will compare face(s) on an image to a database (s3bucket) of other faces. So far, I have a lambda function for the comparefacerequest, a class library which invokes the function, and an UWP that inputs the image file and outputs a result. It has worked so far being based on boolean (true or false) functions, but now I want it to instead return what face(s) are recognized via an array. I struggling at implementing this.
Below is my lambda function. I have adjusted the task to be an Array instead of a bool and changed the return to be an array. At the bottom, I have created a global variable class with a testing array so I could attempt to reference the array elsewhere.
public class Function
{
//Function
public async Task<Array> FunctionHandler(string input, ILambdaContext context)
{
//number of matched faces
int matched = 0;
//Client setup
var rekognitionclient = new AmazonRekognitionClient();
var s3client = new AmazonS3Client();
//Create list of target images
ListObjectsRequest list = new ListObjectsRequest
{
BucketName = "bucket2"
};
ListObjectsResponse listre = await s3client.ListObjectsAsync(list);
//loop of list
foreach (Amazon.S3.Model.S3Object obj in listre.S3Objects)
{
//face request with input and obj.key images
var comparefacesrequest = new CompareFacesRequest
{
SourceImage = new Image
{
S3Object = new S3Objects
{
Bucket = "bucket1",
Name = input
}
},
TargetImage = new Image
{
S3Object = new S3Objects
{
Bucket = "bucket2",
Name = obj.Key
}
},
};
//compare with confidence of 95 (subject to change) to current target image
var detectresponse = await rekognitionclient.CompareFacesAsync(comparefacesrequest);
detectresponse.FaceMatches.ForEach(match =>
{
ComparedFace face = match.Face;
if (match.Similarity > 95)
{
//if face detected, raise matched
matched++;
for(int i = 0; i < Globaltest.testingarray.Length; i++)
{
if (Globaltest.testingarray[i] == "test")
{
Globaltest.testingarray[i] = obj.Key;
}
}
}
});
}
//Return true or false depending on if it is matched
if (matched > 0)
{
return Globaltest.testingarray;
}
return Globaltest.testingarray;
}
}
public static class Globaltest
{
public static string[] testingarray = { "test", "test", "test" };
}
Next, is my invoke request in my class library. It has so far been based on the lambda outputting a boolean result, but I thought, "hey, it is parsing the result, it should be fine, right"? I do convert the result to a string, as there is no GetArray, from what I know.
public async Task<bool> IsFace(string filePath, string fileName)
{
await UploadS3(filePath, fileName);
AmazonLambdaClient client = new AmazonLambdaClient(accessKey, secretKey, Amazon.RegionEndpoint.USWest2);
InvokeRequest ir = new InvokeRequest();
ir.InvocationType = InvocationType.RequestResponse;
ir.FunctionName = "ImageTesting";
ir.Payload = "\"" + fileName + "\"";
var result = await client.InvokeAsync(ir);
var strResponse = Encoding.ASCII.GetString(result.Payload.ToArray());
if (bool.TryParse(strResponse, out bool result2))
{
return result2;
}
return false;
}
Finally, here is the section of my UWP where I perform the function. I am referencing the lambda client via "using Lambdaclienttest" (name of lamda project, and this is its only instance I use the reference though). When I run my project, I do still get a face detected when it should, but the Globaltest.testingarray[0] is still equal to "test".
var Facedetector = new FaceDetector(Credentials.accesskey, Credentials.secretkey);
try
{
var result = await Facedetector.IsFace(filepath, filename);
if (result)
{
textBox1.Text = "There is a face detected";
textBox2.Text = Globaltest.testingarray[0];
}
else
{
textBox1.Text = "Try Again";
}
}
catch
{
textBox1.Text = "Please use a photo";
}
Does anyone have any suggestions?

Can't exit function properly

I have a C# unity function and I'm trying to request some json from a webserver but for some reason its not exiting the function where I want it to:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Proyecto26;
using UnityEditor;
using UnityEngine.Networking;
using System.Text;
using TMPro;
using SimpleJSON;
public class APIRequest
{
// string basicRequestPage = "****";
string basicAPI_Key = "****";
// Start is called before the first frame update
string outputs;
public string basicRequest(int requestTyp)
{
auth test1 = new auth();
test1.key = basicAPI_Key;
switch (requestTyp)
{
case 0:
test1.request = "deckList";
break;
case 1:
test1.request = "card";
break;
}
string output = JsonConvert.SerializeObject(test1);
byte[] outputToServer = Encoding.ASCII.GetBytes(output); ;
//Debug.Log(output);
RestClient.Request(new RequestHelper
{
Uri = basicRequestPage,
Method = "POST",
Timeout = 10,
//EnableDebug = true,
ParseResponseBody = false, //Don't encode and parse downloaded data as JSONe
BodyRaw = outputToServer
}).Then(response =>
{
// EditorUtility.DisplayDialog("Status", response.Text, "Ok");
var rest = response.Text;
//Debug.Log(rest);
outputs = rest.ToString();
//EditorUtility.DisplayDialog("Status", outputs, "Ok");
return outputs;
}).Catch(err =>
{
var error = err as RequestException;
EditorUtility.DisplayDialog("Error Response", error.Response + "", "Ok");
}
);
}
}
Link to code because it wouldn't work with the built in code text
It works perfectly fine as a method but keeps complaining that I don't have a valid return from the function. When I try to return the value I get from API, namely the outputs variable, it says it null. I have tried putting it in the function, in a public variable. but to now avail.
Any ideas?
When you're working with promises you need to return a promise from that method too (Promise chaining):
public IPromise<string> basicRequest(int requestTyp)
{
return RestClient.Request(new RequestHelper
{
Uri = basicRequestPage,
Method = "POST",
Timeout = 10,
//EnableDebug = true,
ParseResponseBody = false, //Don't encode and parse downloaded data as JSONe
BodyRaw = outputToServer
}).Then(response => response.Text.ToString());
}
For more details, check the repository of the Promises library used by the RestClient for Unity here.
You have defined an output of type string however you do not return a string at all exits. After the catch you have to return a string either at the end of the method or in the catch. The last line I have added return ""; should solve the problem.
public string basicRequest(int requestTyp)
{
auth test1 = new auth();
test1.key = basicAPI_Key;
switch (requestTyp)
{
case 0:
test1.request = "deckList";
break;
case 1:
test1.request = "card";
break;
}
string output = JsonConvert.SerializeObject(test1);
byte[] outputToServer = Encoding.ASCII.GetBytes(output);
//Debug.Log(output);
RestClient.Request(new RequestHelper
{
Uri = basicRequestPage,
Method = "POST",
Timeout = 10,
//EnableDebug = true,
ParseResponseBody = false, //Don't encode and parse downloaded data as JSONe
BodyRaw = outputToServer
}).Then(response =>
{
// EditorUtility.DisplayDialog("Status", response.Text, "Ok");
var rest = response.Text;
//Debug.Log(rest);
outputs = rest.ToString();
//EditorUtility.DisplayDialog("Status", outputs, "Ok");
return outputs;
}).Catch(err =>
{
var error = err as RequestException;
EditorUtility.DisplayDialog("Error Response", error.Response + "", "Ok");
}
);
return ""; //<----- this here
}

System.ArgumentNullException: 'value can't be null Parametre name: source'

I have a program which connects to a site and gets the list of orders using soap api. But i have a really strange issue. When a i try to get the orders of a day which there is no orders and then try get list of orders of a day i get this error. But strange thing is if a put a break point to line where i got the error and evalute the program step by step i don't get any errors. How could that happen. herre is the code.
https://api.n11.com/ws/OrderService.wsdl
using n11.Deneme.Forms.com.n11.api;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace n11.Deneme.Forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strStartDate = "18/01/2020";
string strEndDate = "18/01/2020";
long totalCountValue = 50;
int currentPageValue = 0;
int pageCountValue = 1;
int pageSizeValue = 50;
Authentication auth = new Authentication();
auth.appKey = "b891a6b9-cb97-4a7e-9ffb-f7b1e2a593e8";
auth.appSecret = "pHCjYYadxwTG64Ej";
OrderSearchPeriod orderSearchPeriod = new OrderSearchPeriod();
orderSearchPeriod.startDate = strStartDate;
orderSearchPeriod.endDate = strEndDate;
OrderDataListRequest orderDataListRequest = new OrderDataListRequest();
//orderDataListRequest.status = "1";
orderDataListRequest.period = orderSearchPeriod;
//orderDataListRequest.orderNumber = "209524598478";
PagingData pagingData = new PagingData();
pagingData.currentPage = currentPageValue;
pagingData.pageCount = pageCountValue;
pagingData.pageSize = pageSizeValue;
pagingData.totalCount = totalCountValue;
DetailedOrderListRequest request = new DetailedOrderListRequest();
request.auth = auth;
request.pagingData = pagingData;
request.searchData = orderDataListRequest;
OrderServicePortService port = new OrderServicePortService();
DetailedOrderListResponse response = port.DetailedOrderList(request);
List<DetailedOrderData> orderList = response.orderList.ToList();
foreach (var order in orderList)
{
MessageBox.Show(order.totalAmount.ToString() + " - " + order.orderNumber + " - " + order.citizenshipId + " - " + order.createDate);
long orderIdValue = order.id;
OrderDataRequest orderDataRequest = new OrderDataRequest();
orderDataRequest.id = orderIdValue;
OrderDetailRequest orderdetailrequest = new OrderDetailRequest();
orderdetailrequest.auth = auth;
orderdetailrequest.orderRequest = orderDataRequest;
OrderServicePortService port1 = new OrderServicePortService();
OrderDetailResponse orderDetailResponse = port1.OrderDetail(orderdetailrequest);
OrderDetailData orderDetail = orderDetailResponse.orderDetail;
MessageBox.Show(orderDetail.orderNumber);
List<OrderSearchData> orderItemList = orderDetail.itemList.ToList();
foreach (var item in orderItemList)
{
MessageBox.Show(item.shipmentInfo.campaignNumber);
}
}
}
}
}
If you're getting the error on the line:
List<DetailedOrderData> orderList = response.orderList.ToList(); //I GOT THE ERROR ON THIS LINE
then the thing to do is to look at how response.orderList gets a value. In particular, does it do something with threads, tasks, timers, external events, or anything else like that - which could mean that it gets populated shortly after the initial return from DetailedOrderList, which could explain why it works when you debug and step through (adding a crucial delay into things).
You could also simply do:
var tmp = response.orderList;
if (tmp == null) throw new InvalidOperationException(
"hey, response.orderList was null! this is not good!");
List<DetailedOrderData> orderList = tmp.ToList();
return orderList;
which would make it very clear and explicit that this is what is happening. If you don't get this exception, but something else, then: more debugging needed!
if(response.orderList == null)
{
var temp = button1_Click.PerformClick();
return temp;
}
else
{
List<DetailedOrderData> orderList = response.orderList.ToList();
return orderList;
}

CosmosDB Entity with the specified id does not exist

I am developing an ASP.NET Core MVC API to call resources in an Azure Cosmos DB. When I try to perform a GET for any specific ID, I receive DocumentClientException: Entity with the specified id does not exist in the system. I can confirm that the entity does exist in the system, and the connection is successful because I can successfully perform other methods and requests. The partition key is _id .
Debugging with breakpoints in Visual Studio, I can see where the correct ID is received at the API, but I can't confirm what specifically it is sending to Azure
The controller methods: (the ID field is a random string of numbers and text)
//controller is MoviesController decorated with [Route(api/[controller])]
//sample GET is to localhost:port/api/Movies/5ca6gdwndkna99
[HttpGet("{id}")]
public async Task<MoviesModel> Get(string id)
{
MoviesModel movie = await _persistence.GetMovieAsync(id);
return movie;
}
The data handling method:
public async Task<MoviesModel> GetMovieAsync(string Id)
{
string _id = Id;
RequestOptions options = new RequestOptions();
options.PartitionKey = new PartitionKey(_id);
var documentUri = UriFactory.CreateDocumentUri(_databaseId, "movies", Id);
Document result = await _client.ReadDocumentAsync(documentUri,options);
return (MoviesModel)(dynamic)result;
}
Other methods, like getting a list of all movies and returning to a table are working fine, so we can rule out network issues
public async Task<List<MoviesModel>> GetMoviesAsync()
{
var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, "movies");
// build the query
var feedOptions = new FeedOptions() { EnableCrossPartitionQuery = true };
var query = _client.CreateDocumentQuery<MoviesModel>(documentCollectionUri, "SELECT * FROM movies", feedOptions);
var queryAll = query.AsDocumentQuery();
// combine the results
var results = new List<MoviesModel>();
while (queryAll.HasMoreResults)
{
results.AddRange(await queryAll.ExecuteNextAsync<MoviesModel>());
}
return results;
}
public async Task<List<GenresModel>> GetGenresAsync()
{
await EnsureSetupAsync();
var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, "genres");
// build the query
var feedOptions = new FeedOptions() { EnableCrossPartitionQuery = true };
var query = _client.CreateDocumentQuery<GenresModel>(documentCollectionUri, "SELECT * FROM genres", feedOptions);
var queryAll = query.AsDocumentQuery();
// combine the results
var results = new List<GenresModel>();
while (queryAll.HasMoreResults)
{
results.AddRange(await queryAll.ExecuteNextAsync<GenresModel>());
}
return results;
}
Firstly, I would suggest to re-look at your cosmosDb design once, bcz of the following reasons...
Problems:
If your _id is random string of numbers and text, then its not good
to have the entire _id as your partition key, bcz this would create a
new partition for each entry.(although azure will range parition it
later)
Querying just by partition key is not efficient, for pin point
queries we should have both partition key and row key.
Solution:
Make the first one or two letters of your _id as your partition key. (so your partitions will be finite).
Make your _id as your row key.
If your _id = "abwed123asdf", then your query should be..
RequestOptions options = new RequestOptions();
options.PartitionKey = new PartitionKey(_id.Substring(0,1));
options.RowKey = _id;
This way, your look up will pin point to the exact required entry with the help of partition and row key. (saves lot of RUs)
Please refer docs for choosing a better partition keys for your needs https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview
I was able to get this to work by completely refactoring to the dotnet v3 SDK. My code for the solution is in the comments of the gitHub link:
using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VidlyAsp.DataHandlers;
namespace VidlyAsp.DataHandlers
{
public class PersistenceNew
{
private static string _endpointUri;
private static string _primaryKey;
private CosmosClient cosmosClient;
private CosmosDatabase database;
private CosmosContainer movieContainer;
private CosmosContainer genreContainer;
private string containerId;
private string _databaseId;
public PersistenceNew(Uri endpointUri, string primaryKey)
{
_databaseId = "Vidly";
_endpointUri = endpointUri.ToString();
_primaryKey = primaryKey;
this.GetStartedAsync();
}
public async Task GetStartedAsync()
{
// Create a new instance of the Cosmos Client
this.cosmosClient = new CosmosClient(_endpointUri, _primaryKey);
database = await cosmosClient.Databases.CreateDatabaseIfNotExistsAsync(_databaseId);
CosmosContainer moviesContainer = await GetOrCreateContainerAsync(database, "movies");
CosmosContainer genresContainer = await GetOrCreateContainerAsync(database, "genres");
movieContainer = moviesContainer;
genreContainer = genresContainer;
}
public async Task<GenresModel> GetGenre(string id)
{
var sqlQueryText = ("SELECT * FROM c WHERE c._id = {0}", id).ToString();
var partitionKeyValue = id;
CosmosSqlQueryDefinition queryDefinition = new CosmosSqlQueryDefinition(sqlQueryText);
CosmosResultSetIterator<GenresModel> queryResultSetIterator = this.genreContainer.Items.CreateItemQuery<GenresModel>(queryDefinition, partitionKeyValue);
List<GenresModel> genres = new List<GenresModel>();
while (queryResultSetIterator.HasMoreResults)
{
CosmosQueryResponse<GenresModel> currentResultSet = await queryResultSetIterator.FetchNextSetAsync();
foreach (GenresModel genre in currentResultSet)
{
genres.Add(genre);
}
}
return genres.FirstOrDefault();
}
public async Task<MoviesModel> GetMovie(string id)
{
var sqlQueryText = "SELECT * FROM c WHERE c._id = '" + id + "'";
var partitionKeyValue = id;
CosmosSqlQueryDefinition queryDefinition = new CosmosSqlQueryDefinition(sqlQueryText);
CosmosResultSetIterator<MoviesModel> queryResultSetIterator = this.movieContainer.Items.CreateItemQuery<MoviesModel>(queryDefinition, partitionKeyValue);
List<MoviesModel> movies = new List<MoviesModel>();
while (queryResultSetIterator.HasMoreResults)
{
CosmosQueryResponse<MoviesModel> currentResultSet = await queryResultSetIterator.FetchNextSetAsync();
foreach (MoviesModel movie in currentResultSet)
{
movies.Add(movie);
}
}
return movies.FirstOrDefault();
}
/*
Run a query (using Azure Cosmos DB SQL syntax) against the container
*/
public async Task<List<MoviesModel>> GetAllMovies()
{
List<MoviesModel> movies = new List<MoviesModel>();
// SQL
CosmosResultSetIterator<MoviesModel> setIterator = movieContainer.Items.GetItemIterator<MoviesModel>(maxItemCount: 1);
while (setIterator.HasMoreResults)
{
foreach (MoviesModel item in await setIterator.FetchNextSetAsync())
{
movies.Add(item);
}
}
return movies;
}
public async Task<List<GenresModel>> GetAllGenres()
{
List<GenresModel> genres = new List<GenresModel>();
// SQL
CosmosResultSetIterator<GenresModel> setIterator = genreContainer.Items.GetItemIterator<GenresModel>(maxItemCount: 1);
while (setIterator.HasMoreResults)
{
foreach (GenresModel item in await setIterator.FetchNextSetAsync())
{
genres.Add(item);
}
}
return genres;
}
private static async Task<CosmosContainer> GetOrCreateContainerAsync(CosmosDatabase database, string containerId)
{
CosmosContainerSettings containerDefinition = new CosmosContainerSettings(id: containerId, partitionKeyPath: "/_id");
return await database.Containers.CreateContainerIfNotExistsAsync(
containerSettings: containerDefinition,
throughput: 400);
}
}
}

Get All List Members - MailChimp 3.0

I'm trying to get all the members in my list (around 19,000 members) and am using the Mailchimp.NET.V3 package in C#.
The following code only retrieves the first 1000 members
IMailChimpManager MC = new MailChimpManager(#"xxxxxxxxxxxxxxxxxxxxxxxxx-xxx");
var listMembers = await MC.Members.GetAllAsync(ListId);
I also tried using the MemberRequest constructor but this never returns any value.
var listMembers = await MC.Members.GetAllAsync(ListId, new MemberRequest { Limit = 20000 } );
Can anyone help? Thanks!
You need to use the 'count' parameter, not limit.
Use the offset value.
List<Member> listMembers = new List<Member>();
IMailChimpManager manager = new MailChimpManager(MailChimpApiKey);
bool moreAvailable = true;
int offset = 0;
while (moreAvailable)
{
var listMembers = manager.Members.GetAllAsync(yourListId, new MemberRequest
{
Status = Status.Subscribed,
Limit = 250,
Offset = offset
}).ConfigureAwait(false);
var Allmembers = listMembers.GetAwaiter().GetResult();
foreach(Member member in Allmembers)
{
listMembers.Add(member);
}
if (Allmembers.Count() == 250)
//if the count is < of 250 then it means that there aren't more results
offset += 250;
else
moreAvailable = false;
}
I have done something similar using C# script on unity 3D, with basic authentication and WWW request. This script returns the members of a list on Mailchimp using API 3.0. Maybe this code helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mailchimpRESTful : MonoBehaviour
{
/*
* USE using CURL:
*
curl --request GET \
--url 'https://<dataCenter>.api.mailchimp.com/3.0/lists/<your-list-name>/members/' \
--user 'anystring:<mailchimp-api-key>'
EXAMPLE using CURL:
curl --request GET \
--url 'https://us12.api.mailchimp.com/3.0/lists/era987af43/members/' \
--user 'anystring:e419ac3fefefgkjne0559901222a3dbf-us12'
*/
// Mailchimp API RESTful C# script GET Member list
private const string DC = "<your-data-center>"; // us1,us2..us18
private const string LIST = "<your-list-name>"; // Something similar to: era987af43
private const string URL = "<your-url>"; // "https://<dataCenter>.api.mailchimp.com/3.0/lists/<your-list-name>/members/";
private const string USER = "anystring"; // You can use any user
private const string API_KEY = "your-api-key"; // Mailchimp API-key: e419ac3fefefgkjne0559901222a3dbf-us12
public void Start()
{
Request ();
}
public void Request()
{
Dictionary<string, string> headers = new Dictionary<string,string>();
headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes (USER + ":" + API_KEY));
WWW request = new WWW(URL, null, headers);
StartCoroutine (OnResponse (request));
}
private IEnumerator OnResponse(WWW req)
{
yield return req;
Debug.Log ("Query Result:"+req.text);
}
}
private async Task<List<Member>> GetMemberListAsync(string listId)
{
var offset = 0;
var moreAvailable = true;
var listMembers = new List<Member>();
while (moreAvailable)
{
var result = await manager.Members.GetAllAsync(listId, new MemberRequest
{
Status = Status.Subscribed,
Limit = 250,
Offset = offset
});
var resultList = result.ToList();
foreach (var member in resultList)
{
listMembers.Add(member);
}
if (resultList.Count() == 250)
offset += 250;
else
moreAvailable = false;
}
return listMembers;
}
var listMembers = manager.Members.GetAllAsync(ListId).ConfigureAwait(false);
var Allmembers = listMembers.GetAwaiter().GetResult();

Categories