ListTablesSegmentedAsync disregards the continuation token - .net-core

I am trying to use ListTablesSegmentedAsync and it doesn't seem to be using the continuable token. I am using Microsoft.Azure.Cosmos.Table v1.0.8 and trying to list tables on the storage emulator. Here's my simplified test code:
public async Task ListTablesTest(
CloudTableClient client)
{
TableContinuationToken continuationToken = null;
TableResultSegment ret;
ret = await client.ListTablesSegmentedAsync(
string.Empty,
5,
continuationToken,
CancellationToken.None);
continuationToken = ret.ContinuationToken;
ret = await client.ListTablesSegmentedAsync(
string.Empty,
5,
continuationToken,
CancellationToken.None);
}
Both calls to ListTablesSegmentedAsync return the first 5 tables (alphabetically). The token returned from the first call looks correct, as far as I can tell. Am I missing something? Maybe this doesn't work in the emulator?

Please make sure you're using the latest version of azure storage explorer 1.16.0 and azure storage emulator 5.10.
In my test, I'm also using Microsoft.Azure.Cosmos.Table v1.0.8, it can list next 5 tables by using ContinuationToken:

Related

Can consuming a CosmosDB FeedIterator result in multiple continuation tokens?

So I'm performing a query on a CosmosDB container on behalf of a client and want to support returning continuation tokens to the caller. Right now my query against CosmosDB based off the examples in the official documentation looks like:
FeedIterator<T> feedIterator = container.GetItemQueryIterator<T>(queryDefinition, continuationToken, queryOptions);
// class MyResponse {
// List<T> Objects;
// string? ContinuationToken
// }
var myResponse = new MyResponse();
while (feedIterator.HasMoreResults)
{
FeedResponse<T> response = await feedIterator.ReadNextAsync();
myResponse.ContinuationToken = response.ContinuationToken;
foreach (var result in response)
{
myResponse.Objects.add(result);
}
}
So the documentation opted to use a while loop to consume the FeedIterator, implying it is possible this block is run multiple times and as a result we can consume more than one FeedResponse. Each FeedResponse has a ContinuationToken property, so it seems possible to have multiple ContinuationTokens. If so, is it guaranteed the latest response will have the most up to date continuation token?
Yes; each newer continuation token is set on your response object, overwriting the previous continuation token.
I'm not sure why you have a while loop, though. It seems like you would just want to call ReadNextAsync once per request.

How to reset firestore emulator in integration test

