protected virtual async Task BatchCore() { var actionDescriptors = GetService <IActionDescriptorCollectionProvider>().ActionDescriptors; var actionInvokerFactory = GetService <IActionInvokerFactory>(); String[] apiSegments = base.HttpContext.Request.Path.Value.Split(new[] { '/' }, 2, StringSplitOptions.RemoveEmptyEntries); Uri baseUri = UriHelper.GetBaseUri(base.Request); var messageContext = new OeMessageContext(baseUri, EdmModel, DataAdapter.EntitySetMetaAdapters); OeBatchMessage batchMessage = OeBatchMessage.CreateBatchMessage(messageContext, base.HttpContext.Request.Body, base.HttpContext.Request.ContentType); Object dataContext = null; try { dataContext = DataAdapter.CreateDataContext(); foreach (OeOperationMessage operation in batchMessage.Changeset) { OeEntitySetAdapter entitySetAdapter = DataAdapter.GetEntitySetAdapter(operation.EntityItem.EntitySet.Name); var segments = new String[apiSegments.Length]; Array.Copy(apiSegments, segments, 1); segments[apiSegments.Length - 1] = entitySetAdapter.EntitySetMetaAdapter.EntitySetName; var candidates = OeRouter.SelectCandidates(actionDescriptors.Items, segments, operation.Method); if (candidates.Count > 1) { throw new AmbiguousActionException(String.Join(Environment.NewLine, candidates.Select(c => c.DisplayName))); } if (candidates.Count == 0) { throw new InvalidOperationException("Action " + operation.Method + " for controller " + segments.Last() + " not found"); } var modelState = new OeFilterAttribute.BatchModelStateDictionary() { Entity = operation.EntityItem.Entity, DataContext = new OeDataContext(ref entitySetAdapter, dataContext, operation) }; OnBeforeInvokeController(modelState.DataContext, operation.EntityItem); var actionContext = new ActionContext(base.HttpContext, base.HttpContext.GetRouteData(), candidates[0], modelState); IActionInvoker actionInvoker = actionInvokerFactory.CreateInvoker(actionContext); await actionInvoker.InvokeAsync(); } await SaveChangesAsync(dataContext).ConfigureAwait(false); } finally { if (dataContext != null) { DataAdapter.CloseDataContext(dataContext); } } base.HttpContext.Response.ContentType = base.HttpContext.Request.ContentType; var batchWriter = new OeBatchWriter(baseUri, EdmModel); batchWriter.Write(base.HttpContext.Response.Body, batchMessage); }
protected virtual async Task BatchCore() { var actionDescriptors = GetService <IActionDescriptorCollectionProvider>().ActionDescriptors; var actionInvokerFactory = GetService <IActionInvokerFactory>(); String basePath = ""; if (base.HttpContext.Request.PathBase.HasValue) { basePath = base.HttpContext.Request.PathBase; } else { int i = base.HttpContext.Request.Path.Value.IndexOf('/', 1); if (i > 0) { basePath = base.HttpContext.Request.Path.Value.Substring(0, i); } } Uri baseUri = UriHelper.GetBaseUri(base.Request); OeBatchMessage batchMessage = OeBatchMessage.CreateBatchMessage(EdmModel, baseUri, base.HttpContext.Request.Body, base.HttpContext.Request.ContentType); Object dataContext = null; try { dataContext = DataAdapter.CreateDataContext(); foreach (OeOperationMessage operation in batchMessage.Changeset) { OeEntitySetAdapter entitySetAdapter = DataAdapter.EntitySetAdapters.FindByEntitySetName(operation.EntitySet.Name); String path = basePath + "/" + entitySetAdapter.EntitySetName; List <ActionDescriptor> candidates = OeRouter.SelectCandidates(actionDescriptors.Items, base.RouteData.Values, path, operation.Method); if (candidates.Count > 1) { throw new AmbiguousActionException(String.Join(Environment.NewLine, candidates.Select(c => c.DisplayName))); } if (candidates.Count == 0) { throw new InvalidOperationException("Action " + operation.Method + " for controller " + basePath + " not found"); } Object entity; if (operation.Method == ODataConstants.MethodPatch) { var properties = new Dictionary <String, Object>(); foreach (ODataProperty odataProperty in operation.Entry.Properties) { PropertyInfo propertyInfo = entitySetAdapter.EntityType.GetProperty(odataProperty.Name); properties[odataProperty.Name] = OeEdmClrHelper.GetClrValue(propertyInfo.PropertyType, odataProperty.Value); } entity = properties; } else { entity = OeEdmClrHelper.CreateEntity(entitySetAdapter.EntityType, operation.Entry); } var modelState = new OeFilterAttribute.BatchModelStateDictionary() { Entity = entity, DataContext = new OeDataContext(entitySetAdapter, EdmModel, dataContext, operation) }; OnBeforeInvokeController(modelState.DataContext, operation.Entry); var actionContext = new ActionContext(base.HttpContext, base.HttpContext.GetRouteData(), candidates[0], modelState); IActionInvoker actionInvoker = actionInvokerFactory.CreateInvoker(actionContext); await actionInvoker.InvokeAsync(); } await SaveChangesAsync(dataContext).ConfigureAwait(false); } finally { if (dataContext != null) { DataAdapter.CloseDataContext(dataContext); } } base.HttpContext.Response.ContentType = base.HttpContext.Request.ContentType; var batchWriter = new OeBatchWriter(EdmModel, baseUri); batchWriter.Write(base.HttpContext.Response.Body, batchMessage); }