Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6344855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:43:19+00:00 2026-05-24T20:43:19+00:00

I have been looking for a way to use Unity for Dependency Injection in

  • 0

I have been looking for a way to use Unity for Dependency Injection in my WCF service. I have been trying to understand the code described in these two blogs, which is quite similar:

  • Integrating Unity with WCF
  • WCF and Unity 2.0

So I added this code to a separate project in my solution and refer to the custom servicehostfactory in a SVC file to my WFC service (separate project).

The question is now: How do I access the objects in my container from my WCF Service methods?


EDIT

These are my implementations:

ServiceHostFactory…

namespace UnityWcfAssembler
{
public class UnityServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        UnityServiceHost serviceHost = new UnityServiceHost(serviceType, baseAddresses);
        UnityContainer container = new UnityContainer();
        serviceHost.Container = container;

        //TODO configuration from app.config 
        //configure container
        //UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        //section.Configure(serviceHost.Container);

        InitializeSessionFactories(container);

        return serviceHost;
    }

    private static void InitializeSessionFactories(UnityContainer container)
    {
        Dictionary<String, ISessionFactory> sessions = new Dictionary<string, ISessionFactory>();

        Configuration Cfg = new Configuration();
        Cfg.Configure();
        Cfg.SetProperty("connection.connection_string",
            "Data Source=(Local);Initial Catalog=Fossils;Integrated Security=true;");
        ISessionFactory Factory = Cfg.BuildSessionFactory();
        sessions.Add("fossils", Factory);

        Cfg.SetProperty("connection.connection_string",
            "Data Source=(Local);Initial Catalog=TypeCollection;Integrated Security=true;");
        ISessionFactory typeFactory = Cfg.BuildSessionFactory();

        sessions.Add("type", typeFactory);

        Cfg.SetProperty("connection.connection_string",
            "Data Source=(Local);Initial Catalog=PersonalCollection;Integrated Security=true;");
        ISessionFactory persFactory = Cfg.BuildSessionFactory();

        sessions.Add("personal", persFactory);

        container.RegisterInstance(sessions);
    }
}
}

ServiceHost…

namespace UnityWcfAssembler
{
    public class UnityServiceHost : ServiceHost
    {
        public UnityContainer Container { get; set; }

        public UnityServiceHost()
        {
            Container = new UnityContainer();
        }

        public UnityServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)
        {
            Container = new UnityContainer();
        }

        protected override void OnOpening()
        {
            new UnityServiceBehavior(Container).AddToHost(this);

            base.OnOpening();

            if (Description.Behaviors.Find<UnityServiceBehavior>() == null)
                Description.Behaviors.Add(new UnityServiceBehavior(Container));
        }
    }
}

InstanceProvider…

namespace UnityWcfAssembler
{
    public class UnityInstanceProvider : IInstanceProvider
    {
        public UnityContainer Container { set; get; }
        public Type ServiceType { set; get; }

        public UnityInstanceProvider() : this(null)
        {
        }

        public UnityInstanceProvider(Type type)
        {
            ServiceType = type;
            Container = new UnityContainer();
        }

        // Get Service instace via unity container
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return Container.Resolve(ServiceType);
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return GetInstance(instanceContext, null);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }
    }
}

IServiceBehavior…

namespace UnityWcfAssembler
{
    public class UnityServiceBehavior : IServiceBehavior
    {
        public UnityInstanceProvider InstanceProvider { get; set; }
        private ServiceHost serviceHost;

        public UnityServiceBehavior()
        {
            InstanceProvider = new UnityInstanceProvider();
        }

        public UnityServiceBehavior(UnityContainer unity)
        {
            InstanceProvider = new UnityInstanceProvider();
            InstanceProvider.Container = unity;
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher cd = cdb as ChannelDispatcher;
                if (cd != null)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        InstanceProvider.ServiceType = serviceDescription.ServiceType;
                        ed.DispatchRuntime.InstanceProvider = InstanceProvider;
                    }
                }
            }
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }

        public void AddToHost(ServiceHost host)
        {
            // only add to host once
            if (serviceHost != null) return;
            host.Description.Behaviors.Add(this);
            serviceHost = host;
        }
    }
}

