【问题标题】:WCF RIA Service with Unity 3.5.1.0带有 Unity 3.5.1.0 的 WCF RIA 服务
【发布时间】:2014-07-02 18:40:29
【问题描述】:

我正在尝试将此博客用于 WCF RIA 应用程序。所以我创建了一个 Silverlight 导航应用程序,它给了我 2 个项目 abs 和 abs.Web

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

  1. abs.Data (c#), DbContext 实现存储库接口 +Factory 以提供用户想要的。
  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 

我所有的类都和博客一样。

【问题讨论】:

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


    【解决方案1】:

    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(实际上是对它的静态外观)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 2011-01-14
      相关资源
      最近更新 更多