Mugen Injection [WCF]
Getting Started
WCF integration is in the MugenInjection.Web.dll and MugenInjection.Wcf.dll assembly.The MugenInjection WCF support is available for:
- IIS Hosted Services
- Self-Hosted Services
Self-Hosted Services
Below is a sample of a web service that has a constructor dependency on an ITimeService instance.[ServiceContract] public interface ITimeService { [OperationContract] int GetDay(); [OperationContract] DateTime GetUtcTime(); }
public class DefaultTimeService : ITimeService { #region Implementation of ITimeService public int GetDay() { return DateTime.UtcNow.Day; } public DateTime GetUtcTime() { return DateTime.UtcNow; } #endregion }
public class NoLoggingTimeService : ITimeService { #region Fields private readonly ITimeService _timeService; #endregion #region Constructor /// <summary> /// Initializes a new instance of the <see cref="NoLoggingTimeService"/> class. /// </summary> public NoLoggingTimeService(ITimeService timeService) { _timeService = timeService; } #endregion #region Implementation of ITimeService public int GetDay() { return _timeService.GetDay(); } public DateTime GetUtcTime() { return _timeService.GetUtcTime(); } #endregion }
public class MainService : ServiceBase { #region Fields private IInjector _injector; private ServiceHost _serviceHost; #endregion #region Constructor /// <summary> /// Initializes a new instance of the <see cref="MainService"/> class. /// </summary> public MainService(IInjector injector) { _injector = injector; } #endregion #region Static methods private static void Main(string[] args) { var injector = new MugenInjector(new ServiceModule()); injector.Bind<ITimeService>().To<DefaultTimeService>().InSingletonScope(); WcfConfigExtension .CreateWcfConfiguration(injector) .Start(); var mainService = injector.Get<MainService>(); #if DEBUG mainService.OnStart(null); Console.Read(); #else Run(mainService); #endif } #endregion #region Overrides of ServiceBase protected override void OnStart(string[] args) { try { Console.WriteLine("Starting a service..."); if (_serviceHost != null) _serviceHost.Close(); _serviceHost = WcfConfigExtension.CreateServiceHost(typeof (NoLoggingTimeService)); _serviceHost.Open(); Console.WriteLine("Service was started."); } catch (Exception exception) { Console.WriteLine("Fatal exception when starting service: {0}", exception); } } protected override void OnStop() { try { if (_serviceHost != null) _serviceHost.Close(); if (_injector != null) { _injector.Dispose(); _injector = null; } } catch (Exception exception) { Console.WriteLine("Fatal exception when stopping service: {0}", exception); } } #endregion }
IIS Hosted Services
Change the global.asax to derive from MugenInjectionHttpApplication instead of HttpApplication and override CreateInjector to create a IInjector and load all modules that you need in your application, and set the configuration for WCF:public class Global : MugenInjectionHttpApplication { #region Overrides of MugenInjectionHttpApplication protected override IInjector CreateInjector() { var injector = new MugenInjector(); injector.Bind<ITimeService>().To<DefaultTimeService>().InOperationRequestScope(); return injector; } protected override void Configure(IApplicationConfiguration applicationConfiguration) { applicationConfiguration .WithHttpConfiguration() .WithWcfConfiguration(); } #endregion }
<%@ ServiceHost Language="C#" Debug="true" Service="IISHostedService.NoLoggingTimeService" CodeBehind="NoLoggingTimeService.svc.cs" Factory="MugenInjection.Wcf.MugenInjectionServiceHostFactory" %>
public class NoLoggingTimeService : ITimeService { #region Fields private readonly ITimeService _timeService; #endregion #region Constructor public NoLoggingTimeService(ITimeService timeService) { _timeService = timeService; } #endregion #region Implementation of ITimeService public int GetDay() { return _timeService.GetDay(); } public DateTime GetUtcTime() { return _timeService.GetUtcTime(); } #endregion }