c#-5.0

Propagate OperationContext into Async WCF Call

偶尔善良 提交于 2019-12-06 02:05:18
问题 With C#5 Async-Await in WCF, after an await if rest of the code continues on a different thread, we loose the Current Operation Context. (OperationContext.Current is null). I am working on a WCF Service which calls another external service. And there are a few Custom Binding Extensions used in the external service call which access the Operation Context. So I need the Context to be propagated during this call and it cant just work with copying the operation context into a local variable. My

Winforms asynchronous loading large data?

无人久伴 提交于 2019-12-06 00:36:24
I just received a bug list for an old app developed yeah years ago and one of the things i need to sort out is the amount of time it takes to load data into one screen,of course, while the screen is frozen and unfortunately this is in WinForms .NET 4.5. The data is loaded into a WinForms DataGridView. I would like to find out if there is any way of loading this data using C# 5 async and await,while refreshing the grid to add the next set of data. It may be while scrolling or in the background.Any ideas? Try loading all of the data into an array from an asynchronous thread and then using Invoke

Overload resolution, extension methods and genericity in C#

你说的曾经没有我的故事 提交于 2019-12-05 17:27:50
I have the following scenario in my C# source: class A{} class Dispatch<T>{} static class DispatchExt { public static void D<T>(this Dispatch<T> d, int a) { Console.WriteLine("Generic D chosen with a = " + a.ToString()); } public static void D(this Dispatch<A> d, int a) { Console.WriteLine("D<A> chosen with a = " + a.ToString()); } } class Program { static void D<T>(Dispatch<T> d, int a) { d.D(a); } static void Main(string[] args) { int a = 5; var dispatch = new Dispatch<A>(); dispatch.D(a); D(dispatch, a); } } When I run this code the output is: " D<A> chosen with a = 5" "Generic D chosen

Registering async factory in Autofac

99封情书 提交于 2019-12-05 10:53:41
I have a Wallet class that I get from a repository. I'm trying to properly register both in Autofac so classes using the wallet could have a proper instance injected. The problem is that the repository uses an async method (returning Task). Does Autofac support such cases? This doesn't work: cb.RegisterType<WalletRepository>() .As<IWalletRepository>() .SingleInstance(); cb.Register(async c => await c.Resolve<IWalletRepository>().CreateAsync(App.WalletPath)); cb.RegisterType<ViewModel>() .AsSelf(). .SingleInstance(); Somewhere in the app I just have: class ViewModel { public ViewModel(Wallet

Async code without waiting for completion

大兔子大兔子 提交于 2019-12-05 08:29:52
I'm currently having issues with IIS crashing out leaving the following messages in the event log. They're not too helpful in directing me to the actual source of the error but a bit of research suggests this is just a case of spawning tasks but not waiting for the result, when they do eventually complete if the parent process has completed it cannot be associated with the parent thread which causes a null reference exception. Is that correct? For the most part I have removed or added awaits where this was going on but there are some areas where it is handy. One such example is on session

HttpContent.ReadAsStringAsync causes request to hang (or other strange behaviours)

荒凉一梦 提交于 2019-12-05 03:42:09
We are building a highly concurrent web application, and recently we have started using asynchronous programming extensively (using TPL and async / await ). We have a distributed environment, in which apps communicate with each other through REST APIs (built on top of ASP.NET Web API). In one specific app, we have a DelegatingHandler that after calling base.SendAsync (i.e., after calculating the response) logs the response to a file. We include the response's basic information in the log (status code, headers and content): public static string SerializeResponse(HttpResponseMessage response) {

How to implement the TryDoSomething pattern with async [duplicate]

我的未来我决定 提交于 2019-12-05 01:51:12
问题 This question already has answers here : How to write a async method with out parameter? (11 answers) Closed 3 months ago . I often use the TryDoSomething pattern like this totally made up example: GameContext gameContext; if (gamesRepository.TryLoadLastGame(out gameContext)) { // Perform actions with gameContext instance. } else { // Create a new game or go to home screen or whatever. } This allows a nice readable flow but it also allows a success status true but a null return value which is

Read headers from HttpResponseMessage before Content is 100% complete

佐手、 提交于 2019-12-05 01:41:04
How do I access the response headers, before the entire response has been streamed back? How do I read the stream as it arrives? Is HttpClient my best choice for such granular control of receiving the http response? Here's a snip that might illustrate my question: using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { var streamTask = response.Content.ReadAsStreamAsync(); //how do I check if headers portion has completed? //Does HttpCompletionOption.ResponseHeadersRead guarantee that? //pseudocode while (!(all headers have been received)) /

Many awaits for Async method, or a single await for a wrapping Task.Run?

孤者浪人 提交于 2019-12-04 17:08:19
Suppose we have to write down on database a list of 1000 elements, through an async flow. Is it better to await 1000 times an asynchronous insert statement, or to wrap all the 1000 inserts in one single synchronous method encapsulated into a Task.Run statement, awaiting one single time? For example, SqlCommand has every method coupled with his async version. In this case, we have an insert statement, so we can call ExecuteNonQuery or ExecuteNonQueryAsync . Often, on async/await guidelines, we read that if you have an asynchronous version available for some method, you should use it. So suppose

Is there a way to Imitate C# 6 Null-Conditional operator in C# 5

假装没事ソ 提交于 2019-12-04 08:57:26
I have a situation where I need to assign some objects' properties inside an object initializer. Some of these objects can be null and I need to access their properties, the problem is that they are too many, and using a if/else thing is not good. Example visits = visitJoins.AsEnumerable().Select(joined => new VisitPDV() { VisiteId = joined.Visite.VisiteId.ToString(), NomPointDeVente = joined.VisitePdvProduit.PointDeVente.NomPointDeVente, }); The joined.VisitePdvProduit can be null, and the problem is that there are like dozens of such assignments (I just took one to shorten the code) The C# 6