使用Unity 3.5.1.0的WCF RIA服务

时间:2014-07-02 18:40:30

标签: wcf unity-container wcf-ria-services prism-4

我试图将此博客用于W​​CF RIA Applcation。所以我创建了一个Silverlight Nevigation Applciation,它给了我2个项目abs& abs.Web

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

  1. abs.Data(c#),DbContext实现存储库接口+工厂以提供用户想要的东西。
  2. abs.Data.Contarct(c#)操作存储库的接口
  3. abs.Data.Model(c#) - 包含POCO - EF 6
  4. 现在我在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 
    

    我的所有课程都与Blog一样。

1 个答案:

答案 0 :(得分: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(实际上是对它的静态外观)