My name is Juri Strumpflohner and this is my technical blog. I'm a software architect, .Net, Android, Web and Java dev, TDD and best practices promoter and martial arts practitioner.

Recent Posts Subscribe

Dear reader of Juri's TechBlog,
I moved my blog to a new domain and a new hosting solution as well. I'm now blogging on juristr.com.

RT: Testing with VS2010 - A Bugs Life

I'm surely one who loves automated tests. Having them in place (with appropriate code coverage of course) and seeing the green bar or green check icons (depending on your IDE) just gives you the necessary peace for making changes to your application and for applying concepts like refactoring which otherwise is just as if you'd drive blindly (it may work, but it very much depends on your luck). And not just that, in my opinion writing unit tests makes you produce better, more loosely coupled code, 'cause otherwise testing would just be a nightmare. And this pushes you towards using best practices approaches like dependency injection, layering and so on. The only problem is that it is sometimes hard to motivate people to jump on board since they do not immediately recognize the value of automated testing. :(

I'm actually coming from the jUnit world but while diving recently into .Net development I got in touch with MS Tests, concepts like data-driven tests and the whole test-integration into MS's Visual Studio IDE. And I have to admit that I really like Microsoft's recent efforts to strive for best practices like MVC (or MVVM in WPF) and Unit testing.

Here I post a very nice presentation from a former work mate and colleague, Peter, hold on a user group. It's about the awesome integration of testing into the new VS2010 IDE. Enjoy.

HowTo: Include JavaScript file from JavaScript code

Recently a colleague asked me on how to add a JavaScript file reference to an HTML document from within JavaScript code. It's actually quite easy. All you need is to create the appropriate element and attach it to the HTML document's dom structure on the appropriate position, i.e. the head element. The following JavaScript does the job.

function loadScript(src) {
   var script = document.createElement("script");
   script.type = "text/javascript";
   document.getElementsByTagName("head")[0].appendChild(script);
   script.src = src;
}
And then call it from within your JavaScript code with
loadScript('http://someurl.com/test.js');

Similarly you may want to attach a JavaScript function programmatically to the HTML body's onLoad event. This can be achieved with the following function
function addLoadEvent(func) {
   var old = window.onload;
   if (typeof window.onload != 'function') {
      window.onload = func;
   } else {
      window.onload = function() {
         old();
         func();
      }
   }
}
And then by calling it similarly as in the loadScript function
addLoadEvent(<someJavaScriptFunction>);

Implementing the onTouchEvent for the MapActivity

Android View classes expose an onTouchEvent(MotionEvent ev) method. As the name already suggests, by overriding this method you can handle touch events done by the user on the current view.

@Override public boolean onTouchEvent(MotionEvent event) {
   int action = event.getAction();
   switch (action) { 
      case (MotionEvent.ACTION_DOWN) : // Touch screen pressed
         break; 
      case (MotionEvent.ACTION_UP) : // Touch screen touch ended
         break; 
      case (MotionEvent.ACTION_MOVE) : // Contact has moved across screen
         break; 
      case (MotionEvent.ACTION_CANCEL) : // Touch event cancelled
         break;
   } 
   return super.onTouchEvent(event);
Now when you have a MapActivity you're tempted to do the same, however this won't work. The handler isn't being executed. I didn't find the exact reason for this behavior yet. Probably the reason is that the MapActivity doesn't automatically forward the event to the registered MapView or it just doesn't get notified about the event, since the motion event actually occurs on the MapView itself and not its parent, the MapActivity.

What can be done instead is to either register the event directly on the MapView
mapView.setOnTouchListener(new OnTouchListener() {
  
 public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
  return false;
 }
});
..or to override the MapActivity's dispatchTouchEvent(MotionEvent). What has to be considered however is to appropriately forward the event in this case. For overriding event handlers, the Android API suggests to
Returns: True if the event was handled, false otherwise.
Now if you catch some touch event and you do your according processing, then you have to be aware that if you return true, the onTouch event won't be propagated, with the side-effect that the user won't be able to move on the map (or better "move the map on the screen"). Therefore, while in other cases you would return true after handling the event, in this specific case of the MapActivity you may consider not returning (or returning false) and just propagating the event by invoking super.dispatchTouchEvent(...).

Visual Studio intellisense not working properly

Recently a work mate pointed me out that my Visual Studio Intellisense seemed to not work correctly. When writing...

this.Page.GetPostBackClientEvent(
...the info with the remaining parameters should appear automatically which was not the case on my machine. I actually hadn't noticed that before since you can just press CTRL+SHIFT+SPACE within the method call and you will get the parameter info.

Nevertheless I tried to figure out the reason and I was already fearing that some 3rd-party VS add-in may have broken my Intellisense and that I'd have to invoke "devenv.exe /resetuserdata" (which however is a pain). Then I found this setting inside the VS options dialog:


From the "grayed-out" checkbox I assume that some configuration was really damaged since otherwise it should either be in the disabled or enabled state. Anyway, ticking the checkbox in again solved my issue :)

