Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I thought I could do i straight forward, but maybe there is something wrong with my setup? I'm trying to download a string in my app for logging in:
private async void DoLogin()
{
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(Config.SERVER_URL + "/Login/");
All logic is removed, im going to add headers and so on, but VS2012 will no allow me to await that response.
I tried to follow the code from here, but in my case I only get Cannot await 'System.Threading.Tasks.Task<string'.
Why is that? Should'nt GetStringAsync simply return me a string? It returns a Task<string>, but do I have to wrap it in a method?
Your code is perfectly fine. However: In order to use async / await in portable class libaries you need to add the NuGet package Microsoft.Bcl.Async to your project.
Also please note the comment of Servy to use the return type Task instead of void.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Xamarin.Android C#: I have several void where I need to have the AddOnSuccessListener, the problem is that they all go to the same OnSuccess.
How can I have 3 or more AddOnSuccessListener with different OnSuccess for each void?
Example of voids:
query.Get (). AddOnSuccessListener (this);
database.Collection ("Users"). Document (DocID) .Get ().
AddOnSuccessListener (this);
Ideas?
A Get() call returns a task. While a task can have multiple listeners, you can't chain them together like you're trying.
So you'll want to capture the task, and then call AddOnSuccessListener multiple times.
var task = database.Collection("Users").Document(DocID).Get();
task.AddOnSuccessListener(this);
task.AddOnSuccessListener(...);
task.AddOnSuccessListener(...);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to post datetime into the below api. But it is not going through.
lastRefreshDateTime.Result is datetime
Below is the code I tried:
await client.PostAsync($"{_printApiUrl}pdf/GenerateAndEmailZipOfPDFs/${lastRefreshDateTime.Result}", content, cancellationToken);
[Authorize]
[Route("api/pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime}")]
public void GenerateAndEmailZipOfPDFs([FromBody]List<UrlObject> urls,DateTime lastRefreshDateTime)
You are most likely experiencing a deadlock by mixing a blocking call like .Result in an async function.
You need to use await.
var lastRefreshDateTime = await _contentManagement.GetLastRefreshDateTime(cancellationToken);
Reference Async/Await - Best Practices in Asynchronous Programming
Also note how the URI is generated using $ - string interpolation
var uri = $"{_printApiUrl}pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime}";
await client.PostAsync(uri, content, cancellationToken);
There was an errant $ in the shown URI that would cause the date time to be malformed when posted.
Should also consider using a route constraint
[Authorize]
[Route("api/pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime:datetime}")]
public void GenerateAndEmailZipOfPDFs(
[FromBody]List<UrlObject> urls,
[FromRoute]DateTime lastRefreshDateTime
)
Reference Routing to controller actions in ASP.NET Core
Reference Routing in ASP.NET Core
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
This question is related to entire "async-await" implementation.
I will keep it simple.
Example:
HttpClient client = new HttpClient();
string urlContents = await client.GetStringAsync("http://msdn.microsoft.com");
Is there any reason to use client.GetString()?
Why not just name GetStringAsync as GetString, and get rid of non-async GetString? And get rid of async-await keywords at all?
I read many posts, but didn't find a clear simple explanation.
Why create so many duplicate methods, what's the point? It just doesnt feel right. Give me a reason why use non-async method? Don't you want long running methods to use available threads and CPU cores to run things in parallel?
(obviously it has nothing to do with the web-based ajax async operations)
Why not just name GetStringAsync as GetString, and get rid of non-async GetString?
There are two reasons for this:
Doing so would break backward compatibility - programs that rely on GetString returning a string rather than a Task<string> would stop compiling, and
Requiring await would make it hard to use the API from non-async methods - programmers would need to write additional code to wait for the task to complete.
Each of these considerations is disqualifying by itself.
Note that once your API is in use, you must be extremely cautious about "breaking changes", i.e. changes that break code relying on your API. Changing method's return type, along with a fundamental part of its functionality (synchronous vs. asynchronous) is definitely a breaking change.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
All in the Title.
Also please give an answer in less than a week.
I need someone to really help me with this, I'm using an online course and it says I have to Write the NumberBoard method, then write a NumberBoard constructer and call the method.
Here's the link to the .zip file that has the PDF telling me what to do, and the project that they give you:
https://d396qusza40orc.cloudfront.net/gameprogramming%2Frequired_assessment_materials%2FRequiredProjectMaterials.zip
(Copy and paste if link not clickable)
To get to the project, open the code folder and click the "GameProject" thing
Something like this will work
public class SomeClass
{
public SomeClass() //constructor
{
SomeMethod(); //Calling the method in constructor
}
public void SomeMethod() // Method
{
//Method implementation
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I use this code blow in .NET. It works fine. The problem is that I want this audio to play in the root of the website. What changes should I make for this? Thanks
var sample= new System.Windows.Media.MediaPlayer();
sample.Open(new System.Uri( #"D:\voices\1.wav");
sample.Play();
In a web application, this might look something like this:
sample.Open(new System.Uri(Server.MapPath("~/") + #"\voices\1.wav");
I say might because that all depends on whether or not the voices folder exists in the root of the website. Additionally, you should probably leverage Path.Combine instead:
var path = Path.Combine(Server.MapPath("~/"), "voices", "1.wav");
sample.Open(path);
Finally, I don't know what sample is, but the Open method may not work in a website. I'm making the assumption you know what Open does and whether or not it can work in a website.
Use server.mappath("~/") <- that's the filesystem root for your website.
dim path as string = server.mappath("~/") & "/voices/1.wav"
Note that backslashes, for filesystem path, not URI.
Hope it helps.