I'm running my integration-tests in flutter and dart using the firestore emulator. First I start the firestore emulator with some data like so: firebase emulators:start --import=./dir.
Then I start an android emulator and start the app I want to test on the android emulator. The app is configured to use the firestore emulator. Then I run a series of tests, which all write to the firestore emulator.
But on the beginning of each test, I want the data to be reset to the state, when I first started the emulator. So e.g. if the tests are executed in this order:
Test A
Test B
Test C
I don't want to have the data, Test A created to be present in the database, when Tests B and C are executed. I could terminate the firestore emulator and start it again at the beginning of each test. But this would make my tests a lot slower.
Do you know of a way to reset the data, which is present in the firebase emulator?
I am assuming you're referring to firestore when you say you want to 'reset the data'.
Per the documentation at https://firebase.google.com/docs/emulator-suite/install_and_configure#use_the_emulator_hub_rest_api
import fetch from 'node-fetch';
import firebaseConfig from '../../../firebase.json';
const hubEmulatorPort = firebaseConfig.emulators.hub.port;
const firestoreEmulatorPort = firebaseConfig.emulators.firestore.port;
async function clearDb() {
const response = await fetch(
`http://localhost:${firestoreEmulatorPort}/emulator/v1/projects/${process.env.PROJECT_ID}/databases/(default)/documents`,
{
method: 'DELETE',
}
);
if (response.status !== 200) {
throw new Error('Trouble clearing Emulator: ' + (await response.text()));
}
}
async function populateDb(data) {
// Logic for adding in any data you want in the db
// before each test run
}
async function enableBackgroundTriggers() {
const response = await fetch(`http://localhost:${hubEmulatorPort}/functions/enableBackgroundTriggers`, {
method: 'PUT',
});
if (response.status !== 200) {
throw new Error('Trouble enabling database triggers in emulator: ' + (await response.text()));
}
}
async function disableBackgroundTriggers() {
const response = await fetch(`http://localhost:${hubEmulatorPort}/functions/disableBackgroundTriggers`, {
method: 'PUT',
});
if (response.status !== 200) {
throw new Error('Trouble disabling database triggers in emulator: ' + (await response.text()));
}
}
async function resetDb(data) {
await disableBackgroundTriggers();
await clearDb();
await populateDb(data);
await enableBackgroundTriggers();
}
export { resetDb };
I can't find a source for the clearing of the db, but the RESTful call in clearDb does what you want.
It's important to disable the triggers before clearing or populating the database, in case you have firestore triggers that modify data in ways your tests don't expect. I write tests by passing full DB state to the populateDb method, then reenable triggers before running tests so I can test said triggers. If you aren't running any firestore triggers, the clearDb call alone should be enough for your purposes.
My tests all have calls to resetDb() in my beforeEach hook in jest to ensure clean runs of each test. I recommend adding this to whatever 'beforeEach'-like hook your favorite testing API exposes.
If your tests do things like create users in Firebase Authentication you'll have to find another way to clear them between test runs.
If anyone can find documentation on how to clear other emulators in the Firebase Emulator Suite, please drop it in the comments. I am currently trying to find a way to clear Authentication emulators, which is actually how I found this question.
Best of luck!
If you want to clear out all the collections programatically, like in a setUp() or tearDown() there's a reference for that here: Delete data from Cloud Firestore - Delete Collections
Note that it's not recommended for all implementations, but there are examples in Java, Python, Node.js, go, PHP, C#, and Ruby.
Here's an example of how to iterate through all your collections and delete them all in Java, using the deleteCollection() method from that link.
public static void main(String[] args) throws IOException {
final int BATCH_SIZE = 5;
Firestore firestore = initializeCloudFirestore();
for (CollectionReference listCollection : firestore.listCollections()) {
deleteCollection(listCollection, BATCH_SIZE);
}
}
/**
* One way of initializing Firestore,
* see other options at https://firebase.google.com/docs/firestore/quickstart#initialize
*/
private static Firestore initializeCloudFirestore() throws IOException {
// Use the application default credentials
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.setProjectId("projectId")
.build();
FirebaseApp.initializeApp(options);
Firestore firestore = FirestoreClient.getFirestore();
return firestore;
}
/**
* Delete a collection in batches to avoid out-of-memory errors. Batch size may be tuned based on
* document size (atmost 1MB) and application requirements.
* See https://firebase.google.com/docs/firestore/manage-data/delete-data#java_5
*/
static void deleteCollection(CollectionReference collection, int batchSize) {
try {
// retrieve a small batch of documents to avoid out-of-memory errors
ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
int deleted = 0;
// future.get() blocks on document retrieval
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
document.getReference().delete();
++deleted;
}
if (deleted >= batchSize) {
// retrieve and delete another batch
deleteCollection(collection, batchSize);
}
} catch (Exception e) {
System.err.println("Error deleting collection : " + e.getMessage());
}
}
For the entire file, including imports, see this Github Gist.

Office 365 Rest Api Having issues getting access token

