简体   繁体   English

.Net RIA服务:DomainService需要一个无参数的构造函数?

[英].Net RIA Services: DomainService Needs a Parameterless Constructor?

I'm using the July CTP of .Net RIA Services in an ASP.Net application with some Silverlight components. 我在一个带有一些Silverlight组件的ASP.Net应用程序中使用.Net RIA Services的July CTP。 I'm calling the RIA Services from Silverlight. 我在Silverlight上打电话给RIA服务。

My problem arose when I tried to use Unity and constructor dependency injection in my Domain Service (a LinqToEntitiesDomainService object). 当我尝试在我的域服务(LinqToEntitiesDomainService对象)中使用Unity和构造函数依赖注入时出现了我的问题。 The Silverlight application now complains about not having a parameterless constructor. Silverlight应用程序现在抱怨没有无参数构造函数。

I don't want to have a parameterless constructor, I want Unity to resolve the constructor arguments. 我不想拥有无参数构造函数,我希望Unity解析构造函数参数。 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. 由于您的ctor中有一个带有参数的DomainService,并且通常需要通过某种IoC容器或依赖注入系统构建,因此您需要提供应用级域服务工厂。 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. 然后,您的工厂负责实例化域服务(并处理它),并且可以通过调用另一个API(例如Unity)来实现。

Here's a basic example: 这是一个基本的例子:

In Global.asax.cs of your app, add the following: 在应用的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();
    }
}

Hope that helps! 希望有所帮助!

@Brien, I assume the 'IUserService' depends on IUnitOfWork, where the IUnitOfWork is the DashboardEntities ? @Brien,我假设'IUserService'依赖于IUnitOfWork,其中IUnitOfWork是DashboardEntities?

Like this UserRepository : 像这个UserRepository

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

    ...
}

And this IUnitOfWork : 而这个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'm using this design. 我正在使用这个设计。 One thing I noticed is that the DashboardEntities class is created more than once. 我注意到的一件事是DashboardEntities类是多次创建的。 The first time it's created by Unity (and will only be created once because it's declared as an Singleton in the Unity Configuration). 它是第一次由Unity创建(并且只会创建一次,因为它在Unity配置中被声明为Singleton)。

But the next time, it seems that a new DashboardEntities class is created during the initialize from the DomainService (DashboardService) ? 但是下一次,似乎在从DomainService(DashboardService)初始化期间创建了一个新的DashboardEntities类? This is no big deal because the DomainService will not use this ObjectContext, it will use the ObjectContext which is injected by Unity in the Repositories. 这没什么大不了的,因为DomainService不会使用这个ObjectContext,它将使用Unity在存储库中注入的ObjectContext。

Can someone confirm this design or show some more light on this issue ? 有人可以确认这个设计或在这个问题上显示更多的亮点吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是 RIA Services DomainService 类 - What layer are the RIA Services DomainService classes Dapper 需要一个无参数的构造函数? - Dapper needs a parameterless constructor? 使用ASP.NET Auth和带有Silverlight的DomainService保护WCF服务 - Securing WCF services using ASP.NET Auth and a DomainService with Silverlight Protobuf-net“找不到二进制的无参数构造函数” - Protobuf-net “No parameterless constructor found for Binary” Silverlight RIA DomainService在表中具有200万行 - Silverlight RIA DomainService with 2 million rows in a table .NET Web Api 2.1中Ninject绑定的无参数构造函数错误 - Parameterless constructor error with Ninject bindings in .NET Web Api 2.1 在ASP.NET MVC中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in ASP.NET MVC ASP.NET MVC 4 + Ninject MVC 3 = 没有为此对象定义无参数构造函数 - ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object 套接字上的 Protobuf-net 序列化。 未找到无参数构造函数 ProtoException - Protobuf-net serialization on sockets. No parameterless constructor found ProtoException 在 asp.net web 服务中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in asp.net web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM