Skip to main content

Posts

The Cornball goes to Brunch with Chaplin

Lately I've been working pretty hard on different projects but not really stumbling upon anything blogworthy. The most recent project is quite interesting though, a single page, touch friendly, web application using the latest and greatest technologies. We've ended up with using Brunch with Chaplin , which is a very neat way of setting up a Backbone based single page web project with Brunch and Chaplin . Aside from this, I have my own little project that has lived on for almost 15 years already, The Cornball . From being a plain Windows application written i C an Win32 API, it has been ported to .NET using WPF, and is currently a Silverlight application hosted on Windows Azure. I could not find a better time to reanimate this project and create a new web based version, touch friendly, super optimized, awesome in any way. So I did... So please follow my journey at Github . It's going to take a while, I assure you, but I already have some ground work done. Meanwhile,...

jQuery file upload with Bootstrap progress bar

Performing an asynchronous file upload from the browser is a common problem with almost as many solutions as there are developers. The following solution is the best fit for my needs, and also works well with most popular browsers. Backwards compatibility is not an issue in this case which is great, because I can use the new technologies as they are supposed to be used. Everything is put together in JSFiddle for a working example . I will continue to explain the parts below... First, the input field needs to be styled as a button. The form tag is only present for us to be able to reset the file input field later on. <form>     <span class="fileUpload btn btn-default">         <span class="glyphicon glyphicon-upload"></span> Upload file         <input type="file" id="uploadFile" />     </span> </form> .fileUpload { position: relative; overflow: hidden; } .fileUpload inpu...

Using ASP.NET MVC with MEF

