使用Unity 3.5.1.0的WCF RIA服务

使用Unity 3.5.1.0的WCF RIA服务,wcf,unity-container,wcf-ria-services,prism-4,Wcf,Unity Container,Wcf Ria Services,Prism 4,我正在尝试使用这个博客进行WCF RIA应用。所以我创建了一个Silverlight Nevigation应用程序,它给了我两个项目abs和abs.Web 更多信息我在解决方案中创建了3个项目: abs.datac,DbContext实现存储库接口+工厂,以提供用户所需。 abs.Data.Contarct c操作库接口 abs.Data.c型-包含POCO-EF 6 现在,我在abs.Web项目中创建了一个wcf服务,其中包含一个存储库的构造函数注入,以在操作合同中完成我的工作。 所以我试着在

我正在尝试使用这个博客进行WCF RIA应用。所以我创建了一个Silverlight Nevigation应用程序,它给了我两个项目abs和abs.Web

更多信息我在解决方案中创建了3个项目:

abs.datac,DbContext实现存储库接口+工厂,以提供用户所需。 abs.Data.Contarct c操作库接口 abs.Data.c型-包含POCO-EF 6 现在,我在abs.Web项目中创建了一个wcf服务,其中包含一个存储库的构造函数注入,以在操作合同中完成我的工作。 所以我试着在下面的博客中使用Unity

现在我开始

无法将提供的服务类型作为服务加载,因为它没有默认的无参数构造函数。要解决此问题,请向该类型添加默认构造函数,或将该类型的实例传递给主机

描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.InvalidOperationException:无法将提供的服务类型作为服务加载,因为它没有默认的无参数构造函数。要解决此问题,请向该类型添加默认构造函数,或将该类型的实例传递给主机

源错误:

在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别

**堆栈跟踪:**

[InvalidOperationException:无法将提供的服务类型作为服务加载,因为它没有默认的无参数构造函数。若要解决此问题,请向该类型添加默认构造函数,或将该类型的实例传递给主机。]

  System.ServiceModel.Dispatcher.InstanceBehavior..ctor(DispatchRuntime dispatch, ImmutableDispatchRuntime immutableRuntime) +12761206
  System.ServiceModel.Dispatcher.ImmutableDispatchRuntime..ctor(DispatchRuntime dispatch) +173
 System.ServiceModel.Dispatcher.DispatchRuntime.GetRuntimeCore() +85
 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpened() +148
 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +321
 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +139
 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +310
 System.ServiceModel.Channels.CommunicationObject.Open() +36
 System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +91
 System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +598

[ServiceActivationException: The service '/DomainServices/UserWcfService.svc' cannot be activated due to an exception during compilation.  The exception message is: The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host..]
 System.Runtime.AsyncResult.End(IAsyncResult result) +499812
 System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +178
 System.ServiceModel.Activation.ServiceHttpHandler.EndProcessRequest(IAsyncResult result) +6
 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129




 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET      Version:4.0.30319.18446 

我的所有课程都和like Blog一样。

WCF Ria有自己的工厂。 简而言之

public class MyDomainServiceFactory : IDomainServiceFactory
{

    #region IDomainServiceFactory Members

    public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
    {
        try
        {
            if (!typeof (DomainService).IsAssignableFrom(domainServiceType))
            {
                throw new ArgumentException(String.Format("Cannot create an instance of {0} since it does not inherit from DomainService", domainServiceType), "domainServiceType");
            }

            if (IoC.IsRegistered(domainServiceType))
            {

                var dmnService = (DomainService) IoC.Resolve(domainServiceType);
                dmnService.Initialize(context);
                return dmnService;
            }
            else
            {
                //if the IoC container doesn't know the service, simply try to call its default constructor
                //could throw proper exception as well
                var dmnService = (DomainService) Activator.CreateInstance(domainServiceType);
                dmnService.Initialize(context);
                return dmnService;
            }
        }
        catch (Exception ex)
        {
            ExceptionHandler.HandleException(ex);
            return null;
        }

    }


    public void ReleaseDomainService(DomainService domainService)
    {
        domainService.Dispose();
    }

    #endregion
}
在引导代码的某个地方添加

 DomainService.Factory = new MyDomainServiceFactory();
当然,工厂中的IoC这个词表示unityContainer实际上是一个静态的外观