10

I want to add async support to current VS 2010 .NET 4.0 C# project

I have found:

I don't even get real difference between them.

I installed both. Visual Studio Async CTP (Version 3), Microsoft.Bcl and Microsoft.Bcl.Async. (also used to run tools\portable-net40+sl4+win8+wp71\install.ps1 in Microsoft.Bcl)

And still can't see any effect. Same error for

public async Task<CommResponse>

->

Error   37  The type or namespace name 'async' could not be found (are you missing a using directive or an assembly reference?)

So is it real how should I use this stuff?

cnd
  • 32,616
  • 62
  • 183
  • 313
  • Possible Duplicate : http://stackoverflow.com/questions/12056525/proper-way-to-use-async-with-vs-2010-now-that-vs-2012-is-released – cvraman May 15 '13 at 04:52
  • 1
    @cvraman sorry but I can't find some answer that will help me there. Because they speak there alike Async CTP works for them and it's not for me. – cnd May 15 '13 at 05:01
  • @Chipzilla thanks but I've got .NET 4.0 set there and I want to have async working with it. And I wonder if it even possible? – cnd May 15 '13 at 05:17
  • As far as I know, the async keyword needs the VS2012 C# compiler (aka .Net 4.5 compiler) even though you are targeting .Net 4.0, because the compiler needs to understand `async`. The .Net 4.0 support is a library issue, which is how that can work - but you still need the 4.5 compiler. *As far as I know!* I used this btw (with VS2012, not VS2010): http://www.microsoft.com/en-us/download/details.aspx?id=29576 – Matthew Watson May 15 '13 at 06:19
  • But Visual Studio Async CTP says it can add such functional to VS 2010. – cnd May 15 '13 at 06:27
  • @Heather You're right, it does seem to say that (e.g. [here](http://msdn.microsoft.com/en-us/magazine/hh456401.aspx)). I don't know why this doesn't work for you then. :( – Matthew Watson May 15 '13 at 07:35
  • @Heather But it's also a *CTP*, i.e. code that's not release quality and probably shouldn't be used in production. – svick May 15 '13 at 09:45
  • 1
    Did you add a reference to `AsyncCTPLibrary.dll`? In the Solution Explorer, right-click on your project and choose `Add Reference`. You should see a 'browse' section and you can add the library through there. You can find it in your `Documents` folder under `Microsoft Visual Studio Async CTP/Samples`. You may want to create a new project to test this out, just in case... – pcnThird May 16 '13 at 00:29
  • well it could be strange but there is no folder `Microsoft Visual Studio Async CTP` in My Documents – cnd May 16 '13 at 04:27
  • The location of the dll depends on where you installed the Async CTP. I should've mentioned it in my previous comment. – pcnThird May 16 '13 at 05:19
  • @Chipzilla it doesn't say where installs. – cnd May 16 '13 at 06:04
  • @Chipzilla same after adding library (got it from nuget...). Does it work for you? – cnd May 16 '13 at 11:40
  • I haven't attempted it myself as I'm using Visual Studio 2012. But according to one of my books you need the `AsyncCTPLibrary` in order to use thee `async` and `await` keywords in VS 2010. – pcnThird May 16 '13 at 13:43
  • I think the easiest solution would be to upgrade. – zmbq Jul 01 '13 at 05:21
  • @zmbq with upgrade you mean buying new Visual Studio? Only if I can explain to my direction why do I need it. And also, there is VS2013 coming so I'm not sure if that is sane to buy 2012 today. – cnd Jul 01 '13 at 07:44
  • That was my intention, yes, but, of course, I have no idea what your budget constraints are. I do know that you probably do not want async production code developed with Visual Studio 2010. You want .NET 4.5 and C# 5. – zmbq Jul 01 '13 at 08:06
  • I'll turn it into an answer and elaborate – zmbq Jul 01 '13 at 09:32
  • 1
    @Heather: If you buy VS2012 with an MSDN subscription, you get VS2013 for free. – Stephen Cleary Jul 01 '13 at 10:33
  • @StephenCleary Thank you, that's new for me to know but as far as I know MSDN subscription costs a lot. – cnd Jul 02 '13 at 04:03

2 Answers2

4

I don't think you should do that. The Visual Studio 2010 version of async/await is more of a preview than anything else. If you want to use this in real, production level code, you should definitely upgrade to Visual Studio 2012 (or 2013, if you can wait a little while).

If you don't need this in real production code that requires Visual Studio Pro for some reason, and are just toying around, you can use Visual Studio 2012 Express.

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • The Async CTP had many installation problems due to the way the installer worked. @Heather, check out the MSDN forums; missing the Async CTP directory is a sure sign of this. If you installed the CTP right when it was released, it would work, but it breaks if you have other VS updates installed before the CTP. MS is not going to fix this; the only way forward is VS2012. – Stephen Cleary Jul 01 '13 at 10:32
  • The topic is becoming to complex and going out from actual solution for VS2010 so and I even can't find better solution for myself, I can't search it for ages so I will try to avoid async in 2010. Upgrading to newer version is completely another question for me but I set this answer as solution to close this question. – cnd Jul 02 '13 at 04:06
-1

I am working on something similar (I'm writing a RestApiLibrary in VS2010, that I borrowed from a VS2017 project) and found the following URL to be helpful.

https://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee

The main thing that helped was:

        // Send a request asynchronously continue when complete 
        client.GetAsync(_address).ContinueWith( 
            (requestTask) => 
            { 
                // Get HTTP response from completed task. 
                HttpResponseMessage response = requestTask.Result; 

                // Check that response was successful or throw exception 
                response.EnsureSuccessStatusCode(); 

The 'ContinueWith' method, and the '.Result' property seemed to be key to (sort of using) an async function in VS2010. Note, I doubt this behaves in the traditional async way, but at least this way you can use async methods in VS2010.

Hope this helps!

Jeff Moretti
  • 613
  • 5
  • 6