Questions tagged [.net-4.5]

Version 4.5 of the Microsoft .NET Framework. Use for questions specifically related to .NET Framework 4.5. For questions on .NET Framework generally, use the .net tag.

Reference

3775 questions
696
votes
6 answers

async/await - when to return a Task vs void?

Under what scenarios would one want to use public async Task AsyncMethod(int num) instead of public async void AsyncMethod(int num) The only scenario that I can think of is if you need the task to be able to track its progress. Additionally, in…
user981225
  • 8,762
  • 6
  • 32
  • 41
446
votes
17 answers

How do I make calls to a REST API using C#?

This is the code I have so far: public class Class1 { private const string URL = "https://sub.domain.com/objects.json?api_key=123"; private const string DATA = @"{""object"":{""name"":""Name""}}"; static void…
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
412
votes
12 answers

Do HttpClient and HttpClientHandler have to be disposed between requests?

System.Net.Http.HttpClient and System.Net.Http.HttpClientHandler in .NET Framework 4.5 implement IDisposable (via System.Net.Http.HttpMessageInvoker). The using statement documentation says: As a rule, when you use an IDisposable object, you should…
Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
373
votes
3 answers

Do you have to put Task.Run in a method to make it async?

I'm trying to understand async await in the simplest form. I want to create a very simple method that adds two numbers for the sake of this example, granted, it's no processing time at all, it's just a matter of formulating an example here. Example…
Neal
  • 9,487
  • 15
  • 58
  • 101
221
votes
6 answers

What's the difference between Task.Start/Wait and Async/Await?

I may be missing something but what is the difference between doing: public void MyMethod() { Task t = Task.Factory.StartNew(DoSomethingThatTakesTime); t.Wait(); UpdateLabelToSayItsComplete(); } public async void MyMethod() { var result =…
Jon
  • 38,814
  • 81
  • 233
  • 382
220
votes
5 answers

Retargeting solution from .Net 4.0 to 4.5 - how to retarget the NuGet packages?

I have migrated a solution that is currently targeting .NET 4.0 in VS2010 to VS2012 and now I would like to re-target it to .Net 4.5 What I am not sure about is the NuGet packages. For example EF5, which I updated from EF4 in VS2010 turns out to be…
Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50
210
votes
4 answers

Using 'async' in a console application in C#

I have this simple code: public static async Task SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await firstTask + await secondTask; } private async Task
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
206
votes
11 answers

Is it possible to await an event instead of another async method?

In my C#/XAML metro app, there's a button which kicks off a long-running process. So, as recommended, I'm using async/await to make sure the UI thread doesn't get blocked: private async void Button_Click_1(object sender, RoutedEventArgs e) { …
Max
  • 9,220
  • 10
  • 51
  • 83
198
votes
2 answers

Why would finding a type's initializer throw a NullReferenceException?

This has got me stumped. I was trying to optimize some tests for Noda Time, where we have some type initializer checking. I thought I'd find out whether a type has a type initializer (static constructor or static variables with initializers) before…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
197
votes
10 answers

C# HttpClient 4.5 multipart/form-data upload

Does anyone know how to use the HttpClient in .Net 4.5 with multipart/form-data upload? I couldn't find any examples on the internet.
ident
  • 3,885
  • 3
  • 14
  • 8
190
votes
5 answers

Async/await vs BackgroundWorker

In the past few days I have tested the new features of .net 4.5 and c# 5. I like its new async/await features. Earlier I had used BackgroundWorker to handle longer processes in the background with responsive UI. My question is: after having these…
Tom
  • 3,899
  • 22
  • 78
  • 137
187
votes
4 answers

How to cancel a Task in await?

I'm playing with these Windows 8 WinRT tasks, and I'm trying to cancel a task using the method below, and it works to some point. The CancelNotification method DOES get called, which makes you think the task was cancelled, but in the background the…
Carlo
  • 25,602
  • 32
  • 128
  • 176
184
votes
1 answer

Catch-22 prevents streamed TCP WCF service securable by WIF; ruining my Christmas, mental health

I have a requirement to secure a streamed WCF net.tcp service endpoint using WIF. It should authenticate incoming calls against our token server. The service is streamed because it is designed to transfer large amounts of data n stuff. This…
user1228
184
votes
19 answers

Cleaner way to do a null check in C#?

Suppose, I have this interface, interface IContact { IAddress address { get; set; } } interface IAddress { string city { get; set; } } class Person : IPerson { public IContact contact { get; set; } } class test { private test() …
171
votes
5 answers

Targeting .NET Framework 4.5 via Visual Studio 2010

Today I installed the .NET Framework 4.5 on my machine expecting to be able to use it from Visual Studio 2010, since it's just a minor update that should't pose problems for Visual Studio 2010. Unfortunately I am not, even manually removing certain…
Golvellius
  • 1,928
  • 2
  • 13
  • 16
1
2 3
99 100