2009-08-18 73 views
6

我在一些Silverlight组件的ASP.Net应用程序中使用.Net RIA Services的七月CTP。我从Silverlight调用RIA服务。.Net RIA服务:DomainService需要一个无参数构造函数?

当我尝试在我的域服务(LinqToEntitiesDomainService对象)中使用Unity和构造函数依赖项注入时,出现了我的问题。 Silverlight应用程序现在抱怨没有无参数的构造函数。

我不想有无参数的构造函数,我想要Unity来解析构造函数参数。这可能吗?难道我做错了什么?或者我应该找到另一种方法来注入我的构造函数参数?

public class DashboardService : LinqToEntitiesDomainService<DashboardEntities> 
{ 
    private IUserService userService; 

    public DashboardService(IUserService userService) 
     : base() 
    { 
     if (userService == null) 
     { 
      throw ExceptionBuilder.ArgumentNull("userService"); 
     } 
     this.userService = userService; 
    } 

    ... 

这里是我得到的错误:

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 

回答

12

既然你有一个的DomainService在其构造函数的参数,一般多需要通过某种IoC容器或依赖注入的构造系统,您需要提供应用程序级别的服务工厂。然后,您的工厂负责实例化域服务(并对其进行处置),并且可以通过调用另一个API(如您的案例中的Unity)来执行此操作。

这里有一个基本的例子:

在您的应用程序的Global.asax.cs,添加以下内容:

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(); 
    } 
} 

希望帮助!

+0

我一直在寻找它。感谢那 :) – Davita 2011-04-21 11:44:00

0

@布莱恩,我认为'IUserService'取决于IUnitOfWork,其中IUnitOfWork是DashboardEntities?

像这样UserRepository

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

    ... 
} 

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; 
    } 

    ... 
} 

我使用这种设计。 我注意到的一件事是DashboardEntities类是多次创建的。 第一次由Unity创建(并且由于它在Unity配置中声明为Singleton,因此只会创建一次)。

但是下一次,似乎有一个新的DashboardEntities类是在从DomainService(DashboardService)初始化期间创建的? 这没什么大不了的,因为DomainService不会使用这个ObjectContext,它会使用由Unity在Repositories中注入的ObjectContext。

有人可以确认这个设计或在这个问题上显示更多的光?

相关问题