Load text from web during xml parsing in Windows 8 Async - c#

I have a unique issue, I want to get the name of an application from it's AppID while I convert an XML file into objects. This is the code I'm using at present:
if (xdoc.Element("Application") != null)
{
var data = from query in xdoc.Descendants("AppID")
select new APP
{
AppID = query.Value,
AppName = GetName(query.Value).ToString(),
};
itemGridView.DataContext = data;
}
This is the code I'm using to convert the GUID into an app name using Microsoft's Store API. I can confirm that it does return the app name. I'm just unsure how I can get this to display.
private async Task<string> GetName(string guid)
{
try
{
string link = "https://services.apps.microsoft.com/browse/6.2.9200-1/615/en-NZ_en-NZ/c/NZ/cp/10005001/Apps/{0}";
string url = string.Format(link, guid);
var httpClient = new HttpClient();
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url);
var response = await httpClient.SendAsync(httpRequestMessage);
var xmlString = await response.Content.ReadAsStringAsync();
XmlDocument NameXML = new XmlDocument();
NameXML = await XmlDocument.LoadFromUriAsync(new Uri(url));
string sAppName = NameXML.GetElementsByTagName("T")[0].ChildNodes[0].NodeValue.ToString();
return sAppName;
}
catch(Exception)
{
return guid;
}
}
I think my problem is with the async / await tasks. I've just been exposed to it now... how would I load up the App Name alongside the AppID when I parse the xml file?
The output that's being displayed when I run the app is "System.Threading.Tasks.Task[System.String]" (The objects load and the links and everything works fine, its just that the above is displayed instead of the app name).
I've been debugging using breakpoints, it appears that the GetName method only seems to be triggered later on, I'm not sure however.

Try to change this line :
AppName = GetName(query.Value).ToString(),
To this :
AppName = await GetName(query.Value),
GetName will return Task<string> instead of string when not awaited. And the method where above code resides required to be async because of using await inside that method :
private async void SomeMethod()
{
....
if (xdoc.Element("Application") != null)
{
var data = from query in xdoc.Descendants("AppID")
select new APP
{
AppID = query.Value,
AppName = await GetName(query.Value),
};
itemGridView.DataContext = data;
}
....
}
UPDATE :
As you already noticed, LINQ has very limited support for async/await currently. So to workaround this limitation, we can use normal for loop to avoid calling async function inside LINQ :
private async void SomeMethod()
{
....
if (xdoc.Element("Application") != null)
{
var query = from query in xdoc.Descendants("AppID")
select query.Value;
var data = new List<App>();
foreach (var q in query)
{
data.Add(new App{ AppId = q, AppName = await GetName(q) });
}
itemGridView.DataContext = data;
}
....
}

Related

Lsing API to list results in different method

The aim of the program below is to get a list of Reports built in our database and find out how many of these reports use the field NameFirst within them.
I'm able to make an API call and, at GetReports, get a list of the ReportIDs.
However, I'm unable to move forward with calling the list I created at GetReports in the next method, GetNameFirst. I was wondering if someone could please help me out with this.
For the script below, I get a red underline for the variable values. This is understandable because I didn't know where and how to tell my code to bind the list output for GetReports to the variable values in GetNameFirst.
Also, if I could get some help in finding out which reports have the field NameFirst in them once I accomplish calling the list from the first method to the second, I'd appreciate that also. I'm currently heading in the direction of using a foreach, but I'm unsure if that's the best path to take.
Main Program
namespace NameFirstSearch
{
class Program
{
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
const string username = "Username";
const string password = "Password";
const string baseUrl = "https://example.com/rest/services/";
const string queryString = "query?q=Select * From Report Where LastRanDate is not null";
const string queryNameFirst = "getreport/";
var client = new HttpClient();
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var auth = Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
GetReports(client, queryString).Wait();
GetNameFirst(client, queryNameFirst).Wait();
Console.ReadLine();
}
static async Task<List<Properties>> GetReports(HttpClient client, string queryString)
{
List<Properties> result = new List<Properties>();
var response = await client.GetAsync(queryString);
// Check for a successfull result
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<List<Properties>>(json);
Console.WriteLine(result.Count());
}
else
{
// Error code returned
Console.WriteLine("No records found on first method.");
}
return result;
}
static async Task GetNameFirst(HttpClient client, string queryNameFirst)
{
string reportType = "JSON";
foreach (var item in values)
{
var output = await client.GetAsync(queryNameFirst + item.ReportID + reportType);
if (output.IsSuccessStatusCode)
{
var allText = await output.Content.ReadAsStringAsync();
var fields = JsonConvert.DeserializeObject<List<NameFirst>>(allText);
}
else
{
// Error code returned
Console.WriteLine("No records found on second method.");
}
}
}
}
Class for report list
class Properties
{
public int ReportID { get; set; }
}
Class for reports' NameFirst property
class NameFirst
{
public string FirstName { get; set; }
}
I thought this was a partial code, but since you've cleared things out.
you'll need to change your code a bit
this :
GetReports(client, queryString).Wait();
do it like this :
var reportsList = GetReports(client, queryString).Result;
now, you'll need to pass the reportsList to the second method GetNameFirst which would be adjusted to this :
static async Task GetNameFirst(HttpClient client, string queryNameFirst, List<Properties> results)
{
string reportType = "JSON";
foreach (var item in results)
{
var output = await client.GetAsync(queryNameFirst + item.ReportID + reportType);
if (output.IsSuccessStatusCode)
{
var allText = await output.Content.ReadAsStringAsync();
var fields = JsonConvert.DeserializeObject<List<NameFirst>>(allText);
}
else
{
// Error code returned
Console.WriteLine("No records found on second method.");
}
}
}
with this adjustment, you'll need to adjust the call as well :
GetNameFirst(client, queryNameFirst, reportsList).Wait();

