dispose

Does one need to close both NetworkStream and TcpClient, or just TcpClient?

江枫思渺然 提交于 2019-12-03 11:27:57
I'm reading the documentation on TcpClient.Close() and noticed this: Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was created. So, correct me if I'm wrong but this says that if one calls Close() on the TcpClient that the NetworkStream will also be closed. So then why at the end of the code example are both Close() called? networkStream.Close(); tcpClient.Close(); Would it be just as fine to only call tcpClient.Close(); ? Responding to this question since no one else

How to dispose resources with dependency injection

倾然丶 夕夏残阳落幕 提交于 2019-12-03 10:45:46
问题 I'm using StructureMap to resolve references to my repository class. My repository interface implements IDisposable, e.g. public interface IMyRepository : IDisposable { SomeClass GetById(int id); } An implementation of the interface using Entity Framework: public MyRepository : IMyRepository { private MyDbContext _dbContext; public MyDbContext() { _dbContext = new MyDbContext(); } public SomeClass GetById(int id) { var query = from x in _dbContext where x.Id = id select x; return x

Does the .dispose() method do anything at all?

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:37:24
I was experimenting with ways to get rid of some memory leaks within my application the other day when I realized that I know virtually nothing about cleaning up my resources. I did some research, and hoped that just calling the .dispose() would solve all of my problems. We have a table in our database that contains about 65,000 records. Obviously when I fill my dataset from the data adapter, the memory usage can get pretty high. When I called the dispose method on the dataset, I was surprised to find out that NONE of the memory got released. Why did this happen? Clearing the dataset doesn't

WCF ChannelFactory and channels - caching, reusing, closing and recovery

耗尽温柔 提交于 2019-12-03 07:26:18
问题 I have the following planned architecture for my WCF client library: using ChannelFactory instead of svcutil generated proxies because I need more control and also I want to keep the client in a separate assembly and avoid regenerating when my WCF service changes need to apply a behavior with a message inspector to my WCF endpoint, so each channel is able to send its own authentication token my client library will be used from a MVC front-end, so I'll have to think about possible threading

Thread-Safety of Dispose methods?

我与影子孤独终老i 提交于 2019-12-03 06:34:02
MSDN documents the thread-safety of instances members of BCL types pretty well, but I have never really seen information indicating how the Dispose method of IDisposable types can be called. Is the Dispose method a) guaranteed to be thread-safe for all classes, b) never guaranteed to be thread-safe, c) guaranteed to be thread-safe for some classes (if so, where is this specifically documented)? Finally, if the Dispose method is guaranteed to be thread-safe, does that mean I have to put a lock around each instance method in the class that uses disposable resources? Side point: I'm aware that

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

感情迁移 提交于 2019-12-03 06:16:15
I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHorse { void Eat(IApple apple); // is supposed to call apple.Consume() } interface IHorseKeeper { void FeedHorse(); // is supposed to call horse.Eat(apple) // where 'horse' is injected into IHorseKeeper // and 'apple' is generated by IHorseKeeper on-the-fly } Further, I

How to wait for a single event in C#, with timeout and cancellation

廉价感情. 提交于 2019-12-03 05:07:54
So my requirement is to have my function wait for the first instance an event Action<T> coming from another class and another thread, and handle it on my thread, allowing the wait to be interrupted by either timeout or CancellationToken . I want to create a generic function I can reuse. I managed to create a couple options that do (I think) what I need, but both seem more complicated than I'd imagine it should have to be. Usage Just to be clear, a sample use of this function would look like this, where serialDevice is spitting out events on a separate thread: var eventOccurred = Helper

C#: Do I need to dispose a BackgroundWorker created at runtime?

那年仲夏 提交于 2019-12-03 04:13:44
问题 I typically have code like this on a form: private void PerformLongRunningOperation() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { // perform long running operation here }; worker.RunWorkerAsync(); } This means that I don't dispose the BackgroundWorker , whereas if I had added it by the form designer then I think it would get disposed. Will this cause any problems? Is it more correct to declare a module-level _saveWorker , and then call Dispose on it from

Correct disposing using Repository and Unit Work patterns with Entity Framework?

社会主义新天地 提交于 2019-12-03 02:51:22
Cheers!I have some doubts about using Unit of Work with Repository. Specially a role of child context from Entity Framework. I have searched a lot of information about this theme, but all that I found just different types of using patterns, I'm confused and I can't understand main think. 1.Where I should realize disposing and saving? -Is it correctly realize Disposable in Inheritance class of DbContext? After that realize in Repository and Unit of Work or just in Uni fo Work? -Where put method Save in Unit of Work or Repository? My repository will be Generic Is my code is correct in architect

C# how to implement Dispose method

会有一股神秘感。 提交于 2019-12-03 02:00:06
问题 I need some advice on the implementation of the Dispose method. In our application the user designs their own UI. I have a preview window that shows what the UI is going to look like. All object drawn in this UI ultimately derive from a common base class ScreenObject. My preview manager contain a single object reference to a ScreenGrid which is the grid object for the entire preview area. Question #1 Some of my derived screen classes hold onto unmanaged resources, such as a database