using-statement

HttpClient in using statement causes Task cancelled

狂风中的少年 提交于 2019-12-03 06:23:42
I created a FileResult : IHttpActionResult webapi return type for my api calls. The FileResult downloads a file from another url and then returns the stream to the client. Initially my code had a using statement like below: public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { try { HttpResponseMessage response; using (var httpClient = new HttpClient()) { response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new System.Net.Http.StreamContent( await httpClient.GetStreamAsync(this.filePath)) }; } return response; } catch (WebException exception) {

include and using namespace in C++

我的梦境 提交于 2019-12-03 03:51:26
for using cout , I need to specify both: #include<iostream> and using namespace std; Where is cout defined? in iostream , correct? So, it is that iostream itself is there in namespace std ? What is the meaning of both the statements with respect to using cout ? I am confused why we need to include them both. iostream is the name of the file where cout is defined. On the other hand, std is a namespace, equivalent (in some sense) to java's package. cout is an instance defined in the iostream file, inside the std namespace. There could exist another cout instance, in another namespace. So to

EF (entity framework) usage of “using” statement

江枫思渺然 提交于 2019-12-03 03:41:41
问题 I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where " using " statement is used, i.e. public Item GetItem(long itemId) { using (var db = new MyEntities()) { return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault(); } } Here we create a new instance of DBcontext MyEntities() . We using " using " in order to "ensure the correct use of IDisposable objects." It's only one method in my

What is the standard conform syntax for template constructor inheritance?

你离开我真会死。 提交于 2019-12-03 02:52:10
GCC 4.8.1 accepts template <typename T> class Subclass : public Baseclass<T> { public: using typename Baseclass<T>::Baseclass; }; but MSVC does not. On the other hand, MSVC accepts template <typename T> class Subclass : public Baseclass<T> { public: using typename Baseclass::Baseclass; }; but GCC does not. Then I've seen another kind of declaration in this questions: c++11 inheriting template constructors template <typename T> class Subclass : public Baseclass<T> { public: using typename Baseclass::Baseclass<T>; }; for which MSVC warns about an "obsolete declaration style" and GCC says prog

Dealing with .NET IDisposable objects

无人久伴 提交于 2019-12-03 01:54:41
问题 I work in C#, and I've been pretty lax about using using blocks to declare objects that implement IDisposable , which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't seem to indicate this in any way (am I just missing something?). Am I just supposed to check help every time I declare anything, and gradually build up an encyclopedic memory for which objects are and which are not disposable? Seems unnecessary,

Where do I put try/catch with “using” statement? [duplicate]

两盒软妹~` 提交于 2019-12-03 01:24:40
This question already has answers here : try/catch + using, right syntax (7 answers) Possible Duplicate: try/catch + using, right syntax I would like to try/catch the following: //write to file using (StreamWriter sw = File.AppendText(filePath)) { sw.WriteLine(message); } Do i put the try/catch blocks inside the using statement or, around it? Or both? If your catch statement needs to access the variable declared in a using statement, then inside is your only option. If your catch statement needs the object referenced in the using before it is disposed, then inside is your only option. If your

C# conditional using block statement

ⅰ亾dé卋堺 提交于 2019-12-02 21:08:49
I have the follow code but it is awkward. How could I better structure it? Do I have to make my consuming class implement IDisposable and conditionally construct the network access class and dispose it when I am done? protected void ValidateExportDirectoryExists() { if (useNetworkAccess) { using (new Core.NetworkAccess(username, password, domain)) { CheckExportDirectoryExists(); } } else { CheckExportDirectoryExists(); } } Jon Skeet One option, which is somewhat nasty but would work, based on the fact that the C# compiler calls Dispose only if the resource is non-null : protected void

EF (entity framework) usage of “using” statement

我与影子孤独终老i 提交于 2019-12-02 17:08:28
I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where " using " statement is used, i.e. public Item GetItem(long itemId) { using (var db = new MyEntities()) { return db.Items.Where(it => it.ItemId == itemId && !it.IsDeleted).FirstOrDefault(); } } Here we create a new instance of DBcontext MyEntities() . We using " using " in order to "ensure the correct use of IDisposable objects." It's only one method in my manager. But I have more than ten of them. Every time I call any method from the manager I'll be using

C# using statement catch error

眉间皱痕 提交于 2019-12-02 15:36:15
I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year; cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start; cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end; cmd.Connection.Open(); DataSet dset = new DataSet(); new SqlDataAdapter(cmd).Fill

Dealing with .NET IDisposable objects

霸气de小男生 提交于 2019-12-02 15:29:32
I work in C#, and I've been pretty lax about using using blocks to declare objects that implement IDisposable , which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't seem to indicate this in any way (am I just missing something?). Am I just supposed to check help every time I declare anything, and gradually build up an encyclopedic memory for which objects are and which are not disposable? Seems unnecessary, painful, and error-prone. How do you handle this? EDIT: Looking at the related questions sidebar, I found