How to call async method from sync in C#?

I am adding a new web API call to existing functionality. I want to make this API call async but looks like it is causing deadlock. I have to make a lot more changes if I want to make entire code channel async which is not possible.
Questions I have are:
Is it possible to call async method from regular method?
What am I missing here? OR What is the correct approach here?
Code:
// Exisitng Method
public Tuple<RestaurantDeliveryProvider, DeliveryHubResult, Task<DeliveryManagerQuoteResponse>> CreateDeliveryRequest(OrderContextDTO orderContextDto)
{
var provider = RestaurantBl.GetDeliveryProviderInformationByRestaurantId(orderContextDto.RestaurantId ?? 0);
var deliveryHubResult = RestaurantBl.GetDeliveryHubResult(orderContextDto.OrderId ?? 0);;
// New Call which always comes back with "Not Yet Computed" result
Task<DeliveryManagerQuoteResponse> deliveryManagerQuoteResponse = _deliveryManager.CreateQuoteRequestAsync(orderContextDto, orderInfo);
return Tuple.Create(provider, deliveryHubResult, deliveryManagerQuoteResponse);
}
Async Methods:
public async Task<DeliveryManagerQuoteResponse> CreateQuoteRequestAsync(OrderContextDTO orderContextDto, OrderInfoDTO orderInfo)
{
DeliveryManagerQuoteResponse deliveryManagerQuoteResponse = null;
try
{
var restaurantInfo = RestaurantApi.GetRestaurant(orderInfo.RestaurantId);
var quoteRequest = new DeliveryManagerQuoteRequest
{
DeliveryProvider = null,
Country = orderContextDto.DeliveryEstimateRequestDto.RequestedDeliveryAddress.Country,
Concept = "BK",
StoreName = "BK-TEST-US-4",
OrderId = orderInfo.OrderId.ToString(),
AllowCash = false,
PaymentType = OrderPaymentType.Prepaid_Credit,
Note = orderInfo.DeliveryInstructions,
};
deliveryManagerQuoteResponse = await Quote(quoteRequest);
}
catch (Exception ex)
{
Log.ErrorFormat("Get Delivery Manager Quote failed: Error: {0}, OrderId: {1}", ex.Message, orderContextDto.OrderId);
}
return deliveryManagerQuoteResponse;
}
public async Task<DeliveryManagerQuoteResponse> Quote(DeliveryManagerQuoteRequest quoteRequest)
{
DeliveryManagerQuoteResponse deliveryManagerQuoteResponse;
var client = HttpClientFactory.GetClient();
var content = HttpClientFactory.JsonContentFactory.CreateJsonContent(quoteRequest);
var response = await client.PostAsync("https://myUrl", content);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
deliveryManagerQuoteResponse = JsonConvert.DeserializeObject<DeliveryManagerQuoteResponse>(data);
}
else
{
throw new Exception((int)response.StatusCode + "-" + response.StatusCode);
}
return deliveryManagerQuoteResponse;
}
I tried following as well but same result:
public async Task<DeliveryManagerQuoteResponse> Quote(DeliveryManagerQuoteRequest quoteRequest)
{
DeliveryManagerQuoteResponse deliveryManagerQuoteResponse;
using (var client = new HttpClient())
{
var content = HttpClientFactory.JsonContentFactory.CreateJsonContent(quoteRequest);
var response = await client.PostAsync("https://myUrl", content);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
deliveryManagerQuoteResponse = JsonConvert.DeserializeObject<DeliveryManagerQuoteResponse>(data);
}
else
{
throw new Exception((int)response.StatusCode + "-" + response.StatusCode);
}
}
return deliveryManagerQuoteResponse;
}
Output (sorry for the blurry output, if you click on it, you will see clear result):
don't
don't
Basically, there is no good or workable way to call an async method from a sync method and wait for the answer. There's "sync over async", but that's an anti-pattern and should be aggressively avoided.
So either:
rewrite the caller to be async
implement a synchronous version of the API