I wrote this post almost a year ago, but never published it for some reason. Anyway, here is a little MVC/MEF magic. By default a controller in MVC must have a parameterless constructor. When using MEF a good practice is to inject the services via constructor parameters. These two in combination obviously creates an issue where the following scenario will not work out of the box, since there is no parameterless constructor for  MVC  to use. Note that the PartCreationPolicy is set to NonShared since a new controller have to be initialized for each request. [Export] [PartCreationPolicy(CreationPolicy.NonShared)] public class HomeController : Controller {     private readonly IServiceClient _service;     [ImportingConstructor]     public HomeController(IServiceClient service)     {         _service = service;     }     public ActionResult Index()     {         ...

Bindable RichTextBox with HTML conversion in WPF

In WPF , the RichTextBox  control is not really like other controls. Due to its flexible nature, there is no built in way of binding a property to the content. In this case, I wanted a simple  RichTextBox  control with a binding to an HTML formatted string to be able to use the built-in formatting features of the  RichTextBox  and allow users to create simple HTML formatted content. First, doing the conversion on-the-fly proved to have major performance issues, so I ended up binding the content to a XAML string. The XAML to HTML conversion can be performed at any time. I created a UserControl with a bindable Text-property. The view contains a  RichTextBox  control. <RichTextBox x:Name="richTextBox" TextChanged="OnRichTextBoxChanged"> The source code for the user control contains the Text property and the methods to handle the binding. public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Te...

Using Bootstrap Tooltip to show Parsley validation errors

I'm currently working on a web application using a variety of different frameworks, such as Backbone for the back-end, Bootstrap for the front-end and Parsley for client side form validation.  Parsley is a really powerful validation toolkit, but it takes some tweaking to make it blend with the Bootstrap front-end. Fortunately this is a one time fix, which can be re-used all over our project. Since there will be some custom options in our  Parsley  object, we can't use the default parsley-validate attribute on the form. Instead we have to initialize the validation with the jQuery syntax: $('#my-form').parsley(parsleyOptions); The options are were the magic happens, and in our case we have a global options object that our forms use to get the same experience all over the application. Here's what it looks like: var parsleyOptions = {  // Sets success and error class to Bootstrap class names  successClass: 'has-success',  errorClass: 'has-er...

Proper initialize/dispose with WCF and MEF

Initialization and disposing of services in a WCF application is a bit of a hassle to control when running on an IIS instance, since the application starts and stops every now and then based on server requests. It doesnt make it easier when using an IOC, as in our case MEF. To have total control of the startup and shutdown events we began with adding a service factory to all our WCF services. <%@ ServiceHost Language="C#" Debug="true" Service="WcfService" Factory="CustomServiceHostFactory" %> When ever the application gets woke up by a request to any of the services, the service factory will be used to instantiate the service. This means we can add any logic to these methods to have full control of the WCF application initialization. Here is what a service factory class could look like. (Composition is a static class containing a reference to the MEF container) public class CustomServiceHostFactory : ServiceHostFactory { priva...

Custom DataAnnotation validation on multiple properties

There are a few cases where you want to be able to validate a property value based on the value of another property. As always there are a bunch of different solutions to this problem, but the cleanest way of doing so is to create a custom DataAnnotaion validation attribute. It is as easy as creating a class derived from ValidationAttribute and overriding the IsValid-method, but instead of the usual IsValid(object value) we will be overriding the IsValid(object value, ValidationContext validationContext) method. The ValidationContext contains information about the current object beeing validated and makes it possible to get the values of related properties. By including the related properties and values in our validation class constructor. All in all, it could look like this, even though the validation logic does not really make sense in a real world context it explains the idea pretty good. [AttributeUsage(AttributeTargets.Property)] public class CustomValidationAttribute : Val...

Binding Enum with DescriptionAttribute in WPF

Binding an enumeration to a ComboBox can be done in several ways. In most cases you don't want to display the value itself, but a more user friendly description. One common approach is to use the DescriptionAttribute on the Enum values to supply a description for each value.  This is all possible in a very MVVM friendly way. First step is to add the  DescriptionAttribute  to the values of the enumeration. public enum MyValues { [Description("First value")] First, [Description("Second value")] Second } To retrieve the description from the enum we use a simple extension method. This method returns the value of the DescriptionAttribute if it exists, otherwise the string representation of the enum value is returned. public static string GetDescription(this Enum value) { var fieldInfo = value.GetType().GetField(value.ToString()); var attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as ...

LEAP Sweden in Redmond

Well, there is not very much to say about the trip to Redmond, except that it was awesome! It has been a week filled with seminars and social activities in Microsoft Conference Center and closeby venues. We even paid a visit to The Flight Museum outside Seattle and actually got a private tour from Clemens Vasters, who knew surprisingly much about airplanes. Not only have I been able to meet other extremely talented developers and architects who participated in the LEAP program, I've also had the chance to meet some of the most involved people at Microsoft and got to hear about the latest and hottest topics in IT today. The speakers included Jonathan de Halleux , Stuart Kwan, Scott Guthrie , Miha Kralj , Clemens Vasters ,  Cihan Biyikoglu ,  Bryan Agnetta and more. The topics were everything from theoretical architectural lectures to deep product previews and technical discussions. Overall it was a great schedule packed with so much information I almost don't know wha...

LEAP Sweden - Windows 8 and Windows Server 2012

I am back home in the couch filled with enthusiasm from the last gathering of LEAP Sweden before the trip to Redmond in the end of May. Todays topic was the new versions of Windows (server and client) as well as the Business Intelligence product suite from Microsoft. The Business Intelligence suite basically is a really nice integration between products that already exist. With SQL Server 2012 , SharePoint 2010 and Excel 2010 tightly integrated Microsoft offers a really attractive package. Mikael Nyström showed off some of the new features in Windows Server 2012 . To say the least this is an impressive release. There is a heavy focus on virtualization, storage and terminal services which makes Microsoft a strong competitor against both VMWare and Citrix in the small to medium sized business segment. One of the coolest features has to be conditional permissions. Johan Lindfors gave a demo on Windows 8 development . He showed some samples of how easy it is to develop Met...

Connecting to SOAP service with certificate authentication

I have rarely worked with certificate based authentication, so when I got this neat little SOAP service with certificate authentication in my hands I gladly accepted the challenge. All I got was the URL to the service with a certificate and the password. I also got an example written in PHP: $client = new SoapClient("https://url.to.service/", array(         'local_cert' => 'client_certificate.pem', 'passphrase' => 'PASSPHRASE',         'cache_wsdl' => WSDL_CACHE_NONE )); This of course looks very easy, but we all know that it's not really that easy if I want to use .NET and WCF. First of all the PEM-certificate cannot natively be used in .NET. Second of all I don't have the WSDL for the service. Here are the steps I had to go through to be able to successfully connect to the SOAP service. Convert the certificate The certificate I received was a PEM certificate which included both the private and public key. .NET ...

Quartz.NET and MEF

I have been implementing a scheduler service for several different jobs on several difference schedules, which led me into using Quartz.NET . This is a really nice framework, but since we're using MEF I ran into some issues. Quartz.NET  basically consists of the scheduler engine which runs jobs implementing the IJob interface. The interface simply consists of an Execute method. I export each job with the IJob interface using  MEF . [Export(typeof(IJob))] public class MyJob : IJob { public void Execute(JobExecutionContext context) { ... } } In my scheduler implementation the jobs are imported into an IEnumerable<IJob>. [ImportMany(typeof(IJob))] public IEnumerable<IJob> Jobs { get; set; } The initialization of the scheduled tasks is pretty straight forward. A standard scheduler factory is initialized which in turn gives us a scheduler instance. Each job that was imported by  MEF  is then added to the scheduler, here ...

LEAP Sweden - Identity & Access Management

Today was the third day of the Swedish Lead Enterprise Architect Program at Microsoft in Kista. It has been a day filled with sessions on security and identity with a kind of unavoidable focus on federated identities in the cloud. Overall it has been an awesome day with seminars from Henrik Nilsson , Barry O'Reilly and Sergio Molero among others. A lot of Microsoft technologies was mentioned, Forefront Identity Manager , Active Directory Federation Services 2.0 , Forefront Unified Access Gateway , Direct Access ,  Windows Identity Framework and of course  Azure Access Control Service . Microsoft really has a great suite of applications regarding Identity & Access Management. I just wish there was a project or two where I could utilize all of these amazing technologies. I have had a plan to add a login feature to the Cornball using federated identities, so that might happen in the near future. Besides, Björn Eriksen provided a tip for all of us thursty for e...

First gathering of LEAP Sweden 2012

I am back home in the couch after the second day on the first gathering of Microsoft LEAP Sweden 2012 . It has been two days packed with lectures on architecture and cloud computing. Without giving away too much, here are some short notes from this first gathering: In software arcitecture, knowing your patterns and practices is always a good thing. The same goes for best practices. SOLID is not a new concept, but it bears repeating! The future is all about bring-you-own-device , the cloud in general and  Azure  in particular. Also, the future is now! The Windows Azure HPC Scheduler is awesome for high performance computing scenarios where an extra boost is needed. You gotta love the elastic cloud! I will start working on a Rock Paper Azure bot this weekend and try to conquer the world. The speakers that shared this information with us were among others Patrik Löwendahl ,  Brian Prince  and of course  Björn Eriksen  that is organizi...

Binding a HTML-formatted string to a WPF WebBrowser control

Sometimes there is a need to display a HTML formatted string in a WPF application. There are a couple of ways to do this, but the most stright forward is to use a WebBrowser control and the NavigateToString method. This approach has one big flaw, you cannot use binding to a string out of the box, but I found a great solution through Stack Overflow which adds a bindable property to the  WebBrowser  control using  NavigateToString . The following class is all that is needed to add that behavior. A new depencency property named Html is introduced to the  WebBrowser  and the proper change action is performed in the OnHtmlChanged method. public class BrowserBehavior { public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached( "Html", typeof(string), typeof(BrowserBehavior), new FrameworkPropertyMetadata(OnHtmlChanged)); [AttachedPropertyBrowsableForType(typeof(WebBrowser))] public static string GetHtml(...

New Silverlight 5 features in The Cornball

The release of Silverlight 5 is allegedly the last major release of Silverlight , but it brought some great stuff that I have now incorporated in the latest release of The Cornball . There are a lot of good descriptions and even video samples of the new features in  What's new in Silverlight 5 . These are the two new features that I am using in The Cornball . Mouse Button Double Click Previously I have used a custom implementation with a timer to capture double clicks on the cards. But since this feature finally is included in the framework I decided to use that instead. The MouseButtonEventArgs now has a ClickCount property which simply indicates the number of times the mouse button was clicked. That leaves us with a much cleaner implementation of the single and double click scenario. private void CardMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { switch (e.ClickCount) { case 1: // Handle single click break; case 2: ...

Microsoft LEAP Sweden 2012

I have been privileged to participate in the  Lead Enterprise Architect Program  at Microsoft this spring. The Lead Enterprise Architect Program (LEAP) is a program that aims to give understanding of the strategic core components of the Microsoft platform. LEAP consists of five master class training sessions as well as three days of sessions at Microsoft headquarters in Redmond, Seattle. The direction of the classes are extremely appealing and I really hope that they will live up to my expectations: Application architecture on the Microsoft Platform Cloud computing for Architects Identity & Access Management Integration on premises and in the cloud Business Intelligence and Enterprise Search The program starts in February and ends with the trip to Redmond in the end of May. During this time I will try to write as much as possible about this whole experience, which I hope will be amazing!

My experience with TFS Preview

I have been using the Team Foundation Services preview for a while now, and the least I can say is that I am impressed! The web interface has a nice Metro style design and gives a high quality experience overall. By installing the KB2581206  hotfix I can also access my TFS account from Visual Studio, which is absolutely fantastic. I might even sound a bit fanatic by saying that it is like a dream come true... The server appears as a traditional Team Foundation Server in Visual Studio apart from the logon process (a Windows Live popup window). Read more and try it out yourself! Besides playing around with the user interface I have actually tried to work a little seriously with the service as well. Partly by uploading my project the Cornball and developing against the TFS in Visual Studio. So far this experience is very positive and I have not found any issues or oddities at all, except that the environment is kind of slow. This whole thing also takes me one step further out i...

Google Search Appliance .NET solution

I recently worked on a project to integrate the search results from a Google Search Appliance into a .NET application. Although it seems like a pretty straight forward task, there were some small issues that I thought I might share. Background The communication with the Google Search Appliance is made simply with a HTTP GET request, which returns an XML formatted response. As for the Suggest service, the request looks about the same but the result returned is JSON encoded. For more information, please see the Google Search Appliance documentation , which is very extensive. Internationalization Since this solution was to incorporate several languages, the .NET application had to support and differentiate the languages. The GSA has very good built-in language support but there were at least one thing that was not completely obvious. The GSA uses either HTTP headers or query string parameters to determine in which language the request is made. If neither of these are supplied...

Changes in my life, and on my blog

Yesterday I did my last working day at my previous employer and the next week I will begin my employment as a Microsoft consultant at  Connecta  in Stockholm. Of course this means that things will change, but only for the better I hope. Leaving a position that you like on a workplace that you like is a tough decision. But in my case there are some amazing possibilities and challenges that I just cannot turn down. The type of work I will be doing differs a bit from my previous position. I will be more focused on development, which also means that I will try to change the direction of this blog to be more development oriented. At the same time, I have come to realize that by blogging in Swedish I do not really contribute to the community as much as I could. Therefore I have chosen to translate my previous posts to English and continue to blog in English from now on. Furthermore I have decided to focus more on my blog. I have some interesting projects ahead, both perso...