c# - IoC and Unity, where do I call the method that registers the types -


i try learn , use ioc unity in web services can't figure out how or should call method calls registertype methods.

in pseudo code try clarify question.

[webmethod] public void dosomething() {     var obj = ioc.resolve<isomeinterface>();     obj.container.dosomething(); }  interface isomeinterface {     void dosomething(); }  public class someclass : isomeinterface {     pulic void dosomething()     {         //do here     } }  public static class ioc {     public static iunitycontainer container = new unitycontainer();      public static void init()     {         container.registertype<isomeinterface, someclass>();     } } 

i can't figure out how or should call method init() in example above. try in project created following next steps. create new empty asp.net web application in visual studio 2012. add web service item , in .asmx.cs file webmethod. webservice called ajax in javascript.

what need webservice call init method when starts?

you can perform registration in ioc's static constructor it's automatically performed before/when hit container field first time:

public static class ioc {     public static readonly iunitycontainer container;      static ioc()     {         iunitycontainer container = new unitycontainer();         container.registertype<isomeinterface, someclass>();         container = container;     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -