Injecting Dependencies into WCF and ASMX Web Service Implementations

Topics: Web Client Software Factory, User Forum
Jun 25, 2007 at 4:47 PM
Is there a way to get the CWAB and ObjectBuilder to inject dependencies into WCF and ASMX Service Implementations that are a part of a WCSF Solution? The Reference Implementation does not show connecting a web service to a business module or foundation module.

Ideally you would want to use a [CreateNew] and/or [ServiceDependency] Attribute within the Service Implementation to hook into controllers or global ( and possibly local ) services within the application.

Without having the dependency injection in the web services, it nows becomes necessary to create all the classes and wire up the dependencies manually, which ends up defeating the purpose of using Dependency Injection in the modules.

Regards,

Dave

_______________________________

David Hayden
Microsoft MVP C#
Jun 26, 2007 at 2:42 PM

DavidHayden wrote:
Is there a way to get the CWAB and ObjectBuilder to inject dependencies into WCF and ASMX Service Implementations that are a part of a WCSF Solution? The Reference Implementation does not show connecting a web service to a business module or foundation module.

Ideally you would want to use a [CreateNew] and/or [ServiceDependency] Attribute within the Service Implementation to hook into controllers or global ( and possibly local ) services within the application.

Without having the dependency injection in the web services, it nows becomes necessary to create all the classes and wire up the dependencies manually, which ends up defeating the purpose of using Dependency Injection in the modules.

Regards,

Dave

_______________________________

David Hayden
Microsoft MVP C#


David,
I am curious do other dependency injection frameworks (like castle) allow this? Basically where they dont care what type of classes you have, but if you have them decorated with attributes then the dependency injection framework will take care of it.

Thanks
Jun 26, 2007 at 4:04 PM
Yes, but Castle doesn't need to use attributes. It will automatically search constructors / properties and inject dependencies.

I wrote a post awhile back that used Windsor instead of ObjectBuilder for dependency injection like the WCSF. I would do it a bit differently now, but the post might be interesting to you. In this case, I added the attributes just to stay similar to ObjectBuilder.

Wire-Up View-Presenter Pattern Like Web Client Software Factory - Castle Windsor for Dependency Injection

Windsor is a far better container than what we currently have with WCSF. I am hoping in WCSF v2.0, however, we will see vast improvements in the WCSF Container.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
Jul 3, 2007 at 6:56 PM
I am still looking for an answer to the problem of either injecting dependencies into my WCF Services or somehow being able to service locate them via the Composite Web Application Block / ObjectBuilder.

Is there any real guidance on this topic?

Regards,

Dave

_________________________________

David Hayden
Microsoft MVP C#
Jul 4, 2007 at 2:56 PM
Check this David.
http://feeds.feedburner.com/~r/Orand/~3/41233244/wcf-service-dependency-injection.html

Matias
http://staff.southworks.net/blogs/matiaswoloski
Jul 4, 2007 at 3:00 PM
Hi David,

Since you are on this topic, and I am checking out your sample codes from http://www.pnpguidance.net/ website, I wonder is it possible to use the Data Access Software Factory from WSSF inside WCSF to automate generation of stored procs and business entities object. What are the sort of modification need on the project (or even WCSF) itself to have this enabled? I currently find coding your own Biz Entities is a waste of productivity.

-Patrick
Jul 4, 2007 at 4:52 PM
I will read over that during the weekend, Matias. Looks like some interesting reading.

Thanks,

Dave

_____________________________

David Hayden
Microsoft MVP C#
Jul 4, 2007 at 5:03 PM
Patrick,

Yes, you can use the Data Access Guidance Package from the Web Service Software Factory in your WCSF Projects.

If you have it installed, just go to the Tools Menu in Visual Studio and select Guidance Package Manager > Enable / Disable Packages . Check the Web Service Software Factory ( Data Access ) Guidance Package and OK. That will enable the Data Access Guidance Package.

From there you will need to specify project responsibilities and then just use the recipes in your WCSF application.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
Jul 5, 2007 at 12:34 AM
Patrick,

I wrote a post that has some graphics that may be a bit clearer:

Enable Data Access Guidance Package in Web Client Software Factory Solution - Code Generation

Regards,

Dave

_________________________________

David Hayden
Microsoft MVP C#
Jul 6, 2007 at 4:37 AM
Hello David,
I am a regular reader of your blog and learning a lot from it, when I read one of your posts about “Enable Data Access Guidance Package in Web Client Software Factory Solution ” , you have mentioned that “You could also use it for your applications, but let me just say there are far better solutions out there that can provide you a more maintainable and feature-rich data access layer” . Please can you write a details post about it. It will be very helpful to us.
thanks in advance...

Regards,
Nagarajan.
Jul 7, 2007 at 12:06 AM

DavidHayden wrote:
Is there a way to get the CWAB and ObjectBuilder to inject dependencies into WCF and ASMX Service Implementations that are a part of a WCSF Solution? The Reference Implementation does not show connecting a web service to a business module or foundation module.

Ideally you would want to use a [CreateNew] and/or [ServiceDependency] Attribute within the Service Implementation to hook into controllers or global ( and possibly local ) services within the application.

Without having the dependency injection in the web services, it nows becomes necessary to create all the classes and wire up the dependencies manually, which ends up defeating the purpose of using Dependency Injection in the modules.



Please check out the latest weekly drop - we took a stab at this. It's a little ugly, but it works. Please let us know what you think.
Jul 1, 2008 at 12:10 PM


ctavares wrote:

DavidHayden wrote:
Is there a way to get the CWAB and ObjectBuilder to inject dependencies into WCF and ASMX Service Implementations that are a part of a WCSF Solution? The Reference Implementation does not show connecting a web service to a business module or foundation module.

Ideally you would want to use a [CreateNew] and/or [ServiceDependency] Attribute within the Service Implementation to hook into controllers or global ( and possibly local ) services within the application.

Without having the dependency injection in the web services, it nows becomes necessary to create all the classes and wire up the dependencies manually, which ends up defeating the purpose of using Dependency Injection in the modules.



Please check out the latest weekly drop - we took a stab at this. It's a little ugly, but it works. Please let us know what you think.


I know that this topic is old, but did the latest weekly drop work?
Can a SCSF business module be loaded from within a webservice (using dependency injection etc)

"Just replacing the ShellApplication with a Webservice", it sounds so easy!
Coordinator
Jul 2, 2008 at 3:39 PM
DI does work for ASMX web services.  We included a sample in the Orders Management RI in the postal data web service.  For WCF services, things are a bit more interesting:  there are a few solutions I have read about, but I have not tried any of them yet.
I hope that helps,

Michael Puleio -- patterns & practices
http://blogs.msdn.com/mpuleio

Oct 7, 2008 at 11:30 AM
Hello Michael,
I have looked into Orders Management RI example. when I inject controller into my asmx webservice I got exception:
Unable to cast object of type 'System.Web.HttpApplication' to type 'Microsoft. Practices.CompositeWeb.WebClientApplication'

My code looks like this:
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class Service1 : WebService
    {

        [ServiceDependency]
        public IWebAdminController Controller
        {
            get { return m_controller; }
            set { m_controller = value; }
        }
        public IWebAdminController m_controller;

        public Service1()
        {
            try
            {
                WebClientApplication.BuildItemWithCurrentContext(this);
            }
            catch (Exception e)
            {
           
            }

        }

        [WebMethod]
        public CompanyCollection HelloWorld()
        {
            return m_controller.GetCompanies();
        }
    }

Am I missing something i web.config file? What am I doing wrong?
Regards
Bartek