Saturday, November 29, 2008
Windows Azure
To build these applications and services, developers can use their existing Microsoft® Visual Studio® 2008 expertise. In addition, Windows Azure supports popular standards and protocols including SOAP, REST, and XML. Windows Azure is an open platform that will support both Microsoft and non-Microsoft languages and environments.
Visit http://www.microsoft.com/azure/windowsazure.mspx
Thursday, September 11, 2008
Creating Windows Service + Packaging into Setup
From Visual Studio
File->New Project
Select Window Service Project from the template.
Give the project name and location.
Click “Ok” will create a project as shown in the following screen shot.
Open the Service1.cs by right clicking and selecting View Code
OnStart - control the service startup
OnStart - control the service stoppage
Note:- Never put an infinite loop inside the OnStart method. If you are looking for something like Remoting or Socket programming that needs to keep listening; Start a thread on OnStart that will handle it.Here the point to be noted is that OnStart must return.
Here am going to demonstrate an Event Logging Ser
vice using Timer elapsed event
.
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
System.Timers.Timer myTimer;
string Source;
string Log;
string Event;
protected override void OnStart(string[] args)
{
Source = "Timer Service";
Log = "Application";
Event = "Timer Event";
myTimer = new System.Timers.Timer();
myTimer.Interval = 10000;
myTimer.Enabled = true;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
if (!EventLog.SourceExists(Source))
EventLog.CreateEventSource(Source, Log);
}
private void myTimer_Elapsed(object sender, EventArgs e)
{
if (EventLog.SourceExists(Source))
{
EventLog.WriteEntry(Source, Event);
EventLog.WriteEntry(Source, Event, EventLogEntryType.Warning, 1234);
}
}
protected override void OnStop()
{
}
}
Now right click Service1.cs and select View Designer.
In the designer right click and select Add Installer.
This will create two components
Now select the components and change their properties accordingly.
Using these components you can configure the authentication, name of service, service startup, etc.
Select the property page of the Project and set the Startup Object.
Compile the project.
Window service is ready.
Creating a setup for deploying windows service
Next, I will explain how you can package the service into one installer that will automate the installation process.
From Visual Studio
File->Add->New Project
From Other Project Types select Setup Project.
Give name and click OK.
It will create a project as shown in the following screen shot.
Right click TimerServiceSetup Select Add->Project Output
Which will brings the following window.
From this window select TimerService from Project and Select Primary Output and click OK.
Now right click and select View->Custom Actions.
The custom action window will be visible in the designer area.
Right Click Custom Actions and select Add Custom Actions.
This will bring a new window. Double click the Application Folder and click Ok.
Build the project.
Setup is ready.
:-)
Monday, August 11, 2008
Measuring Execution Time via Code
Inorder to measure the execution time in C# you can use the following statements
DateTime startTime = DateTime.Now;
Thread.Sleep(1000);
DateTime endTime = DateTime.Now;
TimeSpan time = endTime - startTime;
Console.WriteLine("Elapsed time {0}", time);
In java the same thing can be achieved using the following statements
long startTime = System.currentTimeMillis();
Thread.Sleep(1000);
long endTime = System.currentTimeMillis();
System.out.println("Connecting MDK method: " + (endTime - startTime)); // print execution time
Thanks,
A.M.A.L
Wednesday, July 9, 2008
Placing cursor at the end of the text in a textbox
{
var text = document.getElementById(textControlID);
if (text != null && text.value.length > 0)
{
if (text.createTextRange)
{
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
}
}
}
Scrolling Div to bottom
private void ScrollToBottom()
{
string jscript =
"objDiv = document.getElementById('div1');
objDiv.scrollTop=objDiv.scrollHeight;";
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "scrollscript", jscript, true);
}
Tuesday, July 8, 2008
Uploading and Downloading file using HttpWebRequest
Today am going to write two functions that will help to Upload & Download
files to and from another website.
public bool UploadFileEx(string fileName, string token, string url, string fileFormName, string contenttype, NameValueCollection querystring, CookieContainer cookies, byte[] fileBytes)
{
try
{
if ((fileFormName == null) ||(fileFormName.Length == 0))
{
fileFormName = "file";
}
if ((contenttype == null) ||(contenttype.Length == 0))
{
contenttype = "application/octet-stream";
}
string postdata = "";
if (querystring != null)
{
foreach (string key in querystring.Keys)
{
postdata += key + "=" + querystring.Get(key) + "&";
}
}
Uri uri = new Uri(url + postdata);
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(fileName);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
long length = postHeaderBytes.Length + fileBytes.Length + boundaryBytes.Length + 2;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
requestStream.Write(fileBytes, 0, fileBytes.Length);
boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);
string r = sr.ReadToEnd();
responce.Close();
return true;
}
catch(Exception ex)
{
return false;
}
}
Now Downloading
private bool DownloadFile(string arg)
{
try
{
string url = arg;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
int bufferSize = 1
//Initialize the output stream
Response.Clear();
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + args);
Response.AppendHeader("Content-Length", resp.ContentLength.ToString());
Response.ContentType = "application/download";
//Populate the output stream
byte[] ByteBuffer = new byte[bufferSize + 1];
MemoryStream ms = new MemoryStream(ByteBuffer, true);
Stream rs = req.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0)
{
Response.BinaryWrite(ms.ToArray());
Response.Flush();
}
//Cleanup
Response.End();
ms.Close();
ms.Dispose();
rs.Dispose();
ByteBuffer = null;
return true;
}
catch (Exception ex)
{
//File download failed;
return false;
}
}
Hope this will help you.
Thursday, July 3, 2008
Asp.Net Multiline TextBox + Default Button
I got into this interesting problem while developing a chat web client.
Basically i was trying to do some POC. Since i haven't had enough
time with me, i used a multiline textbox as the chat input element.
And the send button is set as the default button.
Since the textbox is multiline, if we press enter key,
as its behavior it will produce a newline inside the textbox.
What i want is to fire the send button event.
For that i wrote the following JavaScript.
sendText.Attributes.Add("onkeydown", "javascript: return keydownHandler('"+ sendBtn.ClientId + "')");
function keydownHandler(sendBtnId)
{
if(event.which || event.keyCode)
{
if ((event.which == 13) || (event.keyCode == 13))
{
document.getElementById(sendBtnId).click();
return false;
}
}
else
{
return true
}
}







