To use the container in WCF you need to add code to your Global.asax.cs file:

    public class Global : MugenInjectionHttpApplication
    {
        #region Overrides of MugenInjectionHttpApplication

        /// <summary>
        /// Creates the injector that will manage your application.
        /// </summary>
        /// <returns>
        /// The created injector.
        /// </returns>
        protected override IInjector CreateInjector()
        {
            var injector = new MugenInjector();
            injector.Bind<ISimpleInjectedService>()
                .To<SingletonInjectedService>()
                .WhenInto<SingletonService>();

            injector.Bind<ISimpleInjectedService>()
                .To<SimpleInjectedService>()
                .WhenInto<SimpleService>()
                .InOperationRequestScope();

            return injector;
        }

        /// <summary>
        /// Configure current <see cref="T:MugenInjection.Web.MugenInjectionHttpApplication"/>
        /// </summary>
        /// <param name="applicationConfiguration">Configuration instance.</param>
        protected override void Configure(IApplicationConfiguration applicationConfiguration)
        {
            //This is important.
            applicationConfiguration
                .WithWcfConfiguration();

            //You can add your IServiceBehavior and IDispatchMessageInspector
            applicationConfiguration.WithDispatchMessageInspector(new CustomDispatchMessageInspector());
            applicationConfiguration.WithServiceBehavior(new CustomServiceBehavior());

            //You can create custom behavior for release service instance, default used dispose behavior.

            /*applicationConfiguration.WithReleaseInstanceBehavior(new CustomeReleaseInstanceBehavior());*/
        }

        #endregion
    }

If you’re hosting in IIS, you’ll need to reference this ServiceHostFactory in your .svc file:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="TestMugenInjectorWcf.Service1" 
    CodeBehind="Service1.svc.cs"  
    Factory="MugenInjection.Wcf.MugenInjectionServiceHostFactory" %>

or, if you’re using .Net 4.0 and taking advantage of the new file-less activation, reference the ServiceHostFactory in your web.config like so:

<!-- Using configuration based activation so we don't need to have .svc files -->
<serviceActivations>
<add factory="MugenInjection.Wcf.MugenInjectionServiceHostFactory"
     service="TestMugenInjectorWcf.Service1"
     relativeAddress="Service1.svc" />
</serviceActivations>

In the WCF application is available one additional LifecycleScope, OperationRequestScope. More about LifecycleScope here.

You can add your IServiceBehavior and IDispatchMessageInspector,  for this you must add:

  applicationConfiguration.WithDispatchMessageInspector(new CustomDispatchMessageInspector());
  applicationConfiguration.WithServiceBehavior(new CustomServiceBehavior());

Also you can create custom behavior for release service instance, default used dispose behavior:

applicationConfiguration.WithReleaseInstanceBehavior(new CustomReleaseInstanceBehavior());

Last edited May 7, 2012 at 3:35 PM by VVS0205, version 5

Comments

No comments yet.