Wednesday, November 6, 2013

Sitefinity ajax requests

On a project at work we used Sitefinity from Telerik as a CMS System. I can say that it is a very good and especially end-user friendly CMS System because it let's them drag & drop so called widgets on the page and this is straight forward for almost everyone.

We are using the mixed mode (aspx and mvc) but I developed all my widgets in MVC.
For one widget I had the request to show statistical data in a chart and the request to the datasource (an external web server) takes about 2-3 seconds. I didn't want to let the user feel that the whole page is not responding just because of this data request and so I decided to to an ajax request in the background.
With jQuery this is an easy task, but I was not able to return the json data to the client. I returned a JsonResult on the Controller but Sitefinity always injected somehow the master layout of the page and therefore it was invalid json when it was received by the client.

Fortunately I found a solution to this problem:

public ActionResult GetAjaxData()
{
    Response.ClearHeaders();
    Response.AddHeader("Content-type""text/json");
    Response.AddHeader("Content-type""application/json");
    Response.ClearContent();
    //here you obtain your list of objects
    List myResult = new List(); 
    Response.Write(myResult.ToJson());
    Response.End();
 
    return new EmptyResult();
}

Wednesday, April 11, 2012

WCF RIA Services with JSON endpoint

Today I had the task to add a JSON endpoint for a WCF RIA service for which there exists a Silverlight client. I wanted to state some problems that I encountered such that maybe others can get it to work faster:

  1. Install the following two updates: 
    1. WCF RIA Services V1.0 SP2 
    2. WCF RIA Services Toolkit (September 2011)
  2. Add a reference to "Microsoft.ServiceModel.DomainServices.Hosting" to your web application project
  3. Add the following to your web.config into the tag
        <domainServices>
          <endpoints>
            <add name="OData" type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add name="Soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add name="Json" type="Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
          endpoints>
        domainServices>
    
  4. Here the first pitfall arised; the application did not compile anymore complaining:
    The "CreateRiaClientFilesTask" task failed unexpectedly.
    Unrecognized configuration section system.serviceModel/domainServices.

    To resolve this add the following to the tag:
    <sectionGroup name="system.serviceModel">
          <section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" />
        sectionGroup>
    
  5. According to some howtos the URL is composed as follows:
    http://localhost:[port]/[Name of Silverlight Project]-Web-[Name of Webservice]/Json/[Method name]
    e.g.: http://localhost:2002/MySilverlightApp-Web-DataAccessService/Json/GetArticles
  6. The above URL always returned 404 Not found
  7. My next pitfall was that my service class file was not in the web project but in a subfolder ("Services") of it
  8. To solve that I had to modify the URL as follows:
    http://localhost:[port]/[Name of Silverlight Project]-Web-[Folder name]-[Name of Webservice]/Json/[Method name]
    e.g.: http://localhost:2002/MySilverlightApp-Web-Services-DataAccessService/Json/GetArticles
  9. After that I finally got my JSON from my webservice :-)

Monday, March 12, 2012

Entity Framework no columns when mapping stored procedure

Recently I had the problem that I wanted to map a stored procedure with the Entity Framework. As soon as I clicked on "Get column information" no columns showed up despite I knew that the stored procedure returns a table with some columns.

After some research I found out that in some circumstances it is necessary to add the following two statements at the beginning of the stored procedure:

SET NOCOUNT OFF;
SET FMTONLY OFF;

After that everything worked as expected.

Wednesday, March 9, 2011

New version of Team Viewer

Since my first blog post about Team Viewer has become a sort of "support forum", which has reached 125 comments now, I decided to write about the new version.

As I already wrote on my first post Team Viewer is really great if you have to help kin and kith with all sorts of computer problems. It is much easier and takes less time to solve a problem if you can see it as if someone (who does not know much about computers) describes you what he/she sees. You could also use remote desktop through the MSN Messenger but I tried it out and I had problems to get it to work. Firewalls are the biggest problem for not experienced users.

