Unity Dependency Injection for WCF services

后端 未结 3 1326
借酒劲吻你
借酒劲吻你 2020-12-17 16:13

Can someone direct me to a good example of Unity Dependency Injection for WCF services? Any blog or msdn article will also help.

3条回答
  •  清酒与你
    2020-12-17 17:04

    I'm about to give a try to Unity.Wcf library (https://github.com/ViceIce/unity.wcf), at first sight looks pretty good. I've read in this article this:

    If you are hosting your WCF service within a Windows Service using a ServiceHost, replace the ServiceHost instance with the custom Unity.Wcf.UnityServiceHost. You will find that the UnityServiceHost takes in a Unity container as its first parameter but is otherwise identical to the default ServiceHost.

    As it is my case I'm going to do this...

       class Program
    {
        static void Main(string[] args)
        {
            // 1st Initialize the Host (Configures Container and Factories)
            ServiceHostController.Initialize();
    
            // 2nd Create a URI to serve as the base address.
            var baseAddress = new Uri("http://localhost:54321/BlaBlaBla/");
    
            // 3rd Create a UnityServiceHost instance
            var myService = new UnityServiceHost(ServiceHostController.UnityContainer, typeof(MyService), baseAddress);
    
            try
            {  //etcetera...
    

    And it worked for me, I still have to refactor some things and add features and methods but the starting point works like a charm.

    I hope it helps.

提交回复
热议问题