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

bri*_*ien 6 c# silverlight unity-container wcf-ria-services

我在一个带有一些Silverlight组件的ASP.Net应用程序中使用.Net RIA Services的July CTP.我在Silverlight上打电话给RIA服务.

当我尝试在我的域服务(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;
    }

    ...
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

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
Run Code Online (Sandbox Code Playgroud)

Nik*_*ari 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!