2014-07-02 98 views
0

我正在嘗試使用此WCF RIA Applcation的博客。所以,我創建一個Silverlight Nevigation Applciation這給了我2個項目腹肌& abs.WebWCF使用Unity的3.5.1.0 RIA服務

更多我創建解決方案3項目:

  1. abs.Data(C#)的DbContext Implemeantion庫接口的+工廠以提供用戶想要的東西。
  2. abs.Data.Contarct(C#)接口,用於操作庫
  3. abs.Data.Model(C#) - 包含POCO - EF 6

現在,我創建了abs.Web項目WCF服務這有一個倉庫的構造函數注入來讓我的工作在操作合同中完成。 所以我嘗試使用Unity這裏指導做好在下面博客

http://jamesheppinstall.wordpress.com/2012/06/20/windows-communication-foundation-resolving-wcf-service-dependencies-with-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 

我所有的類都是一樣喜歡博客。

回答

0

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(實際上是針對一個靜態的外觀)

+0

好的,讓我試試這個代碼。 – akirti