0

I'm trying to Use this Blog for WCF RIA Applcation . So I Create a Silverlight Nevigation Applciation which gave me 2 projects abs & abs.Web

More I create 3 project in solution :

  1. abs.Data (c#), DbContext Implemeantion of Repository Interfaces +Factory to provide What user want.
  2. abs.Data.Contarct (c#) Interfaces for Operations Repository
  3. abs.Data.Model (c#) - Contains POCO - EF 6

Now I created A wcf Service in abs.Web project which have constructor injection of a Repository to get my job done in operation contracts. So I tried using Unity here under the guidence with below blog

http://jamesheppinstall.wordpress.com/2012/06/20/windows-communication-foundation-resolving-wcf-service-dependencies-with-unity/

Now I'm getting

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.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

**Stack Trace: **

[InvalidOperationException: 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.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 

My all classes are same as like Blog.

akirti
  • 179
  • 2
  • 15

1 Answers1

0

WCF Ria has its own factory.
Simply put

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
}

and somewhere on your bootstrapping code add

 DomainService.Factory = new MyDomainServiceFactory();

of course, the word IoC in the factory identify the unityContainer (is actually a static façade against it)

mCasamento
  • 1,413
  • 1
  • 11
  • 21