2

I'm trying to use WCF REST Services and Unity (Enterprise Library 5), but can not find examples of how to implement the resolution of the services using Unity. I would like to call the registration of all interfaces and implementations for resolution in global.asax.

I'm using the following class to register the container:

public class DependencyResolver
{
    public static IUnityContainer Container { get; private set; }\

    static DependencyResolver()
    {
        Container = new UnityContainer()
            .AddNewExtension<EnterpriseLibraryCoreExtension>()
            .RegisterType<IServiceDAO, ServiceDAO>()
            .RegisterType<IServiceBO, ServiceBO>()
            .RegisterType<IService, Service>();
    }
}

I searched but could not find enough examples, someone would have a code example with this solution applied?

I found some examples, like How do I pass values to the constructor on my wcf service? but I want the same context for all services. I want something like this http://entlibex.codeplex.com/wikipage?title=Unity%20Service%20Behavior&referringTitle=Documentation but in my case I don't have a svc file.

Thank you.

Community
  • 1
  • 1

1 Answers1

3

Why are you using WCF 4 REST? WCF 4 REST is a little bit out dated and hard to use.

I suggest you to use ASP.NET Web API (former WCF Web API). It's easier to use for REST service and with DI/IoC containers (in WCF you should write your own host, behavior etc.) as it has good built-in connectivity support for DI/IoC.

Here is example:

http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver

Regfor
  • 8,515
  • 1
  • 38
  • 51
  • I can't, is a restriction of the project. – Nicolás Caorsi Jun 22 '12 at 17:42
  • Unity is also restriction? Because Autofac has such funtionality already done "out of the box" – Regfor Jun 22 '12 at 17:44
  • When Unity is also restriction than you should integrate WCF with Unity manually. Here is example of how to do it correctly: http://www.neovolve.com/post/2010/05/15/Unity-dependency-injection-for-WCF-services-e28093-Part-1.aspx, http://www.neovolve.com/post/2010/05/17/Unity-dependency-injections-for-WCF-services-e28093-Part-2.aspx. So you need your ServiceHostFactory, Behavior etc. But personally I will prefer Autofac. – Regfor Jun 22 '12 at 17:50