示例#1
0
        /// <summary>
        /// Extension method to use V3 to V4 translation middleware.
        /// </summary>
        /// <param name="builder">IApplicationBuilder that will use translation middleware</param>
        /// <param name="v3model">V3 model that represents edmx contract</param>
        /// <param name="v4Model">Required V4 model to validate translated URI</param>
        /// <returns>builder now using migration middleware</returns>
        public static IApplicationBuilder UseODataMigration(this IApplicationBuilder builder,
                                                            Data.Edm.IEdmModel v3model,
                                                            Microsoft.OData.Edm.IEdmModel v4Model)
        {
            // Convert v3 model to string to pass through metadata request.
            string v3Edmx;

            using (StringWriter stringWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
                {
                    IEnumerable <Data.Edm.Validation.EdmError> errors = new List <Data.Edm.Validation.EdmError>();
                    Data.Edm.Csdl.EdmxWriter.TryWriteEdmx(v3model, xmlWriter, Data.Edm.Csdl.EdmxTarget.OData, out errors);
                }
                v3Edmx = stringWriter.ToString();
            }
            return(builder.UseODataMigration(v3Edmx, v4Model));
        }
        /// <summary>
        /// Constructs an instance of TranslationMiddleware, requiring the root of the service, a V3 model instance and V4 model instance.
        /// </summary>
        /// <param name="next">Delegate required for middleware</param>
        /// <param name="v3Edmx">V3 EDMX string representation of model</param>
        /// <param name="v4Model">Instance of V4 EDM model</param>
        public ODataMigrationMiddleware(RequestDelegate next,
                                        string v3Edmx,
                                        Microsoft.OData.Edm.IEdmModel v4Model)
        {
            ExceptionUtil.IfArgumentNullThrowException(v3Edmx, "v3Edmx", "V3 edmx cannot be empty");
            ExceptionUtil.IfArgumentNullThrowException(v4Model, "v4Model", "V4 model cannot be null");

            this.next        = next;
            this.serviceRoot = new Uri("http://localhost/"); // The actual service root doesn't matter; it is just needed as a parameter
            this.v4Model     = v4Model;

            try {
                using (XmlReader reader = XmlReader.Create(new StringReader(v3Edmx)))
                {
                    this.v3Model = Data.Edm.Csdl.EdmxReader.Parse(reader);
                }
            }
            catch (Exception)
            {
                throw new ArgumentException("Unable to parse OData V3 Edmx; make sure your edmx is a valid OData contract.");
            }
        }