About .NET, ASP.NET, MVC, C#, WPF, WCF and everything related to .NET and more.

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();
}
The interface and implementation for the DefaultTimeService that will be the dependency.
public class DefaultTimeService : ITimeService
{
    #region Implementation of ITimeService

    public int GetDay()
    {
        return DateTime.UtcNow.Day;
    }

    public DateTime GetUtcTime()
    {
        return DateTime.UtcNow;
    }

    #endregion
}
In this example the ITimeService interface also used as a service-contract:
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
}
The windows-service Application code for building the container and hosting the web service:
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<MainSe