Dependency Injection into .asmx

2,144 views
Skip to first unread message

kos...@gmail.com

unread,
Feb 17, 2015, 3:43:40 AM2/17/15
to aut...@googlegroups.com
Hello, I am trying to figured out is threre a way to instantiate a instance of WebService through Autofact with some DI of it constructor? Because of now if you are using for example

public class MyWebService : WEbService
{

}

Alex Meyer-Gleaves

unread,
Feb 17, 2015, 6:17:40 AM2/17/15
to aut...@googlegroups.com
Hi Alexey,

I'm going to respond to your email here for the benefit of others.

Hello, Alex.
I am trying figured out is there a way to implement DI with Autofac into Web Service?
Based on my investigation, we can made a DI into property, but unfortunately into constructor we can't due to No parameterless constructor defined for this object. Because WebService required default constructor! But I found that another DI frameworks for example Spring.Net supports injections into constructor! You can find such sample by http://www.gitshah.com/2011/11/integrating-springnet-with-asmx.html. SO my question is why Autofac does't support similar thing to do?
Thank's for you answer!

The SoapHttpClientProtocol based proxies have never supported DI natively. Making this work is really not worth the effort for us given how little interest there is in ASMX web services.

To make it work Spring.NET had to implement a dynamic proxy, custom HTTP handler, and custom configuration of the web service.


A simpler approach would be to use service location in the web service and have it call directly through to the real implementation. Service location is generally an anti-pattern but can be acceptable in situations such as these when the use is kept to a minimum. Essentially the web service would be a small shim that delegates all work to the service implementation resolved from the container.

There is an Autofac implementation of CommonServiceLocator that you could use to access the container.


The end result would look something like this.

public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
var service = ServiceLocator.Current.GetInstance<IMyBusinessService>();
        return service.HelloWorld();
    } 
}

public class MyBusinessService : IMyBusinessService
{
readonly IDependency dependency;
public MyBusinessService(IDependency dependency)
{
this.dependency = dependency;
}
    public string HelloWorld()
    {
        return dependency.SayHello();
    }
}

If you want anything more than that you are going to be in for a lot work.

Cheers,

Alex.

jackm...@hotmail.com

unread,
Jul 14, 2015, 12:44:39 AM7/14/15
to aut...@googlegroups.com
El martes, 17 de febrero de 2015, 3:43:40 (UTC-5), Alexey Borodenko escribió:
> Hello, I am trying to figured out is threre a way to instantiate a instance of WebService through Autofact with some DI of it constructor? Because of now if you are using for example
>
> public class MyWebService : WEbService
> {
>
> }

Hi Alex Meyer,
I am trying with webforms:


public partial class MyWebPage : Page{

IService _myService;

public MyWebPage(IService myService){
_myService = myService;
}
[WebMethod(true)]
public static string SayHello(){
var response = _myService.DoSomething();// has error
...
...
return response;
}
}

what do you recomend?

Alex Meyer-Gleaves

unread,
Jul 15, 2015, 8:20:13 AM7/15/15
to aut...@googlegroups.com, jackm...@hotmail.com, jackm...@hotmail.com
I am not exactly sure what your question is or what problem you are having. You can find more information about the WebForms integration in the documentation.

http://docs.autofac.org/en/latest/integration/webforms.html

As far injecting services into your ASMX web service implementation goes the recommendation above is the only option.


Kendall Bennett

unread,
Jul 15, 2015, 11:14:40 AM7/15/15
to <autofac@googlegroups.com>, jackm...@hotmail.com
The simplest solution I have used to inject into environments that don't natively support DI is to use property injection and then derive all my real classes from a common base class that calls Autofac to inject the properties at construction time. Then all your derived classes look and feel like they support native DI, but in reality they are self injecting.

I do this for MVC views right now because the current support (at least last time I looked) did not support injecting anything into layouts. But you could use the same approach for WinForms or web services etc.

Regards,

Sent from my iPad Air!

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autofac+u...@googlegroups.com.
To post to this group, send email to aut...@googlegroups.com.
Visit this group at http://groups.google.com/group/autofac.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages