Start web-api commanding in 30 seconds in LightSwitch via Nuget TheLightSwitchToolbox.

Why does it take 30 seconds?

In my previous post regarding my Nuget package for injecting a RIA service in a LightSwitch project, I claimed it only takes 15 seconds. Why is the web-api package taking 30 seconds? The reason is pretty simple: you need two nuget packages, one for the server and one for the silverlight client.

We presume that you are familiar with the nuget console in Visual studio.

How to proceed server side

Inject following package in the LightSwitch server project:

server

This package will do a lot for you. Basically, it injects all server side what’s described in my various posts on web-api. The most important improvement in this package is a base class I designed called LightSwitchApiController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace LightSwitchApplication
{
    public class LightSwitchApiController: ApiController
    {
        public bool ownServerContext { get; set; }
        public static DataWorkspace CurrentDataWorkspace;

        public LightSwitchApiController()
        {
            if (ServerApplicationContext.Current == null)
            {
                this.ownServerContext = true;
                ServerApplicationContext.CreateContext();
            }
            else
            {
                this.ownServerContext = false;
            }
            CurrentDataWorkspace = ServerApplicationContext.Current.DataWorkspace;
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (this.ownServerContext &&
                        ServerApplicationContext.Current != null &&
                       !ServerApplicationContext.Current.IsDisposed)
                    {
                        ServerApplicationContext.Current.Dispose();
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
    }
}

How to proceed client side

Inject following package in the LightSwitch silverlight client project:

Client

 

The client nuget package is much lighter than the server nuget package. It injects the NewtonSoft.Json dll and adds a base class for handling the web-api calls. It opens up also a readme.txt file which gives some instructions on how to use it client side.

Conclusion

Try it out and give feedback !