Wcf Service…

namespace FossilsWcfService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
    public class FossilsService : IFossilsService
    {
        private readonly Dictionary<string, ISessionFactory> sessionFactories;

        public FossilsService(Dictionary<string, ISessionFactory> s)
        {
            sessionFactories = s;
        }

        public SpeciesList GetAllSpecies()
        {
            SpeciesList list = new SpeciesList();

            ISessionFactory factory = sessionFactories["fossils"];

            if(factory == null)
            {
                list.Species.Add(new FossilSpecies { GenusName = "Session factory could not be resolved from container!" });
                return list;                
            }

            ISession session = factory.OpenSession();

            SpeciesManager speciesManager = new SpeciesManager(session);

            IList<FossilSpecies> species = speciesManager.GetAllSpecies();

            foreach (FossilSpecies fossilSpecies in species)
            {
                list.Species.Add(fossilSpecies);
            }
            return list;
        }

FossilsWcfService.svc…

<%@ ServiceHost Language="C#" Debug="true" 
Service="Server.Services.ExampleService"
Factory="UnityWcfAssembler.UnityServiceHostFactory" %>

Should the latter have a different filename perhaps?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:43:19+00:00Added an answer on May 24, 2026 at 8:43 pm

    Now that you have the factory working, you should create a property with the interface, and declare the constructor and Unity will do the rest

    public class InvoiceService : IInvoiceService {
    
        private IPayService payService;
    
        public IPayService PayService
        {
            get { return payService; }
            set { payService= value; }
        }
    
    
        public InvoiceService(IPayService provider)
        {
            this.payService= provider;
        }
    
        public bool Pay(){
             return PayService.Pay();
        }
    
    }
    

    My Service Factory Implementaion

    public class InvoiceFactory : ServiceHostFactory
        {
            protected override ServiceHost CreateServiceHost(
                                              Type serviceType, Uri[] baseAddresses)
            {
                UnityServiceHost host = new UnityServiceHost(serviceType, baseAddresses);
                UnityContainer unity = new UnityContainer();
                host.Container = unity;
    
    //I'm doing it like this because I put some AOP in the service injected
                var clazz = Intercept.ThroughProxy<IPayService>(new PayServiceConcreteClass(), 
                                                new InterfaceInterceptor(), new[] { new LoggingInjection() });
    
                unity.RegisterType<IPayService>().RegisterInstance(clazz);
    
                return host;
            }
        }
    

    EDIT after code in the question

    I’m not really sure what could be wrong, but I spot two things that I’m not sure about:

    ServiceHostFactory…

    //Better as an Interface
    IDictionary<String, ISessionFactory> sessions = new Dictionary<string, ISessionFactory>();
    
    
    //container.RegisterInstance(sessions);
    //Registering the type not the class
    container.RegisterType<IDictionary<String, ISessionFactory>>().RegisterInstance(sessions);
    

    Wcf Service…

    private readonly IDictionary<string, ISessionFactory> sessionFactories;
    
            public FossilsService(IDictionary<string, ISessionFactory> s)
            {
                sessionFactories = s;
            }
    

    FossilsWcfService.svc

    It’s not missing the codebehind attribute? something like this

    <%@ ServiceHost Language="C#" Debug="true" Factory="InvoiceFactory" Service="InvoiceService" CodeBehind="InvoiceService.svc.cs" %>
    

    …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking at various dependency injection frameworks for .NET as I feel
I've been struggling looking for an understandable way to do this. I have four
I have been looking into IKVMing Apache's FOP project to use with our .NET
I have been looking for a way to improve the performance of my CATiledView.
I have been looking for a way to show the iPhone screen (not the
I have been looking for a way to get the comments from a Blogger
I have been looking for a way to create a function to check if
I have been looking into Code First with Entity Framework CTP4 and you can
I have been looking for a way to launch daemons on the iPhone and
I have been looking for a way to do the following action using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.