示例#1
0
        private static Type EnsureElementType(
            this ApiContext context,
            string namespaceName,
            string name)
        {
            Type elementType = null;

            var mapper = context.GetApiService <IModelMapper>();

            if (mapper != null)
            {
                if (namespaceName == null)
                {
                    mapper.TryGetRelevantType(context, name, out elementType);
                }
                else
                {
                    mapper.TryGetRelevantType(context, namespaceName, name, out elementType);
                }
            }

            if (elementType == null)
            {
                throw new NotSupportedException(string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    Resources.ElementTypeNotFound,
                                                    name));
            }

            return(elementType);
        }
示例#2
0
        /// <summary>
        /// Asynchronously gets an API model using an API context.
        /// </summary>
        /// <param name="context">
        /// An API context.
        /// </param>
        /// <param name="cancellationToken">
        /// An optional cancellation token.
        /// </param>
        /// <returns>
        /// A task that represents the asynchronous
        /// operation whose result is the API model.
        /// </returns>
        public static async Task <IEdmModel> GetModelAsync(
            this ApiContext context,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNull(context, "context");

            var config = context.Configuration;

            if (config.Model != null)
            {
                return(config.Model);
            }

            var builder = context.GetApiService <IModelBuilder>();

            if (builder == null)
            {
                throw new InvalidOperationException(Resources.ModelBuilderNotRegistered);
            }

            Task <IEdmModel> running;
            var source = config.CompeteModelGeneration(out running);

            if (source == null)
            {
                return(await running);
            }

            try
            {
                var buildContext = new ModelContext(context);
                var model        = await builder.GetModelAsync(buildContext, cancellationToken);

                source.SetResult(model);
                return(model);
            }
            catch (AggregateException e)
            {
                source.SetException(e.InnerExceptions);
                throw;
            }
            catch (Exception e)
            {
                source.SetException(e);
                throw;
            }
        }
示例#3
0
 private static PropertyBag GetPropertyBag(this ApiContext context)
 {
     Ensure.NotNull(context, "context");
     return(context.GetApiService <PropertyBag>());
 }