How to test WP app with web requests?

I am developing windows phone application, where data comes from web. I would like to test some method:
public void PrintSomeObject()
{
var apiInstance = new Api();
apiInstance.GetSomeObject("bar",
(res) =>
{
Debug.WriteLine(res.data);
});
}
public class Api
{
public GetSomeObject(string path, Action<SomeObject> callback,)
{
HTTPRequest("http://foo.com/" + path,
(resultStr) =>
{
SomeObject t = ParseSomeObject(resultStr);
callback(t);
});
}
void async void HTTPRequest(string baseUri, Action<string> resultCallback)
{
var result = await httpClient.PostAsync(new Uri(baseUri, UriKind.Absolute), content);
var resultStr = await result.Content.ReadAsStringAsync();
// var resultStr = "{data: 'some fake data', number: 42}" // I want insert fake data here
resultCallback(resultStr);
}
}
I know want PrintSomeObject() should print in some cases, and I'd like to test it. But I need to control data which I get in request response. Is it possible, or I need test each method separatly?

How to call async method inside a method which has return type?

This is windows phone 8.1 silverlight app.
I have a file association. For that I have a class as
class AssociationUriMapper : UriMapperBase
{
public override Uri MapUri(Uri uri)
{
//here I'm getting file ID etc..
}
// here I want to read the file content & determine the file type because,
// the case is, even same file extension can contain different type of data
switch (fileType)
{
//here I'm calling appropriate page according to type
}
}
Now the problem is MapUri is overridden method so it must have a return type. while, OpenStreamForReadAsync() is a async method. I tried Wait() method, creating new task & then calling Start(), Wait() in it but no success. Currently my code is,
class AssociationUriMapper : UriMapperBase
{
string strData = "";
public override Uri MapUri(Uri uri)
{
strUri = uri.ToString();
// File association launch
if (strUri.Contains("/FileTypeAssociation"))
{
// Get the file ID (after "fileToken=").
int nFileIDIndex = strUri.IndexOf("fileToken=") + 10;
string strFileID = strUri.Substring(nFileIDIndex);
string strFileName = SharedStorageAccessManager.GetSharedFileName(strFileID);
string strIncomingFileType = Path.GetExtension(strFileName);
fnCopyToLocalFolderAndReadContents(strFileID);
switch (fileType)
{
case ".gmm":
//determine if gmm is text
if (objGMM.fnGetGMMType() == GMMFILETYPE.TXT)
{
return new Uri("/PageReadText.xaml?data=" + strData, UriKind.Relative);
}
break;
}
}
}
async void fnCopyToLocalFolderAndReadContents(string strIncomingFileId)
{
StorageFolder objLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
objFile = await SharedStorageAccessManager.CopySharedFileAsync(objLocalFolder, TEMP.gmm, NameCollisionOption.ReplaceExisting, strIncomingFileId);
using (StreamReader streamReader = new StreamReader(objFile))
{
strData = streamReader.ReadToEnd();
}
}
}
The first thing I'd do is change the logic. When the OS asks your app whether it supports a Uri mapping, it's expecting an immediate answer; it's not expecting the app to copy and read files. Usually, Uri mappings are very constant; an app either always supports one or it does not.
So, the first thing I would try to do is load all the mapping files at startup and then create the AssociationUriMapper with all the results. If this isn't possible, then you're almost certainly using Uri mappings for the wrong thing. They're not supposed to be dynamic, and it is quite possible that the OS will assume that they're not dynamic.
That said, if you want to get it working, I think the cleanest solution would be to push the asynchronous file operations to another thread and then block on that:
public override Uri MapUri(Uri uri)
{
strUri = uri.ToString();
// File association launch
if (strUri.Contains("/FileTypeAssociation"))
{
// Get the file ID (after "fileToken=").
int nFileIDIndex = strUri.IndexOf("fileToken=") + 10;
string strFileID = strUri.Substring(nFileIDIndex);
string strFileName = SharedStorageAccessManager.GetSharedFileName(strFileID);
string strIncomingFileType = Path.GetExtension(strFileName);
var strData = Task.Run(() => CopyToLocalFolderAndReadContents(strFileID)).GetAwaiter().GetResult();
switch (fileType)
{
case ".gmm":
//determine if gmm is text
if (objGMM.fnGetGMMType() == GMMFILETYPE.TXT)
{
return new Uri("/PageReadText.xaml?data=" + strData, UriKind.Relative);
}
break;
}
}
}
async Task<string> CopyToLocalFolderAndReadContentsAsync(string strIncomingFileId)
{
StorageFolder objLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
objFile = await SharedStorageAccessManager.CopySharedFileAsync(objLocalFolder, TEMP.gmm, NameCollisionOption.ReplaceExisting, strIncomingFileId);
using (StreamReader streamReader = new StreamReader(objFile))
{
return streamReader.ReadToEnd();
}
}
I don't like it much, because it involves code that calls an async method synchronously. But the following should work:
class AssociationUriMapper : UriMapperBase
{
public override Uri MapUri(Uri uri)
{
strUri = uri.ToString();
// File association launch
if (strUri.Contains("/FileTypeAssociation"))
{
// Get the file ID (after "fileToken=").
int nFileIDIndex = strUri.IndexOf("fileToken=") + 10;
string strFileID = strUri.Substring(nFileIDIndex);
string strFileName = SharedStorageAccessManager.GetSharedFileName(strFileID);
string strIncomingFileType = Path.GetExtension(strFileName);
string strData = fnCopyToLocalFolderAndReadContents(strFileID).Result;
switch (fileType)
{
case ".gmm":
//determine if gmm is text
if (objGMM.fnGetGMMType() == GMMFILETYPE.TXT)
{
return new Uri("/PageReadText.xaml?data=" + strData, UriKind.Relative);
}
break;
}
}
}
async Task<string> fnCopyToLocalFolderAndReadContents(string strIncomingFileId)
{
StorageFolder objLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
objFile = await SharedStorageAccessManager.CopySharedFileAsync(objLocalFolder, TEMP.gmm, NameCollisionOption.ReplaceExisting, strIncomingFileId).ConfigureAwait(false);
using (StreamReader streamReader = new StreamReader(objFile))
{
return streamReader.ReadToEnd();
}
}
}
For me, a bigger question is why would you implement a method like MapUri() such that it requires calls to asynchronous methods, and involves this kind of potentially time-consuming I/O. I mean, maybe that is in fact required here, but it just seems a bit off. Unfortunately, there's not enough context in the question for me to feel like I can offer other alternatives.
Unfortunately, there is no "pretty way" of overriding non-async methods.
The best you can do is make sure you add ConfigureAwait(false) to your async calls to make sure the SynchronizationContext doesn't flow and deadlock, and then access the Result property of the returned Task.
What i would do is change the method that reads the file to return a Task<string>:
async Task<string> CopyToLocalFolderAndReadContents(string strIncomingFileId)
{
StorageFolder objLocalFolder = Windows.Storage.ApplicationData.Current
.LocalFolder;
objFile = await SharedStorageAccessManager
.CopySharedFileAsync(objLocalFolder, TEMP.gmm,
NameCollisionOption.ReplaceExisting,
strIncomingFileId)
.AsTask().ConfigureAwait(false);
using (StreamReader streamReader = new StreamReader
(await objFile.OpenStreamForReadAsync().ConfigureAwait(false)))
{
return await streamReader.ReadToEndAsync().ConfigureAwait(false);
}
}
And then change the call site to:
string data = CopyToLocalFolderAndReadContents(strFileID).Result;

