Breezejs的批量后请求

时间:2013-09-15 15:08:03

标签: odata breeze asp.net-web-api

我一直在尝试使用Breezejs和WebAPI OData控制器发布实体。
以下是配置:

config.Routes.MapODataRoute(
routeName: "odata",
routePrefix: "odata",
model: model,
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

模型非常直接:

public class ServiceMetadata
{
    public int ServiceMetadataId { get; set; }
    public string ServiceName { get; set; }
    public string Description { get; set; }
    public ObjectState? State { get; set; }
    public DateTime? LastUpdated { get; set; }
}

它通过默认值映射:

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

客户端也非常简单,使用AngularJs,部分来自Todo示例:http://www.breezejs.com/samples/todo-angular

breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);
var serviceName = 'http://localhost:8081/odata/';
breeze.config.initializeAdapterInstances({ dataService: "OData" });
var manager = new breeze.EntityManager(serviceName);
manager.enableSaveQueuing(true);

使用默认的createEntity()方法完成实际发布:

function createServiceMetadata(initialValues) {
    return manager.createEntity('ServiceMetadata', initialValues);
}

整个事情看起来像:

 serviceMetadatas.createServiceMetadata({
     ServiceName: $scope.newServiceName,
     Description: $scope.newServiceDescription
 });

 serviceMetadatas.saveChanges();


但是,请求未被传输到正确的控制器(ServiceMetadatasController,它继承自EntitySetController),或者任何其他控制器。
HTTP请求如下所示:

    POST http://localhost:8081/odata/$batch HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: multipart/mixed
Accept-Language: he-IL,he;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DataServiceVersion: 2.0
Content-Type: multipart/mixed; charset=UTF-8;boundary=batch_4f09-d7cf-dd99
MaxDataServiceVersion: 2.0
Referer: http://localhost:9000/
Content-Length: 580
Origin: http://localhost:9000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache


--batch_4f09-d7cf-dd99
Content-Type: multipart/mixed; boundary=changeset_ca0c-06b7-ddbe

--changeset_ca0c-06b7-ddbe
Content-Type: application/http
Content-Transfer-Encoding: binary

POST ServiceMetadatas HTTP/1.1
Content-ID: 1
DataServiceVersion: 2.0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1
Content-Type: application/json;odata=verbose
MaxDataServiceVersion: 2.0

{"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df","LastUpdated":null}
--changeset_ca0c-06b7-ddbe--

--batch_4f09-d7cf-dd99--

回复:

    HTTP/1.1 202 Accepted
Cache-Control: no-cache
Pragma: no-cache
Content-Type: multipart/mixed; boundary=batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: http://localhost:9000
Access-Control-Allow-Credentials: true
DataServiceVersion: 2.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcVG9tZXJcRG9jdW1lbnRzXFZpc3VhbCBTdHVkaW8gMjAxMlxQcm9qZWN0c1xFYXN5Qml6eVxFYXN5Qml6eS5XZWJBUElcb2RhdGFcJGJhdGNo?=
X-Powered-By: ASP.NET
Date: Sun, 15 Sep 2013 14:32:39 GMT
Content-Length: 443

--batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac
Content-Type: multipart/mixed; boundary=changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4

--changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 406 Not Acceptable
Content-ID: 1


--changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4--
--batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac--

知道hack正在发生什么事吗? B.T.W GET请求效果很好。

P.S。 在看了几个演示之后,考虑到WebApi和OData,我使用BreezeJS会很直接 我必须说,容易配置这个JS库。我希望它会变得难以设置但易于使用。

感谢。

@UPDATE 查看 Javier 的绝佳答案!!

在微风代码上挖掘allllot后,我开始意识到问题在于breezejs的createChangeRequests(),就在这里:

request.requestUri = entity.entityType.defaultResourceName;

出于某种原因,defaultResouceName完全忽略了该实体的路径。 长话短说,以下是黑客解决:

manager.metadataStore.getEntityType(ENTITY_TYPE).setProperties({defaultResourceName: THE_MISSING_PART_FROM_THE_URL + ENTITY_TYPE});
manager.createEntity(ENTITY_TYPE, values);

不是很好,但仍然有效!

3 个答案:

答案 0 :(得分:4)

新答案是..使用webApiOData数据服务代替" OData":

breeze.config.initializeAdapterInstances({ 
  dataService : 'webApiOData'
});

