I'm using the July CTP of .Net RIA Services in an ASP.Net application with some Silverlight components. I'm calling the RIA Services from Silverlight.
My problem arose when I tried to use Unity and constructor dependency injection in my Domain Service (a LinqToEntitiesDomainService object). The Silverlight application now complains about not having a parameterless constructor.
I don't want to have a parameterless constructor, I want Unity to resolve the constructor arguments. Is this possible? Am I doing something wrong? Or should I find another way to inject my constructor arguments?
public class DashboardService : LinqToEntitiesDomainService<DashboardEntities>
{
private IUserService userService;
public DashboardService(IUserService userService)
: base()
{
if (userService == null)
{
throw ExceptionBuilder.ArgumentNull("userService");
}
this.userService = userService;
}
...
Here's the error I'm getting:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Timestamp: Tue, 18 Aug 2009 14:34:54 UTC
Message: Unhandled Error in Silverlight 2 Application No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Web.DomainServices.DomainService.DefaultDomainServiceFactory.CreateDomainService(Type domainServiceType, DomainServiceContext context)
at System.Web.Ria.DataServiceFactory.GetDataService(HttpContext context)
at System.Web.Ria.DataServiceFactory.System.Web.IHttpHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
Line: 1
Char: 1
Code: 0
URI: http://dev.localhost/Home
Since you have a DomainService with a parameter in its ctor, and more generally needs to be constructed through some sort of IoC container or dependency injection system, you'll need to provide an app-level domain service factory. Your factory is then responsible for instantiating the domain service (and disposing it), and it can do so by calling into another API, such as Unity in your case.
Here's a basic example:
In Global.asax.cs of your app, add the following:
public class Global : HttpApplication {
static Global() {
DomainService.Factory = new MyAppDomainServiceFactory();
}
}
internal sealed class MyAppDomainServiceFactory : IDomainServiceFactory {
public DomainService CreateDomainService(Type domainServiceType,
DomainServiceContext context) {
DomainService ds = ... // code to create a service, or look it up
// from a container
if (ds != null) {
ds.Initialize(context);
}
return ds;
}
public void ReleaseDomainService(DomainService domainService) {
// any custom logic that must be run to dispose a domain service
domainService.Dispose();
}
}
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With