tag:blogger.com,1999:blog-57565176329959915062023-06-20T09:31:48.448-04:00Michael NarinskyProgrammingMichael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-5756517632995991506.post-16475410674804173552011-07-11T19:22:00.001-04:002011-07-11T19:23:42.258-04:00BlackBerry: Cursor.close() and memory optimization<p>There is a couple of things I’ve learned about BlackBerry Java development that I think might be worth sharing.</p> <h4>1. SQLite: Don’t use Cursor.close() – use Statement.close() instead.</h4> <h4><font style="font-weight: normal">It turned out that Cursor.close() method is also closing the Statement that was used to create that Cursor, even if the Statement was later reinitialized. I believe that removing Cursor.close() calls from the code helped me to avoid a couple of ‘File System Error (12)’ errors while doing some IO operations over the SQLite db file. I tend to think that ‘File System Error (12)’ error actually means ‘File is locked’ (thank you BlackBerry, btw, for such nice, self-explanatory error messages and providing no documentation about them), so now it all makes sense to me.</font></h4> <p>More info: <a href="http://supportforums.blackberry.com/t5/Java-Development/Cursor-close-closes-the-Statement-used-to-Create-It/ta-p/614699">http://supportforums.blackberry.com/t5/Java-Development/Cursor-close-closes-the-Statement-used-to-Create-It/ta-p/614699</a></p> <h4>2. Memory optimization</h4> <h4><font style="font-weight: normal">There is a nice blog post that provides an overview of RIM’s memory monitoring tools (Memory Statistics, Object Statistics and Objects) and gives a set of steps that should be taken in order to detect a memory leak and identify the root cause. I’ve seen this article before, but this time I’ve actually read it thoroughly (all three parts) and I think it’s very good place to start if you do a memory optimization:</font></h4> <h4><a href="http://devblog.blackberry.com/2009/10/how-to-find-that-memory-leak-part-one/"><font style="font-weight: normal">http://devblog.blackberry.com/2009/10/how-to-find-that-memory-leak-part-one/</font></a></h4> <h4><a href="http://devblog.blackberry.com/2010/01/how-to-find-that-memory-leak-part-two-detecting-the-leak/"><font style="font-weight: normal">http://devblog.blackberry.com/2010/01/how-to-find-that-memory-leak-part-two-detecting-the-leak/</font></a><font style="font-weight: normal"> (you might need to tweak images’ source URLs in order to get them for this part)</font></h4> <h4><a href="http://devblog.blackberry.com/2010/03/how-to-find-that-memory-leak-part-three/"><font style="font-weight: normal">http://devblog.blackberry.com/2010/03/how-to-find-that-memory-leak-part-three/</font></a></h4> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com0tag:blogger.com,1999:blog-5756517632995991506.post-6062134683513541332011-03-27T15:43:00.001-04:002011-08-10T15:54:48.967-04:00BlackBerry: Sending an HTTP request in a separate thread<p>Sending an HTTP request in a separate thread is a very common operation for almost every client-server web application. The idea is to spin-off a background thread that will send a request and process the response data, but won’t block the main (GUI) thread on the same time.  At the end of its execution the background thread should use some kind of a callback mechanism to pass response results back to the main thread. Since this operation is very common I was expecting that pretty much every modern framework should have a lot of tutorials available online describing how to implement this functionality in a quick and easy way. However this was not the case for the BlackBerry Java API -  after a couple of hours researching I still didn’t find anything that was ‘quick and easy’…</p> <p>Finally, looking through RIM’s API docs I found a method <a href="http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/Application.html#invokeLater(java.lang.Runnable)" target="_blank">Application.invokeLater()</a> which can take a runnable object defined in a background thread and start it in the main thread. This method allowed me to pass execution control back to the main thread.</p> <p>Here is a sample code:</p> <p>First let’s create a ConnectionThread class that can take a URL (to send a request to) and call a method of some screen once the response is received (for the sake of simplicity we’ll be calling a method of the screen that is currently on top of the screen’s stack).</p> <pre class="brush: java; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">public class ConnectionThread extends Thread {
String URL;
public ConnectionThread(String URL) {
this.URL = URL; //URL to send a request to
}
public void run() {
//optional: show some popup "Please wait screen"
final Screen dialogScreen = new WaitPopupScreen(); //wait popup screen extends RIM's PopupScreen class
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushModalScreen(dialogScreen);
}
});
//send HTTP request and save the response
HttpConnection connection = null;
//use API 5.0 Connection factory class to get first available connection
byte responseData[] = null;
try {
connection = (HttpConnection) new ConnectionFactory().getConnection(URL).getConnection();
int len = (int) connection.getLength();
responseData = new byte[len];
DataInputStream dis;
dis = new DataInputStream(connection.openInputStream());
dis.readFully(responseData);
} catch (Exception e) {
// TODO Handle exception
}
final byte[] responseDataToProcess = responseData;
//use invokeLater method to pass results back to the main thread
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(dialogScreen); //hide wait popup screen
//pass results to the callback mathod of the current screen
((MyScreen)UiApplication.getUiApplication().getActiveScreen()).callBackMethod(responseDataToProcess);
}
});
}
}</pre>
This is the actual screen where we spin the connection thread off:
<pre class="brush: java; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">public class MyScreen extends MainScreen {
public MyScreen() {
super();
// add a button that will initiate an HTTP request
ButtonField requestButton = new ButtonField("Send HTTP Request");
requestButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//start connectionThread on a button click
new ConnectionThread("http://mnarinsky.com").start();
}
});
add(requestButton);
}
//this method will be called from the connection thread
public void callBackMethod(byte[] responseData){
//process response
}
}</pre>
Finally this is a code for an optional 'Please wait' popup screen – displays a simple “Please wait..” text. If you want you can make it little more fancy by using <a href="http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/How_To_-_Display_an_animated_GIF.html?nodeid=1405903&vernum=0" target="_blank">some animated GIF image.</a>    <pre class="brush: java; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">public class WaitPopupScreen extends PopupScreen {
public WaitPopupScreen() {
super(new VerticalFieldManager());
LabelField labelField = new LabelField("Please wait...",
Field.FIELD_HCENTER);
add(labelField);
}
}</pre> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com30tag:blogger.com,1999:blog-5756517632995991506.post-59378481738770503552011-03-13T21:13:00.001-04:002011-03-13T21:14:05.727-04:00Microsoft Certification<p><a href="http://lh6.ggpht.com/_V2c2YNu_CKo/TX1rnv0SN8I/AAAAAAAAvWc/X6WdJZgSca4/s1600-h/mcts-logo%5B2%5D.gif"><img style="background-image: none; border-right-width: 0px; margin: 10px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="mcts-logo" border="0" alt="mcts-logo" src="http://lh5.ggpht.com/_V2c2YNu_CKo/TX1rn3YgloI/AAAAAAAAvWg/L-giBXyXopU/mcts-logo_thumb.gif?imgmax=800" width="244" height="183" /></a></p> <p>Last month I have passed <a href="http://www.microsoft.com/learning/en/us/exam.aspx?ID=70-515" target="_blank">Microsoft Exam 70-515</a> (Web Applications Development with .NET Framework 4.0) and thus finally completed my long term project of getting a Microsoft .NET Developer Certification.</p> <p>It all started in October 2009 – back then I’ve decided to pass MS Exam# 70-536 (.NET Application Development Foundation), which was a pre-requirement for all Microsoft development certifications. I’ve got a preparation book, studied it thoroughly and was ready for an exam around March 2010 - just a month before .NET 4.0 came out and Microsoft has announced a complete new set of exams targeting the updated framework. New exams became available on July 2010, but no preparation materials were released until December 2010. Around Christmas 2010 I finally got a new preparation book, spent another 2 months preparing and, voila, passed it in February 2011 – just 14 month after the start :)</p> <p>Anyway, glad that this project is finally over – time to move on to the next one…</p> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com1tag:blogger.com,1999:blog-5756517632995991506.post-45229715594446879852010-07-06T16:51:00.001-04:002010-07-06T17:12:50.570-04:00Customizing ASP.NET AJAX Control Toolkit AsyncFileUpload look<p>The standard HTML <input type=”file”> element does not work inside ASP.NET update panel due to security reasons and restrictions a browser implies (It is not allowed for JavaScript to directly access files in a user's system and it is not allowed to access the details of the selected file). <a href="http://www.asp.net/ajax/AjaxControlToolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx" target="_blank">ASP.NET AJAX Toolkit AsyncFileUpload control</a> seems to be an easy way to solve this issue – it works out-of-box (well, after you add AjaxControlToolkit binaries to your project) and provides an excepted functionality to the end-user.</p> <p>One problem I faced during my work with AsyncFileUpload was inability to customize the look of the control. This is a pretty common issue with a <input type=”file”> element in general and a pretty neat solution for this is described <a href="http://www.quirksmode.org/dom/inputfile.html" target="_blank">here</a>. The basic idea of this solution is that you place <input type=”file”> element on top of another custom element (picture, button, etc.) and then set <input type=”file”> element’s opacity to 0, so it will be transparent and the user will only see the element underneath (picture, button, etc.), but clicking on that element will causes <input type=”file”> to fire off it’s click event. </p> <p>This technique seems to work fine when working with regular <input type=”file”>, however AsyncFileUpload control does not allow to set its opacity…</p> <p>So in order to solve this let’s take a look at how AsyncFileUpload control is being rendered.</p> <p>Turns out that this code:</p> <pre class="brush: xml; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><div id="fileUploadDiv">
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" />
</div></pre>
<p>will be rendered as:</p>
<pre class="brush: xml; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><div id="fileUploadDiv">
<span id="MainContent_AsyncFileUpload1">
<input type="hidden" name="ctl00$MainContent$AsyncFileUpload1$ctl00" id="MainContent_AsyncFileUpload1_ctl00" />
<div id="MainContent_AsyncFileUpload1_ctl01" name="MainContent_AsyncFileUpload1_ctl01">
<input name="ctl00$MainContent$AsyncFileUpload1$ctl02" type="file" id="MainContent_AsyncFileUpload1_ctl02" id="MainContent_AsyncFileUpload1_ctl02" onkeydown="return false;" onkeypress="return false;" onmousedown="return false;" style="width:355px;" />
</div>
</span>
</div> </pre>
<p>This means that using a CSS selector <strong>#fileUploadDiv input[type=file]</strong> you can modify the style (including opacity) of the AsyncFileUpload control the same way you do it with the regular <input type=”file”> control therefore you can still use <a href="http://www.quirksmode.org/dom/inputfile.html" target="_blank">this nice solution I mentioned before</a> to give AsyncFileUpload control a custom look you want.</p> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com4tag:blogger.com,1999:blog-5756517632995991506.post-61946723532877895792010-06-16T15:19:00.001-04:002010-06-18T13:30:25.487-04:00ASP.NET 4.0 Chart Control breaks IIS 7.0<p>I’ve decided to add the new ASP.NET 4.0 Chart Control to one of my web apps. Everything worked fine during the testing on my local machine, but once I published it to remote IIS 7.0 server I’ve got the following error:</p> <blockquote> <p><font color="#ff0000"><strong>HTTP Error 500.23 - Internal Server Error</strong> <br />An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.</font></p> </blockquote> <p>Turns out that it occurs <a href="http://mvolo.com/blogs/serverside/archive/2007/12/08/IIS-7.0-Breaking-Changes-ASP.NET-2.0-applications-Integrated-mode.aspx" target="_blank">because ASP.NET modules and handlers should be specified in the IIS <handlers> and <modules> configuration sections in Integrated mode.</a></p> <p>So the simple solution that worked for me was just to remove the following entry from web.config <system.web> section:</p> <pre class="brush: xml; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><httpHandlers>
<add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35"
validate="false" />
</httpHandlers></pre>
<p>Also make sure that <system.webServer> section has the following:</p>
<pre class="brush: xml; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><handlers>
<remove name="ChartImageHandler" />
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST"
path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers></pre> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com15tag:blogger.com,1999:blog-5756517632995991506.post-32165792637441503322009-11-11T18:17:00.002-05:002009-12-07T16:57:44.504-05:00Creating a .cab file with C#<p>.NET does not have any classes for creating cabinet files out-of-box, so today I had to look what else is available out there. There are a couple of solutions I found for creating a .cab file from the .NET code. The one that actually worked for me was a library developed by Microsoft(!) as part of their <a href="http://wix.sourceforge.net/" target="_blank">Windows Installer XML (WiX) toolset</a>. This toolset is distributed with a bunch of dlls as part of its SDK. The library that allows to create cabinet files is called <strong>Microsoft.Deployment.Compression.Cab </strong>and located under <WIX_Installation_Folder>\SDK.</p> <p>In your project you need to add a reference to the <strong>Microsoft.Deployment.Compression.Cab.</strong>dll and to <strong>Microsoft.Deployment.Compression.</strong>dll (located under the same folder and has some base classes for types defined in the Microsoft.Deployment.Compression.Cab).</p> <p>After doing this you can add files to a cabinet file from .NET with just a couple of line of code:</p> <pre class="brush: csharp; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">using Microsoft.Deployment.Compression.Cab;</pre>
<pre class="brush: csharp; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">//create a instance of Microsoft.Deployment.Compression.Cab.CabInfo
//which provides file-based operations on the cabinet file
CabInfo cab = new CabInfo(@"C:\Cabinet1.cab");
//create a list with files and add them to a cab file
List<string> filesToArchive = new List<string>() { @"C:\file1", @"C:\file2" };
cab.PackFiles(null, filesToArchive, null);
//add a folder (including subdirectories) to another cab file with a maximum compression level
cab = new CabInfo(@"C:\Cabinet2.cab");
cab.Pack(@"C:\folder", true, Microsoft.Deployment.Compression.CompressionLevel.Max, null);
//unpack a cab file into C:\Unpacked folder
cab.Unpack(@"C:\Unpacked");</pre>
<p>The library has an MSDN style help file located under <WIX_Installation_Folder>\doc\DTFAPI.chm. </p>
<p>Another dll that also comes with WiX toolset SDK is <strong>Microsoft.Deployment.Compression.Zip</strong>.dll that provides similar functionality for packing and unpacking zip files.</p> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com3tag:blogger.com,1999:blog-5756517632995991506.post-85146021161480786062009-09-14T11:40:00.001-04:002009-09-14T11:40:00.690-04:00Catching local SOAP traffic with Fiddler<p><a href="http://www.fiddler2.com/">Fiddler</a> is a great tool for testing and debugging web services. It logs and analyze all HTTP traffic between your machine and the Internet. I think it could be extremely beneficial to see how data is actually being serialized and what exactly is being sent to/from a web-service or application. </p> <p>When it comes to catching a local traffic you have to do some <a href="http://www.fiddler2.com/Fiddler/help/hookup.asp#Q-LocalTraffic">tweaks</a>, since .NET tries to avoid any proxies when it sends local requests. I had no problem catching local REST traffic by simply changing web-service address from <em>http://localhost:[port]/…</em> to <em>http://ipv4.fiddler:[port]/…</em> However when I tried to see a local SOAP traffic this simple address change did not bring any result and the SOAP messages were still not logged.</p> <p>I had to spend some time and try multiple ways to fix this issue, starting from modifying a web.config to explicitly <a href="http://dotnetcentral.net/weblog/posts/277966.aspx">specify a proxy</a> and ending with modifying a localhost value in /etc/hosts. Here are the steps that actually worked for me:</p> <p>1. Modified client’s web-service address from “<em>localhost”</em> to “<em>localhost</em><font size="6"><em><strong>.</strong></em><font size="3">”<strong> </strong></font></font>(I think it is better then changing it to the actual machine name (makes client configuration machine-dependent) and definitely better than <em>ipv4.fiddler </em>(cannot work without fiddler proxy turned on)).</p> <p>2. Turned off listening on to the IPv6 adapter: Tools => Fiddler Options… => General tab => Enable IPv6 (if available) [uncheck]:<a href="http://lh4.ggpht.com/_V2c2YNu_CKo/Sq5jyAKZY1I/AAAAAAAAK7I/f_fxUdm3ThM/s1600-h/image%5B7%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_V2c2YNu_CKo/Sq5jzoJz6sI/AAAAAAAAK7M/lX-U4-x2AeE/image_thumb%5B3%5D.png?imgmax=800" width="416" height="301" /></a> </p> <p></p> <p>These two steps allowed me to successfully see HTTP traffic to/from a web service hosted on a local ASP.NET development server (Cassini).</p> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com1tag:blogger.com,1999:blog-5756517632995991506.post-48277294434589952162009-07-21T13:13:00.001-04:002009-07-27T10:12:10.273-04:00Dynamically changing HTML form action using jQuery<p>In order to test a RESTful service I’m currently working on I’ve created a simple HTML page with a bunch of forms. These forms generate various POST/GET requests to my REST service API methods, so I can manually test the service during a development process. One issue I faced was that the action attribute of HTML form element is static while I need to modify it dynamically based on user’s input (i.e. so I can send a GET requests to a URL like http://www.myrestfulservice.com/item/{itemID} where itemID is entered to the form by a user). JavaScript comes to mind. </p> <p>So here is how this could easily be done with jQuery:</p> <pre class="brush: xml; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$("#getForm :submit").click(function() {
var item = $(":input#itemId").val();
$("#getForm").attr("action", "http://www.myrestfulservice.com/" + item);
});
});
</script>
</head>
<body>
<p>
Enter item ID get:
<input type="text" size="50" id="itemId" />
</p>
<form id="getForm" method="get" action="">
<input type="submit" value="Get Item" />
</form>
</body>
</html></pre> Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com0tag:blogger.com,1999:blog-5756517632995991506.post-35523606306018565812009-07-17T11:27:00.008-04:002009-11-12T09:40:50.962-05:00Getting Database column length with LINQ to Entities<p>During the work with ADO.Net Entity Framework sometimes you need to know a metadata for a database you’re working with such as a nchar/nvarchar column max length. Trying to update a DB column using LINQ to Entities with a string greater than the column max length leads to an SqlException (e.g. System.Data.SqlClient.SqlException: String or binary data would be truncated); therefore you need to check that your data does not exceed DB column boundaries manually. LINQ won't do this for you on a run-time.</p> <p>If you don't want to have all your Entity Model metadata hardcoded, you need to get it on a run-time. <a href="http://www.codeproject.com/KB/cs/LinqColumnAttributeTricks.aspx">Here</a> is described how you can do this with LINQ to SQL. </p> <p>Unfortunately this approach seems not to work for Entities Model. In order to do this for Entities you would need to query MetadataWorkSpace class and get a conceptual (CSpace) metadata for your Entity: </p> <pre class="brush: csharp; auto-links: false; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">public static int GetMaxLength(Type entityType, string columnName)
{
int result = 0;
using (myEntities context = new myEntities())
{
var queryResult = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace) //getting conceptual entity metadata
where meta.BuiltInTypeKind == BuiltInTypeKind.EntityType
&& (meta as EntityType).Name == entityType.Name //selecting desired entity by name
from property in (meta as EntityType).Properties
where property.Name == columnName //selecting an entity property that matches the column name
&& (property.TypeUsage.EdmType.Name == "String")
select property.TypeUsage.Facets["MaxLength"].Value;
if (queryResult.Count() > 0)
{
result = Convert.ToInt32(queryResult.First());
}
}
return result;
}</pre>
<p></p>
<p>When you call this method, just pass a type of your entity e.g.</p>
<div id="codeSnippetWrapper"></div>
<pre class="brush: csharp; auto-links: false; collapse: false; first-line: 1; gutter: false; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">GetMaxLength(typeof(myEntity), "myColumnName");</pre>Michael Narinskyhttp://www.blogger.com/profile/10887345517296376127noreply@blogger.com2