LINQ to SQL DataContext Creation Problem

Jan 31, 2011 at 5:46 PM

I need to instantiate LINQ to SQL DataContexts from exports. The tricky thing is that the base DataContext requires the connection string passed to the construcctor. Obviously there will be many connection strings available. I've solved this with the code at the end of the message. This has been working, but has recently broken (with no changes to this part of the app) with the message listed at the very, very end of this message. The summary is "because a cycle exists in the dependencies between the exports."

Can someone shed light on this cycle error, or show a different way to get contexts based on exports from imported connection strings?

  [LinqDataContext()]
   public partial class BillingDataContext : ILinqDataContext
   {
      [ImportingConstructor()]
         public BillingDataContext([ImportMany(Constants.ConnectionString)]IEnumerable<Object> connectionStrings , [Import(AllowDefault =true)]System.String dummy ) : base(selectConnectionString(connectionStrings))
         {
      }
      private static  System.String selectConnectionString(IEnumerable<Object> connectionStrings )
      {
         var name = "Billing";
         var connStrings = from x in connectionStrings where x.ToString().Contains(name)  select x;
         if( connStrings.Count() == 1  )
         {
            var connString = connStrings.First();
            return connString.ToString();
         }
         else
         {
            throw new InvalidOperationException();
         }
      }
   }

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

 

1) Cannot compose part '[[]].BillingSqlRepository.BillingDataContext' because a cycle exists in the dependencies between the exports being composed. To break this cycle, consider changing some imports from constructor to property injection.
Element: [[]].BillingSqlRepository.BillingDataContext -->  [[]].BillingSqlRepository.BillingDataContext -->  DirectoryCatalog (Path="C:\Users\Kathleen\Current Projects\[[]]\MVC2011Root\MVCGenerator2011\Budget\Budget\Runtime")

Resulting in: Cannot get export '[[]].BillingSqlRepository.BillingDataContext (ContractName="[[]].Sql.Common.ILinqDataContext")' from part '[[]].BillingSqlRepository.BillingDataContext'.
Element: [[]]].BillingSqlRepository.BillingDataContext (ContractName="[[]].Sql.Common.ILinqDataContext") -->  [[]].BillingSqlRepository.BillingDataContext -->  DirectoryCatalog (Path="C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Budget\Budget\Runtime")

Jan 31, 2011 at 6:17 PM

More info:

I seem to be randomly switching between the previous error and the two errors below. In both this and the previous message, I've replaced my client's namespace with [[]].

----- (and yes, the export exists)

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) The settings property 'AdministrationConnectionString' was not found.

Resulting in: An exception occurred while trying to create an instance of type '[[]].Administration.SqlRepository.AdministrationDataContext'.

Resulting in: Cannot get export '[[]].Administration.SqlRepository.AdministrationDataContext (ContractName="[[]].Sql.Common.ILinqDataContext")' from part '[[]].Administration.SqlRepository.AdministrationDataContext'.
Element: [[]].Administration.SqlRepository.AdministrationDataContext (ContractName="[[]].Sql.Common.ILinqDataContext") -->  [[]].Administration.SqlRepository.AdministrationDataContext -->  DirectoryCatalog (Path="C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Budget\Budget\Runtime")

-----

System.InvalidOperationException was unhandled by user code
  Message=GetExportedValue cannot be called before prerequisite import '[[]].BillingSqlRepository.BillingDataContext..ctor (Parameter="connectionStrings", ContractName="ConnectionString")' has been set.
  Source=System.ComponentModel.Composition
  StackTrace:
       at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.EnsureGettable()
       at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.GetExportedValue(ExportDefinition definition)
       at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
       at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(ComposablePart part, ExportDefinition export, Boolean isSharedPart)
       at System.ComponentModel.Composition.Hosting.CatalogExportProvider.CatalogExport.GetExportedValueCore()
       at System.ComponentModel.Composition.Primitives.Export.get_Value()
       at System.ComponentModel.Composition.ExportServices.GetExportedValueFromLazy[T](Export export)
       at System.ComponentModel.Composition.ExportServices.<>c__DisplayClass4`2.<CreateStronglyTypedExportOfTM>b__1()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Lazy`1.get_Value()
       at [[]].Sql.Common.DataContextFinder.<get_DataContexts>b__0(Lazy`2 x) in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\SqlCommon\DataContextFinder.cs:line 29
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at [[]].Sql.Common.DataContextFinder.GetDataContextForType(String typeName) in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\SqlCommon\DataContextFinder.cs:line 37
       at [[]].Sql.Common.PersistenceService`2.get_DataContext() in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\SqlCommon\PersistenceServices.cs:line 66
       at [[]].Sql.Common.PersistenceService`2.get_InternalService() in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\SqlCommon\PersistenceServices.cs:line 43
       at [[]].Sql.Common.PersistenceService`2.RetrieveMany(ISpecification specification, Boolean lazyChildren, PagingInfo pagingInfo) in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\SqlCommon\PersistenceServices.cs:line 79
       at [[]].Domain.Common.BizServiceBaseSync`2.RetrieveMany(ISpecification specification, Boolean lazyChildren, PagingInfo pagingInfo) in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\DomainCommon\BizServiceBaseSync.cs:line 50
       at [[]].Mvc.Common.[[]]Controller`2.List(ISpecification criteria, PagingInfo pagingInfo) in C:\Users\Kathleen\Current Projects\Universal\MVC2011Root\MVCGenerator2011\Common\MvcCommon\[[]]Controller.cs:line 106
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException:

 

: