Last active
December 21, 2015 05:38
-
-
Save bhameyie/6257823 to your computer and use it in GitHub Desktop.
Composable Services using Service Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.Composition; | |
using System.ComponentModel.Composition.Hosting; | |
using System.IO; | |
using System.Linq; | |
using System.Configuration; | |
using System.Collections.Generic; | |
using System.Web.Mvc; | |
using Common; | |
using ServiceStack.Mvc; | |
using ServiceStack.WebHost.Endpoints; | |
[assembly: WebActivator.PreApplicationStartMethod(typeof(Spike.AppHost), "Start")] | |
namespace Spike { | |
public class AppHost | |
: AppHostBase { | |
[ImportMany] | |
List<IComposobaleBootstrapper> m_composers; | |
private CompositionContainer m_container; | |
public AppHost() //Tell ServiceStack the name and where to find your web services | |
: base("Making it Happen", typeof(HelloService).Assembly) { | |
var catalog = new AggregateCatalog(); | |
var servicesFolder = @"C:\Spike\bin"; | |
catalog.Catalogs.Add(new DirectoryCatalog(servicesFolder)); | |
//TODO: to load based on folder and sub folders | |
//var folders = Directory.GetDirectories(servicesFolder, "*", SearchOption.AllDirectories); | |
//foreach (string folder in folders) { | |
// var directoryCatalog = new DirectoryCatalog(folder); | |
// catalog.Catalogs.Add(directoryCatalog); | |
//} | |
m_container = new CompositionContainer(catalog); | |
m_container.ComposeParts(this); | |
if (m_composers.Count > 0) { | |
EndpointHost.ConfigureHost(this, "Composed Services", CreateServiceManager(m_composers.Select(e => e.GetAssembly()).ToArray()));//add distince statement top prevent dups | |
} | |
} | |
public override void Configure(Funq.Container container) { | |
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true; | |
Routes.Add<Hello>("/hello") | |
.Add<Hello>("/hello/{Name*}"); | |
foreach (var composobaleBootstrapper in m_composers) { | |
composobaleBootstrapper.Compose(container, Routes); | |
} | |
//Enable Authentication | |
ConfigureAuth(container); | |
//Register all your dependencies | |
container.Register(new TodoRepository()); | |
container.Register(new UserRepository()); | |
//Set MVC to use the same Funq IOC as ServiceStack | |
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container)); | |
} | |
private void ConfigureAuth(Funq.Container container) { | |
//Auth stuff | |
} | |
public static void Start() { | |
new AppHost().Init(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment