locked
how to expose an objectcontext as a data service when the objectcontext is defined in a class library? RRS feed

  • Question

  • Hi all,

    I'm trying to use data services but I'm getting errors on implementation.

    My implementation is done like this:

    - I have one data model for all project databases, so I can instantiate it with multiple connections.

    Therefore I removed the empty constructor on the context and added a DBManager to instantiate it.

    To expose it as a service, I was thinking about using a Header to make the project selection. in the constructor for the data service I will then get the project details according to that project ID, and I have also overridden the "CreateDataSource" method to be able to instantiate my objectcontext throught my DB Manager.

    the ObjectContext class is in a seperate Class library, it is a model created based upon the database, everything is made with the latest versions

    public class ProjectDataService<T> : DataService<T> where T: ObjectContext
    	{
    		protected Project Project { getset; }
     
    		protected override T CreateDataSource()
    		{
    			T context =  DBManager.GetObjectContext<T>(this.Project.Id);
    			return context;
    		}
     
    		public ProjectDataService()
    		{
    			var projectId = HttpContext.Current.Request.Headers["ET_ProjectId"];
    			Guid gProjectId = Guid.Empty;
    			Guid.TryParse(projectId, out gProjectId);
     
    			try
    			{
    				using (MasterContext ctx = DBManager.GetMasterContext())
    				{
    					Project = ctx.Projects
    						.Include("Customer")
    						.Include("ProjectSettings.ProjectSettingType")
    						.Include("ProjectSettings.ProjectSettingTypeValue")
    						.Where(x => x.Id == gProjectId).Single();
    				}
    			}
    			catch (Exception)
    			{
     				throw;
    			}
    		}
    	}

    When using the code above in a service like this:

    [ServiceBehavior(IncludeExceptionDetailInFaults=true, InstanceContextMode=InstanceContextMode.PerSession)]
    	public class InsightsDataService : ProjectDataService<InsightsContext>
        {
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(DataServiceConfiguration config)
            {
    			config.SetEntitySetAccessRule("ArticleSets"EntitySetRights.AllRead);
    			config.SetEntitySetAccessRule("Articles"EntitySetRights.AllRead);
    			//config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            }
        }

    I'm getting this error:

         <p>The server encountered an error processing the request. The exception message is 'Value cannot be null.

    Parameter name: key'. See server logs for more details. The exception stack trace is: </p>
          <p>   bij System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
       bij System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       bij System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 childTypes, IDictionary`2 entitySets)
       bij System.Data.Services.DataService`1.CreateProvider()
       bij System.Data.Services.DataService`1.HandleRequest()
       bij System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody)
       bij SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] )
       bij System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
       bij System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
       bij System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
       bij System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
       bij System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>

    Any Ideas

    Tuesday, December 18, 2012 10:36 AM