c#-5.0

How to handle exceptions thrown by Tasks in xUnit .net's Assert.Throws<T>?

試著忘記壹切 提交于 2020-01-21 02:37:05
问题 The following asynchronous xUnit.net test with a lambda marked with the async modifier fails by reporting that no exception was thrown: [Theory, AutoWebData] public async Task SearchWithNullQueryThrows( SearchService sut, CancellationToken dummyToken) { // Fixture setup // Exercise system and verify outcome Assert.Throws<ArgumentNullException>(async () => await sut.SearchAsync(null, dummyToken)); // Teardown } To make sure that an ArgumentNullException is actually thrown I explicitly used a

Explanation for C# Language Specification: 6.2.4 Explicit reference conversions

对着背影说爱祢 提交于 2020-01-15 04:20:28
问题 As I mentioned in this post, I faced a for me not understandable compiler behaviour. The code: IEnumerable<IList<MyClass>> myData = //...getMyData foreach (MyClass o in myData){} It compiles, but fails on runtime: InvalidCastException; for me it is obvious. If I change the IList to List as following, it complains: IEnumerable<List<MyClass>> myData = //...getMyData foreach (MyClass o in myData){} When instead of the class type I put var as following, intellisense recognizes the correct type:

C# Static Analysis, possible values for a variable/parameter

拥有回忆 提交于 2020-01-12 03:24:07
问题 In code similar to each of the following examples, I would like to be able to statically analyze code to determine the list of possible values that are passed into SpecialFunction(). SpecialFunction(5); // A int x = 5; SpecialFunction(x); // B int x = 5; x = condition ? 3 : 19; SpecialFunction(x); // C I can already parse the C# into an abstract syntax tree and I can already handle cases like A, and I guess I could keep track of initial assignments of values to guess case B, but cases as easy

Site stopped working in asp.net System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to

走远了吗. 提交于 2020-01-11 15:05:08
问题 I have a problem like this on server [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages

EF6 alpha Async Await on an Entity Stored Procedure / Function Import?

五迷三道 提交于 2020-01-11 05:38:09
问题 I'd like to apply the new async await functionality to Stored Procedures / Function Imports imported in my Entity model, but have as yet been unable to with the EF6 alpha. Is it yet possible in EF6 alpha2 (or the nightly build as of 20211) to call any of the new Async methods on an Entity Function Import (which calls a SQL Stored Procedure) that returns a collection of Complex Type? e.g. private async Task<IList<Company>> getInfo (string id) { using (CustomEntity context = new CustomEntity())

Error 401 in C#

不羁岁月 提交于 2020-01-03 02:26:20
问题 I'm working on my first app for Windows 8 but I have a problem, when I'm launch my app, I've got the error "Response status code does not indicate success: 401 (Authorization Required) So yes, I need to include username and password but I don't know where in my code: var fond = new HttpClient(); var reponse = await fond.GetStreamAsync("http://example.com/search/mario%20kart" + TitleNewsGrid.Text); So where I'm include the username and password in the code? 回答1: There should be a Credentials

Async code without waiting for completion

烂漫一生 提交于 2020-01-02 03:27:07
问题 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

Using C# 5 async feature in Linqpad

大憨熊 提交于 2020-01-01 08:40:48
问题 Is it possible to use C# 5 async features in Linqpad snippets? Does anyone know of any hack/beta which allows you to do it? 回答1: Installing the async CTP should be enough - async code should compile in LINQPad (although the Intellisense will show red squigglies). I'll look at dealing to the red squigglies in the next beta :) You will have to add a reference to asyncctplibrary.dll, as in VS. Update: the red squigglies and autocompletion has been dealt to in the latest beta. 来源: https:/

Is it OK to have virtual async method on base class?

戏子无情 提交于 2020-01-01 00:58:07
问题 I am working with some code, where I have 2 classes with very similar logic and code. I have protected async void LoadDataAsync() method on both classes. Currently I am refactoring it and thinking to move shared logic to base class. Is it OK to have virtual async method on base class and override it on derived classes? Are there any issues with it? My code looks like this: public class Base { protected virtual async void LoadDataAsync() {} } public class Derived : Base { protected override

When to use OrderByCompletion (Jon Skeet) vs Parallel.ForEach with async delegates

别来无恙 提交于 2019-12-31 22:41:52
问题 Recently Jon Skeet at NDC London spoke about C# 5 async/await and presented the idea of " ordering by completion " a list of async tasks. A link http://msmvps.com/blogs/jon_skeet/archive/2012/01/16/eduasync-part-19-ordering-by-completion-ahead-of-time.aspx I am a bit confused or should I say I am not sure when will this technique be more appropriate to use. I cannot understand the difference between this and the below example var bag = new ConcurrentBag<object>(); Parallel.ForEach