locked
WCF Data Services client - Delete a GUID key is throwing "An error occurred while processing this request." Inner execption shows "Bad Request - Error in query syntax." RRS feed

  • Question

  • I'm trying out my WCF data services by creating a console app which add service reference to my service. My service is a WCF Data Services custom provider.
     
    Below is my code to delete.
     
    ServiceReference1.Account svc = new ServiceReference1.Account(new uri(...));
    var accounts = svc.Execute<ServiceReference1.Account>(new uri(..."),UriKind.Relative));
    foreach (var acc in accounts) {svc.DeleteObject(acc);}
    svc.SaveChanges();
     
    Running the code will throw "An error occurred while processing this request." with inner execption showing "Bad Request - Error in query syntax."
     
    My key is a GUID type. Resource type is defined as below
    Key-> <Key><PropertyRef Name="ID" /></Key>
    Property -> <Property Name="ID" Type="Edm.Guid" Nullable="false" />
     
    Taking a look at Fiddler shows
    DELETE http://.../Account('%7B96fe6d65-ec96-4227-9450-06b21c90cd25%7D') HTTP/1.1
    Should it be something like below?
    DELETE http://.../Account(guid'%7B96fe6d65-ec96-4227-9450-06b21c90cd25%7D') HTTP/1.1
     
    If yes, how do I ensure that guid syntax is included in my query string?
    If not, what is the correct way?
    Friday, September 30, 2011 10:47 AM

Answers

  • After more analysis, found that it is because of this.

    <link rel="edit" title="Account" href="Account('%7B96fe6d65-ec96-4227-9450-06b21c90cd25%7D')" />

    It is actually trying to update/delete account according to the href value.

    Also found out that the data that is returning from my data source is a string although the type defined is a string.

    Making the return data from string to guid solved my problem.

     

    • Marked as answer by Yew Fook Monday, October 10, 2011 10:45 AM
    Monday, October 10, 2011 10:45 AM

All replies

  • Hi Andy,

     In the client type generated by "Add Service Reference", is the Key property generated as a String property or a Guid property ?


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Sunday, October 2, 2011 4:38 PM
    Moderator
  • Hi,

    It is generated as Guid.

    Thanks.

    Tuesday, October 4, 2011 9:11 AM
  • Hi Pratik,

    I think my reply earlier was too brief and created missunderstanding.

    You asked if it is generated as a String property or a Guid property.

    The key property is generated as a Guid (always been), so my problem still exists.

    I'm also facing the same problem for Update.

    I tried DELETE http://.../Account(guid'%7B96fe6d65-ec96-4227-9450-06b21c90cd25%7D') HTTP/1.1 in fiddler and my service works.

    I tried stepping into .NET framework and I came to the following dead end.

      System.Data.Services.Client.dll!System.Data.Services.Client.Util.CreateUri(System.Uri baseUri, System.Uri requestUri) Line 279 + 0xf bytes C#
    > System.Data.Services.Client.dll!System.Data.Services.Client.EntityDescriptor.GetResourceUri(System.Uri baseUriWithSlash, bool queryLink) Line 385 + 0xf0 bytes C#
      System.Data.Services.Client.dll!System.Data.Services.Client.DataServiceContext.CreateRequest(System.Data.Services.Client.EntityDescriptor box, System.Data.Services.Client.EntityStates state, bool replaceOnUpdate) Line 2824 + 0xe bytes C#
      System.Data.Services.Client.dll!System.Data.Services.Client.DataServiceContext.SaveResult.CreateNextRequest(bool replaceOnUpdate) Line 4591 + 0x14 bytes C#
      System.Data.Services.Client.dll!System.Data.Services.Client.DataServiceContext.SaveResult.BeginNextChange(bool replaceOnUpdate) Line 4328 + 0xc bytes C#
      System.Data.Services.Client.dll!System.Data.Services.Client.DataServiceContext.SaveChanges(System.Data.Services.Client.SaveChangesOptions options) Line 1186 C#
      System.Data.Services.Client.dll!System.Data.Services.Client.DataServiceContext.SaveChanges() Line 1150 + 0xc bytes C#

    The debbuging info is probably not up to date as it is not stepping correctly in EntityDescriptor.GetResourceUri where it gets to GetLink and straight to CreateUri method? I suspect that it actually gets to return this.EditLink in GetLink.

    The value of this.EditLink is the Uri of the invalid query filter.

    I do not know where and how this value is assigned or if the code should get there at all.

    Sorry for the confusing caused 


    • Edited by Yew Fook Friday, October 7, 2011 11:05 AM
    Friday, October 7, 2011 10:58 AM
  • After more analysis, found that it is because of this.

    <link rel="edit" title="Account" href="Account('%7B96fe6d65-ec96-4227-9450-06b21c90cd25%7D')" />

    It is actually trying to update/delete account according to the href value.

    Also found out that the data that is returning from my data source is a string although the type defined is a string.

    Making the return data from string to guid solved my problem.

     

    • Marked as answer by Yew Fook Monday, October 10, 2011 10:45 AM
    Monday, October 10, 2011 10:45 AM