Do I have to explicitly dispose my db connection object in MVC? - asp.net-mvc-4

Do I HAVE to explicitly dispose of a db connection object using a dispose() method in my controller?
As the base Controller class already implements the IDisposable interface do I need to put a dispose() in my controller?
Or will the garbage collector do this for me anyway...what's the point if this is the case?

Use using statements
using(var connection = new SqlConnection())
{
...
}
It is equivalent to:
try
{
var connection = new SqlConnection()
...
}
finally
{
connection.Dispose()
}

Yes you should close it by using a "using" statement like Selman22 showed. The problem with waiting for the garbage collector to dispose of your connection after it falls out of scope is the garbage collector will only dispose of the connection when your application needs the memory. The garbage collector does not constantly clean up objects as they fall out of scope. If your application is not using a lot of memory, it could potentially be a while before that connection is closed.

Related

Totally form Disposing and free up memory

I already read a ton of topics "class disposing" about and I did understand how to do it and it works fine! Meanwhile "form disposind" is not same and resources still leaking!
What I am doing:
Initializing form by "using" method:
Using f as New Form
f.ShowDialog()
'my code
End using
In this case by "end using" form terminated with "Dispose" method.
For example:
I have a form which have a class (it contain parameters and other classes).
On initialization form event, form (itself) take 12Mb of memory (I measure it in process explorer) and plus my class take 10Mb (which Disposable and also disposing before Dispose form on Dispose event). After my form disposed (I measure it again) I see my class disposed and resources (of class) are free totaly, the form is not freeing resources. In other words I kill 10Mb (of class) BUT DON'T kill 12Mb (of form).
My Dispose code:
Private Sub Form_Disposed(sender As Object, e As EventArgs) Handles
Me.Disposed
If Not fDisposed Then
MyPersonalClass.Dispose()'class
MyPersonalClass = Nothing
fDisposed = True
GC.SuppressFinalize(Me)
GC.Collect()
Finalize()
End If
End Sub
It's very bad. If most of time I working with these forms and if they are don't free resources after dispose, each opened form will be leave approx 10-12Mb in memory after closed. And if I open my form 100 times it will freezes more then 1Gb of memory.
What I doing wrong?
I'm not really familiar with VB, but I think it will do things similar as to how they are done using winforms for C#
The easiest way to make sure that all Disposable objects of A Form are disposed when the form is disposed, to keep a collection of Disposable objects. You could use the existing class Sytem.ComponentModel.Component class for this. The disadvantage is that it only accepts objects that implement interface IComponent. If you have only a few classes that must be disposed that don't have this interface yet, this is the easiest method. Otherwise design your own DisposableCollection
class DisposableCollection : List<object>, IDisposable
{
public bool IsDisposed {get, private set} = false;
private IEnumerable<IDisposable> DisposableItems => this.OfType<IDisposable>();
public void Dispose()
{
if (!this.IsDisposed)
{
// Dispose all disposable items
foreach (IDisposable disposableItem in this.DisposableItems)
{
disposableItem.Dispose();
}
this.disposed = true;
}
}
}
Usage:
class MyForm : Form
{
private readonly DisposableCollection disposables = new DisposableCollection();
public MyForm()
{
// create and add all your items to disposables
}
protected override void OnDisposing(bool disposing)
{
this.disposables.Dispose();
}
}
If desired you can add event handlers to notify others that the object is being disposed

Will be dispose called on dependency injection object?

I am concerning, if GC will call dispose method on IDisposable argument passing to object creating in using() {} statement.
HttpClient
and
HTTPHttpClientHandler
are IDisposable classes.
var handler = new HttpClientHandler();
using (var client = new HttpClient(handler))
{
}//1
//1 In this point GC will call dispose() method on client.
Does it mean, that dispose() method will be called on handler too ?
Yes.
If you view the code for the HttpClient.Dispose(), it calls base.Dispose() which does the following:
this.handler.Dispose(); where handler is an instance of HttpMessageHandler and HttpClientHandler : HttpMessageHandler.
Also, if you ran the code you have written as is, once the using block has completed, you will see that the handler.disposed is set to true (using the debugger in Visual Studio).