Team Viewer is very easy to use and has no problems with firewalls; that is what I like so much about it; and of course that it is FREE for personal use ;-). Also a computer noob can run it and tell you the ID and the password. Team Viewer is also very useful to maintain remote machines such as your home server.

The application has reached version 6 now and they say that they have improved many things such as performance, usability and much more. If you are interested in all the details have a look at http://www.teamviewer.com/en/download/tv6.aspx. You can also use it to share files with someone over the internet but since I’m using DropBox to store and share files I don’t use this feature very much.

Since I am a proud owner of an Android Smartphone (Samsung Galaxy S) I was curious to try out also the Team Viewer application for Android. It is really amazing how good it works!! I tried it over Wifi, where the speed is quite good, and over my mobile phone network where it goes much slower but is still usable. This allows me to help kin and kith with their computer problems also on the go :-) There exists also an iPhone and iPad app but I couldn’t try it out because I don’t have one.

Link to Team Viewer

Monday, November 29, 2010

Smelly code

Original, smelly code
public bool isLocked(string s)
{
    if (!File.Exists(s)) return false;
    try { FileStream f = File.Open(s, FileMode.Open, FileAccess.ReadWrite); f.Close(); }
    catch { return true; }
    return false;
}

Objections and Comments
The code abve has in my eyes several problems:

  1. Method name starts with lower case
  2. The name of the input parameter is meaningless
  3. Logical code blocks are not separated by a new line
  4. You need some time to understand what this 4 lines of code do
  5. Multiple instructions on the same line
  6. The "return false;" statement should be inside the try block because it is logically associated to it
  7. Missing Method documentation


Proposed Refactoring
/// <summary>
/// Checks if a file is in use or not.
/// </summary>
/// <param name="filePath">The path to the file that should be checked.</param>
/// <returns>True if the file is locked; false otherwise</returns>
public bool IsLocked(string filePath)
{
    if (!File.Exists(filePath))
    {
        return false;
    }

    try
    {
        FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
        fileStream.Close();
        return false;
    }
    catch
    {
        return true;
    }
}

Monday, July 19, 2010

Strange undestroyable folders

A friend of mine had a very very strange problem. Suddenly on his external hard disk there appeared a bunch of very strangely named folders like "%/sl94..." and other symbols which windows was not able to display. They occupied 300GB and clearly my friend wanted to delete them; unfortunately windows was not able to delete them. It gave always an error like "Path not found" or "Invalid path symbols".
I tried then to delete them with different data shredders but none of them was able to successfully delete them.
I have then run also different virus and spy-ware scanners over them but none of them found something.
I was stuck and at the end with my knowledge.

Then I found on the Internet the good old command line instruction:

del foldername

With that I was finally able to delete them all.

Direct SQL vs. View vs. Stored Procedure

I was wondering for a very very long time what all these "battels" around direct sql, views and stored procedures are about. I read several articles on the Internet where people were claiming that nowadays there is almost no difference anymore between these three database "constructs". I saw many discussions and blogs where people tested the performance of these three with different results and that made me even more confused.

So as I am curious and the fact that I need to know this as a software developer I decided to try this out by myself. At work I had the chance to test this against a very large amount of real data (I needed to optimize this query anyway);
Sometimes the sample databases which are used for theses kinds of tests are either very small or very simple or even both which makes not really sense in my eyes.

So the query consisted of joins of multiple tables with a case in the select statement. I run the query 300 times with every "construct" and then I took the average value for all three of them in order to reduce network and SQL Server workload "noise".

The result of my test (in Ticks):

Direct SQL

23077

View

19363

Stored procedure

3438

So what I found out for myself is that it really does matter what kind of "construct" you use to query your data; especially on a large amount of data.
On large data you should also take into consideration that a big performance impact may also have where you set your indexes and which statistical data you let create by SQL Server.

If you made other experiences on this, please let me know and leave a comment...