.NET RIA服务:需要DomainService的一个参数的构造函数? [英] .Net RIA Services: DomainService Needs a Parameterless Constructor?

查看:254
本文介绍了.NET RIA服务:需要DomainService的一个参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的.Net RIA服务的CTP七月与一些Silverlight组件的ASP.Net应用程序。我是从Silverlight中调用RIA服务。



当我试图用团结和构造函数依赖注入在我的域名服务(一LinqToEntitiesDomainService对象)我的问题又出现了。 Silverlight应用程序现在抱怨没有参数的构造函数。



我不希望有一个参数的构造函数,我要团结,以解决构造函数的参数。这可能吗?难道我做错了什么?或者我应该找到另一种方式来注入我的构造函数的参数

 公共类DashboardService:LinqToEntitiesDomainService< DashboardEntities> 
{
私人IUserService userService;

公共DashboardService(IUserService userService)
:基地()
{
如果(userService == NULL)
{
扔ExceptionBuilder。 ArgumentNull(userService);
}
this.userService = userService;
}

...

下面是我的错误m到处:

 网页错误的详细信息

用户代理:Mozilla的/ 4.0(兼容; MSIE 8.0; Windows NT的5.2;三叉戟/ 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)
时间戳:星期二,2009年8月18日14时34分54秒UTC


消息:在Silverlight 2应用程序处理错误此无参数的构造函数定义目的。在System.RuntimeTypeHandle.CreateInstance(RuntimeType型,布尔publicOnly,布尔NOCHECK,布尔和放大器; canBeCached,RuntimeMethodHandle&安培;构造函数,布尔和放大器; bNeedSecurityCheck)
在System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔fillCache)
。在System.RuntimeType.CreateInstanceImpl(布尔publicOnly,布尔skipVisibilityChecks,布尔fillCache)
在System.Activator.CreateInstance(类型类型,布尔非公开)
在System.Web.DomainServices.DomainService.DefaultDomainServiceFactory.CreateDomainService(类型在System.Web.Ria.DataServiceFactory.GetDataService domainServiceType,DomainServiceContext上下文)
(在HttpContext的背景下System.Web.Ria.DataServiceFactory.System.Web.IHttpHandlerFactory.GetHandler)
(HttpContext的背景下,字符串请求类型, URL字符串,字符串pathTranslated)
线:1
字符:1
码:0
网址:HTTP://dev.localhost/Home


解决方案

既然你有一个的DomainService在其构造函数的参数,一般多需要构建通过某种IoC容器或依赖注入系统,你需要提供应用级域服务工厂。你的工厂则实例化域服务(和处置它)负责,它可以通过调用另一个API,如团结在你的情况这样做。



下面是一个基本的例子:



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

 公共类全球:HttpApplication的{

静态全局(){
DomainService.Factory =新MyAppDomainServiceFactory();
}
}

内部密封类MyAppDomainServiceFactory:IDomainServiceFactory {

公众的DomainService CreateDomainService(类型domainServiceType,
DomainServiceContext上下文){
的DomainService DS = ... //代码来创建一个服务,或者从容器中

查一查
//如果(DS!= NULL){
DS .Initialize(上下文);
}
返回DS;
}

公共无效ReleaseDomainService(的DomainService的DomainService){
//必须运行处置域服务
domainService.Dispose任何自定义逻辑();
}
}



希望帮助!


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!

这篇关于.NET RIA服务:需要DomainService的一个参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