Loading Web API Controllers from other Libraries when Self Hosting Web API

When Self Hosting Web API, we have to create controllers on the same project, otherwise it does not detect the libraries even if we reference the Web API project where we have all the controllers and classes defined.

For example, if we have a Web API project which was hosted over IIS earlier and we decided to Self Host it on a windows service project or a console application; we have to copy-paste or drag/drop the classes from the Web API project to Self Host Web API project and also have to change its namespace so it could be searchable by the Web API framework. But what if we have a centralized project where we have all the controllers in place and we want Self Host Web API project to load all the controllers from there.

In order to overcome this scenario we can create a custom assembly resolver let say SelfHostAssemblyResolver and replace the default one to this. SelfHostAssemblyResolver implements IAssemblyResolver interface and then implements the GetAssemblies method that loads the assemblies from particular path.

So first, create a custom assembly resolver

In the below code, where you are initializing the Self Host Web API, replace the default assembly resolver with your custom assembly resolver using HttpSelfHostConfiguration object.

In the above code snippet, DemoWebApi.dll is the main Web API project I was using initially, that had used the default Web API hosting on IIS.

Happy coding!

Reducing XAP size of Silverlight Client apps

In this article I would like to share my experience related to the hosting of Silverlight Chat applications on real production servers and accessing it over internet.

I developed a Silverlight chat application based on Silverlight 5 and ASP.Net that’s works perfectly and runs smoothly but sometimes take some time to download and load a Chat Panel (that was based on Silverlight) which seems quite notorious. After doing some R&D I came to know that with simple configuration from Visual Studio on your project settings page you can compress the XAP file size and make your life easier.

As in the project I used Telerik and AJAX toolkit controls and some of the assembly sizes were big which takes some time to load the Silverlight application on client side. As by default, the XAP file embedded all the assemblies in itself. But you can change that option by “Reduce XAP size by using application library caching” option from Project properties of your Silverlight project.

By doing this a compressed version of all the assemblies downloaded on client side and cached and next time if you reload the application it will not download all the assemblies again but used the cached version.

When you enable this check all the assemblies that are referenced in the Silverlight project got compressed and added in the ClientBin folder.

After doing this, when I checked the XAP file size I got really happy.

Before compression

After compression