Skip to content

Instantly share code, notes, and snippets.

@nemesv
Created August 9, 2013 18:36
Show Gist options
  • Save nemesv/f36b27826a401eec8f82 to your computer and use it in GitHub Desktop.
Save nemesv/f36b27826a401eec8f82 to your computer and use it in GitHub Desktop.
namespace AutofacTest
{
class Program
{
static void Main(string[] args)
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<Logger>().As<ILogger>();
builder.RegisterType<EchoService>();
using (IContainer container = builder.Build())
{
Uri address = new Uri("http://localhost:8080/EchoService");
ServiceHost host = new ServiceHost(typeof(EchoService), address);
var basicHttpBinding = new BasicHttpBinding();
host.AddServiceEndpoint(typeof(IEchoService), basicHttpBinding, string.Empty);
host.AddServiceEndpoint(typeof(IEchoService2), basicHttpBinding, string.Empty);
host.AddDependencyInjectionBehavior<EchoService>(container);
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpGetUrl = address });
host.Open();
Console.WriteLine("The host has been opened.");
Console.ReadLine();
host.Close();
Environment.Exit(0);
}
}
}
[ServiceContract]
public interface IEchoService
{
[OperationContract]
string Test();
}
[ServiceContract]
public interface IEchoService2
{
[OperationContract]
string Test2();
}
public interface ILogger
{
}
public class EchoService : IEchoService, IEchoService2
{
private readonly ILogger logger;
//just to see that Autofac inject the dependicies
public EchoService(ILogger logger)
{
this.logger = logger;
}
public string Test()
{
return "TEst1";
}
public string Test2()
{
return "TEst1";
}
}
public class Logger : ILogger
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment