To initialize the container and populate the dependencies of a Web service application, you must expose user interfaces (such as Windows Forms, WPF, and ASP. NET web forms) in different methods of the application type ). This topic describes possible solutions and resources for ASP. NET web services (asmx), and helps you implement them in WCF applications.
ASP. NET Web Service Application
ASP. NET web service applications can partially use methods in the topic ASP. NET web forms application. You can add the extension method of httpapplicationstate to create and publish containers, and add the code to the Global. asax file of the application to add the Enterprise Library extension to the container. However, because there are no UI controls in asmx applications, you cannot use the HTTP module as described in ASP. NET web forms applications.
However, you can call the httpcontext. Current. application. getcontainer () Extension Method to access the container. You can useBuildupThe method fills in the dependencies you define through Properties.For example, the following code uses dependency injection to solve the problemImyserviceAnd use it to calculateUsedataserviceValue returned by the method.
Using system. web; </P> <p> public class mywebservice: system. web. services. webService <br/>{< br/> private imyservice theservice; </P> <p> Public mywebservice () <br/>{< br/> // pass this class through the container using the buildup method <br/> httpcontext. current. application. getcontainer (). buildup (this ); <br/>}</P> <p> [dependency] <br/> Public imyservice dataservice <br/>{< br/> get {return theservice ;} <br/> set {theservice = value ;}< br/>}</P> <p> [webmethod] <br/> Public String usedataservice () <br/>{< br/> return "the value you require is" + theservice. dosomething (); <br/>}< br/>
You can use the basic methods displayed in the ASP. NET web forms application to create and assembleApplicationThe container in the dictionary object, and then access it through your web method when necessary.
WCF Application
In WCF, you can use technologies similar to the basic principles described in this topic. However, there is no stored httpapplication status in WCF. Instead, you can useInstancecontextmodeOfInstancecontextIt is set as a singleton and applies to all requests. You must implementIextensionInterfaceInstancecontextCustom extension.