HowTo: Get the selected list index on Android Activity from context menu event

Consider the situation where you have an Activity displaying a list of items. You have a context menu and a normal option menu. When pressing the option menu button, you can get the selected item and index as follows:

//the selected index
int index = listView.getSelectedItemPosition();

//selected item
Object selObj = listView.getSelectedItem();
It is as simple as that. If you registered a context menu on the list, then the situation changes slightly and the "getSelectedItemPosition()" will fail. Instead, you have to do something like this
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int index = menuInfo.position;

HowTo: Run IIS 7 on Vista 64bit OS in 32bit mode

A couple of days ago, I ran into a nice problem. I had to configure the setup of a new project using an old ASP based application as the entry point from which then the new ASP.net application will be started. After a couple of strange error messages I realized that my web application running on the local IIS wasn't able to locate the 32bit driver since I'm having a 64bit Vista installation on my machine. Consequently also the IIS application pool was running in 64bit mode.

After a quick exchange with one of our technicians, he sent me the solution on how to run the IIS in 32bit mode. Here just as a note to myself (and any other potentially interested) on how to downgrade the 64bit IIS to 32bit:

  1. Open IIS
  2. Click Application Pools
  3. Right click DefaultAppPool. Choose "Advanced Settings..."
  4. A properties window will open, having on the upper "General" section a field named "Enable 32-Bit Applications". Set that to true.
  5. Restart your web application and it should work.
 Note this works just on IIS 7 (as far as I know).

ASP.net Ajax: Consuming webservice from client-side JavaScript

Yesterday I answered a question on SO related to the problem of consuming an ASP.net webservice from JavaScript on the client-side. So I thought it might be a good idea to put it down in a downloadable example that can be used as a starting point for all those that didn't yet use this method.

What I created is basically a simple example that sends a string to a classic asmx webservice which in turn answers again with a string, which is then displayed on the client-side. A simple greeting service basically :) .
The second example is more complex (although ASP.net makes it really easy to handle) in the sense that I'm sending back a whole object graph: a Person object containing another Location object. For programming such interactions I strongly recommend to use Firebug. It allows you to inspect the messages that are sent back and forth between the client and server back and you can place breakpoints in your JavaScript, debug it and inspect your variables.

I not going into too much detail now since I don't have the time and moreover I think the example speaks for itself. I just want to mention some important parts regarding the WCF service.

namespace AjaxWebservices.Services
{
    [ServiceContract(Namespace = "AjaxWebservices.Services")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PersonService
    {
          ...
    }
}
If you see such a service definition and you have already worked with the somewhat more traditional asmx services, then you'd call this from your client JavaScript like
function sendToServer(){
   AjaxWebservices.Services.PersonService.<method-to-invoke>(<parameters>,onSuccess, onFailure);
}
...
You invoke it with the namespace of the service. But having a WCF service, the namespace definition defined in the ServiceContract (see above) will be the one that actually holds and not the class namespace. In this example above I configured them to have the same name, just for simplicity.
For the rest take a look at the example.


Some of you may also ask why not to use the famous UpdatePanel control. Well, generally you may use it without any problems and have instant Ajax functionality without even modifying a line of code of your traditional app. And that's the point. My personal opinion is just use it for such purposes, i.e. to enhance traditional apps and also in such cases I would be very careful. The major reason is performance. During an asynchronous request to the server, the UpdatePanel will send the whole ViewState of your entire page back to the server. This is needed s.t. on the server-side the whole life-cycle can be completed just as during a normal request. Moreover then the whole rendered HTML will be send back for exchanging it. If you just have a small page, this may not be an issue for you. I had however already experiences, where on a large page an asynchronous paging of a list took on an 56Kbit line about 50-60 seconds, while my optimized, hand-written JavaScript-Webservice solution was able to do it in about 3 - 4 seconds. This is because these requests are encoded in JSON and really small and you don't have the ViewState which can be quite big.

Updated Home: Stackoverflow iGoogle Gadgets

Today my Stackoverflow iGoogle gadgets stopped working. I couldn't identify the problem. I just got the error

Unable to retrieve spec for http://files.getdropbox.com/u/10936/gadgetHosting/stackoverflowProfile/prod/stackoverflow.xml. HTTP error 502
as probably all of my users.

Most probably the error is due to the hosting on Dropbox. Since Dropbox includes file versioning it was quite handy to just place new versions inside the public directory for having them instantly published on the iGoogle homepage and made available to all of the users. Today however I payed for that simplicity. So I decided to move all of the gadgets to a more reliable host.

Therefore I kindly ask you out there (using my gadgets) to just go to my homepage and update the references of your gadgets, basically just re-adding them by clicking the "iGoogle button". In this way you'll get them to point to their new home.

Here's the link for the updated Gadgets:
http://sites.google.com/a/js-development.com/projects/google-gadgets

I'll also update the iGoogle directory, however that may take some time.