예제 #1
0
        public static void InitializeService(DataServiceConfiguration dataServiceConfiguration)
        {
            // Configure settings
            try
            {
                dataServiceConfiguration.UseVerboseErrors = true;
                dataServiceConfiguration.DataServiceBehavior.AcceptCountRequests            = true;
                dataServiceConfiguration.DataServiceBehavior.AcceptAnyAllRequests           = true;
                dataServiceConfiguration.DataServiceBehavior.AcceptProjectionRequests       = true;
                dataServiceConfiguration.DataServiceBehavior.AcceptReplaceFunctionInQuery   = true;
                dataServiceConfiguration.DataServiceBehavior.AcceptSpatialLiteralsInQuery   = true;
                dataServiceConfiguration.DataServiceBehavior.InvokeInterceptorsOnLinkDelete = true;
                dataServiceConfiguration.DataServiceBehavior.MaxProtocolVersion             = DataServiceProtocolVersion.V3;

                IEnumerable <string> entitySetNames = EntitySetHelper.GetEntitySetNames <T>();

                foreach (string entitySetName in entitySetNames)
                {
                    dataServiceConfiguration.SetEntitySetAccessRule(entitySetName, EntitySetRights.All);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
예제 #2
0
        /// <summary>
        /// Subscribes to all <see cref="DataRequest" />s based on the registered <see cref="DbContext" />.
        /// </summary>
        private void SubscribeToDataRequests()
        {
            // Subscribe to all data requests for all entity types
            // CRUD security is implemented in the db context or dataservice layer and not in the P2P layer
            // This makes it easier for applications to centralize their validation
            IEnumerable <Type> entityTypes =
                (((Func <IEnumerable <Type> >)EntitySetHelper.GetEntityTypes <DbContext>).GetMethodInfo()
                 .GetGenericMethodDefinition().MakeGenericMethod(_dbContextType).Invoke(null, null) as
                 IEnumerable <Type> ?? new List <Type>()).ToList();

            IEnumerable <Type> dataRequests = TypeHelper.GetSubclasses(typeof(DataRequest))
                                              .Where(x => x.IsGenericType && x.GetGenericArguments().Length == 1)
                                              .ToList();

            ObjectContext objectContext = (_dbContext as IObjectContextAdapter).ObjectContext;

            foreach (Type dataRequest in dataRequests)
            {
                foreach (Type entityType in entityTypes)
                {
                    try
                    {
                        ISubscription subscription =
                            Activator.CreateInstance(
                                typeof(Subscription <>).MakeGenericType(dataRequest.MakeGenericType(entityType))) as
                            ISubscription;

                        if (subscription == null)
                        {
                            throw new ArgumentException(
                                      string.Format(
                                          "Could not subscribe to data requests of type '{0}' for entities of type '{1}'.",
                                          dataRequest.Name, entityType.AssemblyQualifiedName()));
                        }

                        subscription.EntitySet = EntitySetHelper.GetEntitySetName(objectContext, entityType);
                        _peer.Subscribe(subscription);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                    }
                }
            }
        }