Skip to content

Instantly share code, notes, and snippets.

@scottmcarthur
Created February 1, 2014 09:26
Show Gist options
  • Save scottmcarthur/8750037 to your computer and use it in GitHub Desktop.
Save scottmcarthur/8750037 to your computer and use it in GitHub Desktop.
ServiceStack IoC injection example (v4)
using System;
using ServiceStack;
namespace Tests.Recipe
{
class MainClass
{
public static void Main()
{
// Very basic console host
var appHost = new AppHost();
appHost.Init();
// Create instance of SomeClass
var someClass = new SomeClass();
// Immediately use the IoC injected property
someClass.AppApplicationContext.Name = "Hello World";
// Output
Console.WriteLine(someClass.AppApplicationContext.Name);
Console.ReadKey();
}
}
public class AppHost : AppHostBase
{
public AppHost() : base("Test Service", typeof(TestService).Assembly) {}
public override void Configure(Funq.Container container)
{
container.RegisterAutoWiredAs<AppApplicationContext, IAppApplicationContext>();
}
}
public class TestService : Service
{
}
public class SomeClass
{
public IAppApplicationContext AppApplicationContext { get; set; }
public SomeClass()
{
HostContext.Container.AutoWire(this);
}
}
public interface IAppApplicationContext
{
string Name { get; set; }
}
public class AppApplicationContext : IAppApplicationContext
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment