.Net RIA Services: DomainService needs a parameterless constructor?

I am using the July CTP.Net RIA Services in an ASP.Net application with some Silverlight components. I'm calling RIA services from Silverlight.

My problem arose when I tried to use the Unity and constructor dependency injection in my domain service (LinqToEntitiesDomainService object). The Silverlight application now complains about the absence of a constructor without parameters.

I donโ€™t want to have a constructor without parameters, I want Unity to allow constructor arguments. Is it possible? Am I doing something wrong? Or do I need to find another way to enter 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 is the error I get:

 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 
+6
c # silverlight unity-container wcf-ria-services
source share
2 answers

Since you have a DomainService with a parameter in its ctor and, as a rule, you need to construct it through some IoC container or dependency injection system, you need to provide the domain domain service factory. Then your factory is responsible for creating the instance of the domain service (and deleting it), and it can do this by calling in another API, for example Unity in your case.

Here is a basic example:

In the Global.asax.cs application of your application, 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 this helps!

+12
source share

@Brien, I assume that "IUserService" depends on IUnitOfWork, where is IUnitOfWork - is it DashboardEntities?

Like this UserRepository :

 public class UserRepository : BaseRepository<User>, IUserRepository { protected BaseRepository(IUnitOfWork unitOfWork) { } ... } 

And this IUnitOfWork :

 public partial class DashboardEntities : ObjectContext, IUnitOfWork { public const string ConnectionString = "name=DashboardEntities"; public const string ContainerName = "DashboardEntities"; public DashboardEntities() : base(ConnectionString, ContainerName) { this.ContextOptions.LazyLoadingEnabled = true; } ... } 

I use this design. One thing that I noticed is that the DashboardEntities class is instantiated multiple times. The first time it was created by Unity (and will only be created once since it is declared as Singleton in the Unity configuration).

But next time it seems that during initialization, a new DashboardEntities class is created from DomainService (DashboardService)? This does not really matter, because the DomainService will not use this ObjectContext, it will use the ObjectContext, which is introduced by Unity in the repository.

Can someone confirm this design or show more light on this issue?

0
source share

All Articles