locked
MVC3, Unity using WCF hosted on iis RRS feed

  • Question

  • User-1600012733 posted

    Hello,

    I am using WCF serice having both json & soap end points hosted on iis.  I want to use Unity in MVC3. Really apprecaite if you could provide good example for me.

    Thanks

     

    Friday, August 24, 2012 12:22 PM

Answers

  • User-1916342745 posted

    Not sure about a good example. I've knocked together a really quick example to get you started

    1. in the web.config (One of the nice things about unity is that you can setup in web.config if you like)

    <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
    </configSections>

    ... then ... 

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!---->
    <assembly name="UnityTest" />
    <assembly name="Interfaces" />
    <namespace name="Interfaces" />
    <assembly name="CanDoV2" />
    <namespace name="CanDoV2" />
    <container>
    <register type="ICanDo" mapTo="CanDo" />
    </container>

    </unity>

    2. Create a simple helper class... 

    public class UnityHelper
    {
    public static UnityContainer container = new UnityContainer();

    static UnityHelper()
    {
    //container.RegisterType<ICanDo, CanDoV1.CanDo>();
    container.LoadConfiguration();
    }

    public static T Resolve<T>()
    {
    return container.Resolve<T>();
    }
    }

    3. Use !

    public class HomeController : Controller
    {
    ICanDo cando;
    public HomeController(ICanDo canDo)
    {
    this.cando = canDo;
    }

    public HomeController() : this(UnityHelper.Resolve<ICanDo>()) { }

    public ActionResult Index(string name)
    {
    ViewBag.Message = cando.greeting(name);
    return View();
    }

    public ActionResult About()
    {
    return View();
    }
    }

    notes.. this is a based on a simple interface

    public interface ICanDo
    {
    string greeting(string name);
    }

    Hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2012 10:45 AM

All replies

  • User-1600012733 posted

    Common guys, no one to help here?

    Friday, August 24, 2012 3:12 PM
  • User-1662538993 posted

    I do not know much about unity but you can check this link if you havent-

    http://www.devtrends.co.uk/blog/integrating-the-unity.mvc3-1.1-nuget-package-from-scratch

    http://unitymvc3.codeplex.com/

    Monday, August 27, 2012 10:27 AM
  • User-37275327 posted

    For better answere post on MVC or WCF sections.

    Tuesday, August 28, 2012 1:55 AM
  • User-1916342745 posted

    Not sure about a good example. I've knocked together a really quick example to get you started

    1. in the web.config (One of the nice things about unity is that you can setup in web.config if you like)

    <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
    </configSections>

    ... then ... 

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!---->
    <assembly name="UnityTest" />
    <assembly name="Interfaces" />
    <namespace name="Interfaces" />
    <assembly name="CanDoV2" />
    <namespace name="CanDoV2" />
    <container>
    <register type="ICanDo" mapTo="CanDo" />
    </container>

    </unity>

    2. Create a simple helper class... 

    public class UnityHelper
    {
    public static UnityContainer container = new UnityContainer();

    static UnityHelper()
    {
    //container.RegisterType<ICanDo, CanDoV1.CanDo>();
    container.LoadConfiguration();
    }

    public static T Resolve<T>()
    {
    return container.Resolve<T>();
    }
    }

    3. Use !

    public class HomeController : Controller
    {
    ICanDo cando;
    public HomeController(ICanDo canDo)
    {
    this.cando = canDo;
    }

    public HomeController() : this(UnityHelper.Resolve<ICanDo>()) { }

    public ActionResult Index(string name)
    {
    ViewBag.Message = cando.greeting(name);
    return View();
    }

    public ActionResult About()
    {
    return View();
    }
    }

    notes.. this is a based on a simple interface

    public interface ICanDo
    {
    string greeting(string name);
    }

    Hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2012 10:45 AM