So far i have this.
public static async Task<OutlookServicesClient> CreateOutlookClientAsync(string capability)
{
try
{
string authority = CommonAuthority;
// Create an AuthenticationContext using this authority.
_authenticationContext = new AuthenticationContext(authority);
//See the Discovery Service Sample (https://github.com/OfficeDev/Office365-Discovery-Service-Sample)
//for an approach that improves performance by storing the discovery service information in a cache.
DiscoveryClient discoveryClient = new DiscoveryClient(
async () => await GetTokenHelperAsync(_authenticationContext, DiscoveryResourceId));
// Get the specified capability ("Contacts").
CapabilityDiscoveryResult result =
await discoveryClient.DiscoverCapabilityAsync(capability);
var client = new OutlookServicesClient(
result.ServiceEndpointUri,
async () =>
await GetTokenHelperAsync(_authenticationContext, result.ServiceResourceId));
return client;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (_authenticationContext != null && _authenticationContext.TokenCache != null)
_authenticationContext.TokenCache.Clear();
return null;
}
}
}
private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
{
string accessToken = null;
AuthenticationResult result = null;
string myId = WebConfigurationManager.AppSettings["ida:ClientID"];
string myKey = WebConfigurationManager.AppSettings["ida:Password"];
ClientCredential client = new ClientCredential(myId,myKey);
result = await context.AcquireTokenAsync(resourceId, client);
//result =context.AcquireToken(resourceId, ClientID,_returnUri);
accessToken = result.AccessToken;
return accessToken;
}
When i get to result one of two things happen if i user AcquireTokenAsync i get an error stating Application with identifier XXXX was not found in directory api.office.com otherwise if i run AcquireToken i get the login modal to pop but an error occurs indicating the request must contain client_secret .
I have no idea how to resolve this issue i suspect it may have something to do with the actual app configuration i have tried both creating my own app in Azure AD and using VS Connected Service, Has Anyone Else ran into a similar issues?
Based on the errors you're seeing, there seems to be an issue with how your app is registered. The first error usually happens when the app is not marked as multi-tenant, and you login to the app with a tenant other than the one where the app is registered.
The second error is odd. Client secret is what you're reading out of the ida:Password element and passing in the ClientCredential object.
I just put a .NET tutorial up yesterday that walks through setting this stuff up. Take a look and see if that helps get you unblocked.

How to properly sync Xamarin iOS Sqlite using Azure Mobile Services

In my Xamarin iOS PCL app I'm trying to insert a record into my local Sqlite table, have it synced via Azure Mobile Services, and then read it back.
Here is the code:
private IMobileServiceSyncTable<Job> jobTable;
public async Task InitializeAsync()
{
var store = new MobileServiceSQLiteStore("localdata.db");
store.DefineTable<Job> ();
await this.MobileService.SyncContext.InitializeAsync(store);
jobTable = this.MobileService.GetSyncTable<Job>();
jobTable = this.MobileService.GetSyncTable<Job>();
JObject newJob = new JObject ();
newJob.Add ("Id","job_123");
jobTable.InsertAsync (newJob);
this.MobileService.SyncContext.PushAsync();
var readResult = jobTable.ReadAsync ().Result.AsQueryable();
var resultList = from data in readResult
select data;
var resultCount = resultList.Count ();
}
So far - nothing gets synced up with my Sql Server db (which is on the recieving end of the Mobile Services), and the resultCount remain at 0
I'm sure I do something wrong here, just can't seem to nail what exactly.
-Eugene
You should use PullAsync instead of ReadAsync. Also, you need to await the call to all of your async method calls, such as InsertAsync, PushAsync, and PullAsync.
See this tutorial for a detailed example: http://azure.microsoft.com/en-us/documentation/articles/mobile-services-xamarin-ios-get-started-offline-data/

Issue with httpclient.getstringasync

I want to develop a Dragon Timer Windows Store App for GuildWars 2.
Whatever, I save a timestamp in a sql database. To get this timestamp in the app, I made a php script that writes the content of the database to a page. Now I'm trying to receive that string via the HttpClient.GetStringAsync() Method. Here's the code snipped:
async Task<Dictionary<String, DateTime>> GetKillTimes()
{
Dictionary<String, DateTime> killTimes = new Dictionary<String,DateTime>();
HttpClient httpClient = new HttpClient();
Task<string> getStringTask = httpClient.GetStringAsync("http://www.wp10454523.server-he.de/truhentimer/getTimes.php");
String rawKillTimes = await getStringTask;
//Parse to Dictionary...
return killTimes;
}
I tried some different Methods I got from google (WebRequest ...), but every one got stuck at the Get-Part. Am I maybe misinterpreting the function? Shouldn't I get the content of the page, which is a simple String?
You have to use await keyword as web request & response in WinRT are asynchronous so you have to use await keyword. await before httpClient.GetStringAsync(...)
Task<string> getStringTask = await httpClient.GetStringAsync("http://www.wp10454523.server-he.de/truhentimer/getTimes.php");

Resources