on
1-minute read
Breaking changes in SmtpClient in .NET 4.0
In .NET 4.0 the SmtpClient class now implements IDisposable. This is a breaking change what you should watch out for.
For .NET 4.0 the BCL team decided to pool SMTP connections, just as .NET already did with database connections. This of course means that the SmtpClient class should implement IDisposable, just as the SqlConnection does. When STMP connections are pooled, the overhead over establishing a new connection is lowered, which is a good thing. However, this is a breaking change. Migrating your code to .NET 4.0, without any changes, could lead to the same connection pool timeout exceptions as we’re are used with database connections.
Perhaps there are more of these ‘hidden jams’ inside the new .NET 4.0 framework. So when migrating to .NET 4.0, it’s wise to recompile your project and run FxCop over it. When your code isn’t too complicated, FxCop will spot the places where you didn’t dispose any disposable object. And you can already prepare your code like this:
var client = new SmtpClient();
// Do not remove this using. In .NET 4.0
// SmtpClient implements IDisposable.
using (client as IDisposable)
{
client.Send(message);
}
Comments
Wish to comment?
Found a typo?
Buy my book
Dependency Injection Principles, Practices, and Patterns. If you're interested to learn more about DI and software design in general, consider reading my book. Besides English, the book is available in Chinese, Italian, Polish, Russian, and Japanese.
I coauthored the book