Is WCF MessageBuffer.CreateMessage thread safe?

http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messagebuffer(v=vs.85).aspx is somewhat vague when it says that "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."
As a general rule, it seems like instance members do not have the thread-safe guarantee.
However, I'm guessing that some instance member methods are reentrant and thread-safe and others are not.
MessageBuffer.CreateMessage is an instance method.
Has anyone confirmed whether this specific method is reentrant (or whether callers need to implement locking around calls to the method) ?
I don't think until explicity specified, instance methods are always non thread safe. You can look at this method through reflector to confirm.
Also why are you concern about thread safety of this method? What is your usage scenario?
MessageBuffer.CreateMessage is abstract, so it doesn't make sense to ask whether it's thread safe or not. The subclasses of MessageBuffer in WCF are all internal, so they can potentially be changed. As Chandermani said, you should assume that it is not thread-safe.
Update: it is not thread-safe. The created message may have dependencies on other components, such as serialization of the message body. If those components aren't thread-safe, then the CreateMessage call cannot be considered thread-safe either.
In the example below, the serialized version of the object is time-dependent (it could have some additional dependencies as well), so the order in which the CreateMessage calls are made impacts the result.
public class StackOverflow_6209650_751090
{
[DataContract]
public class MyDC
{
[DataMember]
public DateTime SerializedTime
{
get { return DateTime.Now; }
set { }
}
}
public static void Test()
{
Message message = Message.CreateMessage(MessageVersion.None, "foo", new MyDC());
var buffer = message.CreateBufferedCopy(int.MaxValue);
Console.WriteLine(buffer.CreateMessage());
Console.WriteLine();
Console.WriteLine(buffer.CreateMessage());
}
}

Memory leak with addionatl NHibernate listeners?

i'm using Configuration.AppendListeners for some addional listeners. With the appened listeners the destructor is only called when the program ends - without the additional listeners the destructor is called on System.GC.Collect.
Ad a workaround I implemented IDisposable where I call following method:
private void CleanUpConfigurationListener()
{
if (configuration == null) return;
foreach (NHibernate.Event.ListenerType item in Enum.GetValues(typeof(NHibernate.Event.ListenerType)))
{
configuration.SetListener(item, null);
}
}
With it, the destructor is called again.
Is it a memory leak?
I'm using NH 3.0 because of Fluent NHibernate (1.2).
Thanks for your answers.
It was my fault. I found the problem:
The code created a new SessionFactory for each new DAL-Session object :(

Need a way to close a ChannelFactory when the client only know of the interface

please help me solve this issue.
I have a client using WCF. I don't want this client to know that it gets its data from a service. I want to use a model like this so I it easier for me to unit test the code and to later replace the implementation of the given interface. The problem with this model is that I'm not able to call close on the ChannelFactory. I'm not sure that I need to call close, but it feels right.
So my question is:
Does anybody know of a good pattern for closing the ChannelFactory when the client only should know the interface of the service? Or is the ChannelFactory closed by it self by some kind of magic?
Below is some sample code that I hope will you understand the question:
static void Main(string[] args)
{
ITestService service = GetTestService();
Console.WriteLine(service.GetData(42));
}
private static ITestService GetTestService()
{
var cf = new ChannelFactory<ITestService>(new WSHttpBinding(), new EndpointAddress("http://localhost:8731/TestService/"));
return cf.CreateChannel();
}
Thanks,
GAT
The best way to solve this is to keep the reference to the ChannelFactory alive for as long as you need it. So take the creation of the ChannelFactory out of the GetTestService method and create the factory on initialization of your class (in the constructor for example) and close it when you no longer need the factory (in the Dispose method of your class for example).
ChannelFactory is a factory class that you can reuse as many times as you like, so there is no need to create it each time you need a service implementation.
You should implement class like
public class MyClient : ITestService
{
private ChannelFactory<ITestService> factory;
private ITestService proxy;
//...
//expose here methods Open and Close that internally will open and close channel factory
//implement interface members
}
The lifetime of the client here will be the same as lifetime of channel factory.
Have you tried casting it to a IClientChannel and then calling Close?
((IClientChannel)service).Close();