C# Google drive sdk. How to get a list of google drive folders?

I'm writing a program to allow a user to upload files to their Google Drive account. I have the upload part working and am using OAuth2. The issue I'm currently having is getting a list of folders from the users Drive account.
I found some code that is supposed to do this using the .setUserCredentials method, but it doesn't work:
DocumentsService service1 = new DocumentsService("project");
service1.setUserCredentials("user","pass");
FolderQuery query1 = new FolderQuery();
// Make a request to the API and get all documents.
DocumentsFeed feed = service1.Query(query1);
// Iterate through all of the documents returned
foreach (DocumentEntry entry in feed.Entries)
{
var blech = entry.Title.Text;
}
Nothing is returned. Ideally, I want to use OAuth2 to do this. I've been trying with the following code, trying to set the authentication token, but I always get denied access:
String CLIENT_ID = "clientid";
String CLIENT_SECRET = "secretid";
var docprovider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var docstate = GetDocAuthentication(docprovider);
DocumentsService service1 = new DocumentsService("project");
service1.SetAuthenticationToken(docstate.RefreshToken);
FolderQuery query1 = new FolderQuery();
DocumentsFeed feed = service1.Query(query1); //get error here
// Iterate through all of the documents returned
foreach (DocumentEntry entry in feed.Entries)
{
// Print the title of this document to the screen
var blech = entry.Title.Text;
}
..
private static IAuthorizationState GetDocAuthentication(NativeApplicationClient client)
{
const string STORAGE = "storagestring";
const string KEY = "keystring";
string scope = "https://docs.google.com/feeds/default/private/full/-/folder";
// Check if there is a cached refresh token available.
IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
if (state != null)
{
try
{
client.RefreshToken(state);
return state; // Yes - we are done.
}
catch (DotNetOpenAuth.Messaging.ProtocolException ex)
{
}
}
// Retrieve the authorization from the user.
state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
return state;
}
Specifically, I get "Execution of request failed: https://docs.google.com/feeds/default/private/full/-/folder - The remote server returned an error: (401) Unauthorized".
I've also tried:
var docauth = new OAuth2Authenticator<NativeApplicationClient>(docprovider, GetDocAuthentication);
DocumentsService service1 = new DocumentsService("project");
service1.SetAuthenticationToken(docauth.State.AccessToken);
but "State" is always null, so I get a null object error. What am I doing wrong and how is this done?
You should use the Drive SDK, not the Documents List API, which allows you to list folders. You can use "root" as a folderId if you want to list the root directory.
I actually implemented the v3 version of the GDrive SDK for .NET and needed to search for folders as well.
I prefer requesting uniquely all folders instead of getting all files and then performing a LinQ query to keep just the folders.
This is my implementation:
private async Task<bool> FolderExistsAsync(string folderName)
{
var response = await GetAllFoldersAsync();
return response.Files
.Where(x => x.Name.ToLower() == folderName.ToLower())
.Any();
}
private async Task<Google.Apis.Drive.v3.Data.FileList> GetAllFoldersAsync()
{
var request = _service.Files.List();
request.Q = "mimeType = 'application/vnd.google-apps.folder'";
var response = await request.ExecuteAsync();
return response;
}
You could request the name on the Q this way as well:
request.Q = $"mimeType = 'application/vnd.google-apps.folder' and name = '{folderName}'";
Which would lead and simplify things to (obviating null checking):
private async Task<bool> FolderExistsAsync(string folderName)
{
var response = await GetDesiredFolder(folderName);
return response.Files.Any();
}
private async Task<FileList> GetDesiredFolder(string folderName)
{
var request = _service.Files.List();
request.Q = $"mimeType = 'application/vnd.google-apps.folder' and name = '{folderName}'";
var response = await request.ExecuteAsync();
return response;
}
private IEnumerable<DocumentEntry> GetFolders(string id) {
if (IsLogged) {
var query = new FolderQuery(id)
{
ShowFolders = true
};
var feed = GoogleDocumentsService.Query(query);
return feed.Entries.Cast<DocumentEntry>().Where(x => x.IsFolder).OrderBy(x => x.Title.Text);
}
return null;
}
...
var rootFolders = GetFolders("root");
if (rootFolders != null){
foreach(var folder in rootFolders){
var subFolders = GetFolders(folder.ResourceId);
...
}
}
where GoogleDocumentsService is a instance of DocumentsService and IsLogged is a success logged flag.
I got this way to get list of folders from google drive
FilesResource.ListRequest filelist= service.Files.List();
filelist.Execute().Items.ToList().Where(x => x.MimeType == "application/vnd.google-apps.folder").ToList()

Categories

Resources