MySQL sleeping connection April 19, 2009
Posted by Dante in MySQL, c#.add a comment
I finally found a solution for the problem I encountered using MySQL connector for .NET in conjunction with IIS and ASPX. Using the connector for NET, for a reason that nobody has solved yet, when you close the connection the server process remain in sleep state instead of pull off. So when the number of process reach the max numbers of connections MySQL crash !!! Until now I have chosen to use the ODBC connector instead of .NET connector, but, renouincing performance ! Now I have found a method that allows me to use the .NET connector and force the close of the connection exploiting the “kill” command.
public bool CloseConnection(MySqlConnection MyConn)
{
int PID = MyConn.ServerThread;
try
{
string SQL = "kill " + PID.ToString();
MySqlCommand MyCommand = new MySqlCommand(SQL, MyConn);
MyCommand.ExecuteNonQuery();
MyConn.Close();
MyCommand.Dispose();
}
catch
{
return false;
}
return true;
}
The connection object (MyConn in the example) has a “readonly” property that return the “MySQL process id”: ServerThread. So, you can send the “kill” command and pull off the connection via SQL command.
M10MailServer must start at window startup April 13, 2009
Posted by Dante in M10MAIL, c#.add a comment
Using the “OnStart” method you can perform the steps necessary to implement the code to start the application. Usually in the “OnStart” method must be defined a timer, the timer approach is the most common method used in the services.
protected override void OnStart(string[] args)
{
StartTimer.Elapsed += new ElapsedEventHandler(StartService);
StartTimer.Interval = 2000;
StartTimer.AutoReset = false;
StartTimer.Enabled = true;
}
“StartTimer” is a object create using the “Timer” class in the “System.Timers” namespace.
private System.Timers.Timer StartTimer = new System.Timers.Timer();
This object must be defined at the top level of the class to be access in each class method.
namespace M10MailServer
{
public partial class M10MailServer : ServiceBase
{
private System.Timers.Timer StartTimer = new System.Timers.Timer();
The code is in the “StartService” method defined as event in the “StartTimer.Elapsed” and occurs when the interval elapses.
M10MAIL Server, my longer and not yet finished project April 4, 2009
Posted by Dante in M10MAIL, c#.Tags: mail protocols, windows service
add a comment
The electronic mail has always been the argument I prefer from the point of view of the programming. In a mail server there are a lot of stuff to learn and many of these are amazing. I developed this project like an hobby, not for my job, since 2005 using c#, reading documentation about the protocols and searching all the helpfull tips pubblish on the web.
Protocols used to send, read and store emails are:
- SMTP, simple mail transfer protocol defined in the RFC 2821
- POP3, post office protocol defined in the RFC 1939
- IMAP4, internet access message protocol defined in the RFC 3501
Discover that behind a mail server there are three protocols coded was the incentive for me to start this project.
To develop M10MAIL Server I’ve used: Visual Studio, C# and SQLite.
The components running are:
- M10mailserver, like a windows service starts at the windows startup. This component call and check the runing of the other components.
- M10smtp, the SMPT Server listen on tcp port 25 (or other tcp port) for incoming connection.
- M10router, the SMTP client that deliver the email to the final destination.
- M10pop3, the POP3 server listen on tcp port 110 (or other tcp port).
- M10imap4, the imap4 server listen on tcp port 143 (or other tcp port).
The first componet to develope is M10mailserver, using Microsoft Visual Studio and start a new solution, then add a new project choosing in the “projects template windows” the “windows service template” that prepare for you the class to build a “windows service” and appear in this way:
public partial class M10MailServer : ServiceBase
{
public M10MailServer()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
Rename the solution and the project “M10MailServer” and rename the class name in the “windows service” project in M10MailServer. Now, the template addes for you two methods, “onStart” and “OnStop” but you need other methods to use your “windows service” .
Add this lines to your project:
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
SevicesToRun = new System.ServiceProcess.ServiceBase[] { new M10ailServer() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
Now we are ready to add the statements in the “OnStart” method to tell to “m10mailserver” what must be done !