ASP.NET MVC Dependency Injection Unity with WCF Services - Working Sample Solution

I am looking for a working model of an ASP.NET MVC web application that uses Unity and calls a WCF service. I looked through a lot of explanations on how to add dependency injection to WCF services, but frankly, I'm a little over the head. It does not help that I am new to WCF services.

I am currently using Unity with Contructor injection for our ASP.NET MVC applications, but so far we are not using WCF web services. The plan is to start using web services, and I am very confused about how to enable Unity with them.

I would love a good working sample that I could go through to better understand how to do this.

+4
source share
3 answers

It looks like you are already injecting your MVC controllers using Unity, and all you want to do is start injecting the WCF services that you accept. To implement WCF services, you need to use IInstanceProvider .

A complete working solution is here:

http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html

You need 4 very simple classes:

 MyServiceHostFactory MyServiceHost DependencyInjectionServiceBehavior DependencyInjectionInstanceProvider 

define them, specify your new ServiceHostFactory:

 <%@ ServiceHost Service="NamespaceC.ServiceLayer, AssemblyC" Factory="NamespaceD.MyServiceHostFactory, AssemblyD" %> 

and you're done.

+4
source

I will try to provide you some recommendations.

Suppose you have an existing WCF service defined like this (we don’t care about an implementation that is not important at the moment, you can implement it on your own, varying from hard-coded values, going through the SQL and ORM database, for using another service in the cloud):

 [DataContract] public class Product { [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } } [ServiceContract] public interface IProductsService { [OperationContract] Product Get(int id); } 

Now in your ASP.NET MVC application, the first step is to add a service reference, pointing to the WSDL. This will create proxy client classes.

You can then add the Unity.Mvc3 NuGet package to your MVC application.

Then in your Application_Start you can configure the container (obviously, this configuration could be externalized in a separate method to avoid cluttering your Global.asax with it):

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var container = new UnityContainer(); container .RegisterType<IProductsService, ProductsServiceClient>() .Configure<InjectedMembers>() .ConfigureInjectionFor<ProductsServiceClient>(new InjectionConstructor("*")); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } 

IProductsService and ProducsServiceClient used in this configuration are proxy classes generated when importing the web service definition.

From now on, things become trivial:

 public class HomeController : Controller { private readonly IProductsService _productsService; public HomeController(IProductsService productsService) { _productsService = productsService; } public ActionResult Index() { var product = _productsService.Get(1); return View(product); } } 

and some corresponding index index:

 @model Product <div> @Html.DisplayFor(x => x.Name) </div> 

As you can see from this example, thanks to the abstraction of IProductsService , the HomeController is completely separate from any specific service implementation. In Today, in your Global.asax, you decide to use WCF (ProductServiceClient), but tomorrow you may decide to use a completely different implementation. With one change in the configuration of the DI container, you can switch the implementation. Thanks to this loose connection, your controllers are fully tested separately.

It’s important to understand that your business is the Product class and the IProductsService interface. This is what your domain reflects. This is M in MVC. Implementations may change, but this should remain unchanged, otherwise you have incorrectly defined your business requirements, which can be catastrophic in the long run.

Note: one thing that I did not consider in this example, and which is very important, is the use of view models. In a properly designed ASP.NET MVC application, you should never pass domain models to your views (in this example, the Product class). You must use view models. View models are classes specifically designed for this type of requirement. Thus, in a real ASP.NET MVC application, you will have the ProductViewModel class for which the product model will be displayed in the controller action, and this ProductViewModel will be passed to the view. These view models must be defined in the MVC project, because, contrary to your domain models, they cannot be reused and reflect only the specific requirements for a single view. To simplify the mapping between your domain models and view models, you can take a look at AutoMapper .

+12
source

I know this a little late in the game, but I wrote the Nuget package to simplify the process of using WCF in your MVC / WebApi application, and it uses Unity.

See Unity.Mvc.Wcf on Codeplex or GitHub for details.

0
source