1

I am using UnityContainer for our Project. I have a requirement to Add a Custom Header for all out going wcf calls so i have implemented IClientMessageInspector and IEndpointBehavior. Now i am trying to get the header value from some object using UnityContainer Property Injection. The Same Property injection is working outside IClientMessageInspector but not inside my custom Clientmessageinspector.

Property Injection

[Dependency]
public IUnityContainer _container { get; set; }

public object BeforeSendRequest(ref Message request, IClientChannel channel){var test= _container; }

_container is always coming as null.

Can we use UnityContainer inside MessageInspector as normal as other classes?.

  • This isn't a direct answer, but something that might help. If you use Castle Windsor and their WcfFacility, you can register your ClientMessageInspector and then Windsor will both create the inspector and inject its dependencies, and it will handle the configuration to ensure the inspector always gets called. [Some info.](http://ialekseev.blogspot.com/2012/11/adding-custom-wcf-endpoint-behavior-in.html) – Scott Hannen Apr 25 '17 at 21:13
  • How do you create the `Clientmessageinspector` ? Unity will only inject into instances that creates itself (unless, really rare case, you use `BuildUp`)... – Haukinger Apr 26 '17 at 10:02
  • public class TestClientMessageInspector : IClientMessageInspector – Nithyanantham Apr 26 '17 at 10:06
  • @Haukinger, I have created testclientmessageinspector by inheriting from IClientMessageInspector. And Created Instance on testclientmessageinspector inside endpointbehavior by using below code. `public class TestClientMessageInspector : IClientMessageInspector. {}`, Creating instance of ClientMessageInspector inside Endpointbehavior under ApplyClientBehavior `public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.ClientMessageInspectors.Add(new testclientMessageInspector()); }` – Nithyanantham Apr 26 '17 at 10:12
  • If you create it with `new testclientMessageInspector()` how do you expect unity to do anything to the instance? Try `container.Resolve()` for example. – Haukinger Apr 26 '17 at 13:09
  • @Haukinger Here my problem is to access Resolve method I need unity container object right. Inside inspector or behaviour I am not getting unity container object itself. I tried with both property injection and constructor injection but I am always getting container as null so I am not having any of my registered instances – Nithyanantham Apr 26 '17 at 13:38
  • Then start from a class where the container is available. And btw, you do _not_ want to pass the container around. A simple google search reveals this (http://www.devtrends.co.uk/blog/introducing-unity.wcf-providing-easy-ioc-integration-for-your-wcf-services) which can easily be ported to the current unity.. – Haukinger Apr 26 '17 at 14:05

0 Answers0