答案 1 :(得分:3)

问题出在内部请求的网址中。网址需要与主机相关。假设您的服务托管在host/service中(在我们的例子中,服务将与odata前缀相同),因此通常您会发送host/service/Customers/service/Customers等请求。

发出批量请求时,内部请求中的URL可能是绝对的或相对于主机。问题是,在您的请求中,网址是ServiceMetadatas,它与服务根相关,而不是主机。

Web API将相对网址解释为host/ServiceMetadatas而非host/service/ServiceMetadatas,这就是导致错误的原因。

根据您的repro项目,以下请求正常工作:

POST http://localhost:6974/odata/$batch HTTP/1.1
Host: localhost:6974
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: multipart/mixed
Accept-Language: he-IL,he;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DataServiceVersion: 2.0
Content-Type: multipart/mixed; charset=UTF-8;boundary=batch_4f09-d7cf-dd99
MaxDataServiceVersion: 2.0
Referer: http://localhost:9000/
Content-Length: 565
Origin: http://localhost:9000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

--batch_4f09-d7cf-dd99
Content-Type: multipart/mixed; boundary=changeset_ca0c-06b7-ddbe

--changeset_ca0c-06b7-ddbe
Content-Type: application/http
Content-Transfer-Encoding: binary

POST odata/ServiceMetadatas HTTP/1.1
Content-ID: 1
DataServiceVersion: 2.0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1
Content-Type: application/json;odata=verbose
MaxDataServiceVersion: 2.0

{"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df"}
--changeset_ca0c-06b7-ddbe--

--batch_4f09-d7cf-dd99--

相关的回复如下:

HTTP/1.1 202 Accepted
Cache-Control: no-cache
Pragma: no-cache
Content-Type: multipart/mixed; boundary=batchresponse_6779b5e5-6e40-4363-9a98-5a33d062da28
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 2.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcamFjYWx2YXJcRG93bmxvYWRzXE9EYXRhQmF0Y2gtbWFzdGVyXENsZWFuV2ViQXBpUHJvamVjdFxvZGF0YVwkYmF0Y2g=?=
X-Powered-By: ASP.NET
Date: Tue, 17 Sep 2013 16:48:50 GMT
Content-Length: 872

--batchresponse_6779b5e5-6e40-4363-9a98-5a33d062da28
Content-Type: multipart/mixed; boundary=changesetresponse_b63ca946-ce66-43e6-a78f-d44a5b8f2d5c

--changesetresponse_b63ca946-ce66-43e6-a78f-d44a5b8f2d5c
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 201 Created
Location: http://localhost:6974/odata/ServiceMetadatas(-1)
Content-ID: 1
Content-Type: application/json; odata=verbose; charset=utf-8
DataServiceVersion: 2.0

{
  "d":{
    "__metadata":{
      "id":"http://localhost:6974/odata/ServiceMetadatas(-1)","uri":"http://localhost:6974/odata/ServiceMetadatas(-1)","type":"CleanWebApiProject.Models.ServiceMetadata"
    },"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df"
  }
}
--changesetresponse_b63ca946-ce66-43e6-a78f-d44a5b8f2d5c--
--batchresponse_6779b5e5-6e40-4363-9a98-5a33d062da28--

我在控制器中进行的唯一更改是以下(与批次无关):

public class ServiceMetadatasController : EntitySetController<ServiceMetadata, int>
{
    protected override ServiceMetadata CreateEntity(ServiceMetadata entity)
    {
        return entity;
    }

    protected override int GetKey(ServiceMetadata entity)
    {
        return entity.ServiceMetadataId;
    }

    public override IQueryable<ServiceMetadata> Get()
    {
        return new List<ServiceMetadata>
               {
                   new ServiceMetadata() {ServiceName = "Service1", Description = "Desc1"},
                   new ServiceMetadata() {ServiceName = "Service2", Description = "Desc1"}

               }.AsQueryable();
    }

}

我希望这可以解决您的问题,如果您手动生成内部请求的url,或者如果它是breezejs为您执行此操作,请告诉我,以便我可以跟进并确保它得到修复。

答案 2 :(得分:0)

不确定这是否是您的问题,但Microsoft的ODataModelBuilder未实现完整的OData模型。特别是,它不会生成外键约束。 MS意识到这一点,并计划在以后的版本中更新它。在此之前,您最好使用WCF数据服务来创建OData端点。