<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>With Great Power comes Great Response.write("Ability") : Debugging</title><link>http://blogs.msdn.com/phaniraj/archive/tags/Debugging/default.aspx</link><description>Tags: Debugging</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Working with associations in ADO.NET Data Services , Part 2</title><link>http://blogs.msdn.com/phaniraj/archive/2008/10/23/working-with-associations-in-ado-net-data-services-part-2.aspx</link><pubDate>Fri, 24 Oct 2008 00:01:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9013687</guid><dc:creator>PhaniRajuYN</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/phaniraj/comments/9013687.aspx</comments><wfw:commentRss>http://blogs.msdn.com/phaniraj/commentrss.aspx?PostID=9013687</wfw:commentRss><description>&lt;p&gt;In part 2 ,we will talk about how to use the client library to deal with :&lt;/p&gt;  &lt;h1&gt;1..N Associations&lt;/h1&gt;  &lt;p&gt;This is the case of entities associated with each other 1..N . An example from Northwind is the relation between Categories and Products Entities. &lt;/p&gt;  &lt;p&gt;An instance of the Categories entity type should have associated Products entities, this is another way of saying “ A row in the Products table should have links to a row in the Categories table”.    &lt;br /&gt;In case of 1..N relations , the left( child ) ends of the association are expressed as Collection properties on the right ( parent ) end of the relation.     &lt;br /&gt;This is shown in the metadata of the service as :&lt;/p&gt;  &lt;pre class="darkcsharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;EntityType&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Categories&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;&amp;lt;!-- Primary Key--&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Key&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PropertyRef&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;CategoryID&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Key&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="rem"&gt;&amp;lt;!--Primitive Properties--&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Property&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;CategoryID&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Edm.Int32&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Nullable&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Property&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;CategoryName&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Edm.String&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Nullable&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Property&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Description&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Edm.String&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Nullable&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Property&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Picture&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Edm.Binary&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Nullable&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
 &lt;span class="rem"&gt;&amp;lt;!-- Child Entity Set--&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;NavigationProperty&lt;/span&gt; 
        &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Products&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;Relationship&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;northwind.FK_Products_Categories&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;FromRole&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Categories&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;ToRole&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Products&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;EntityType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This leads the client utility( DataSvcUtil.exe ) to generate a type Individual which has instances of types Customer and Contact as Properties.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The generated class on the client looks like this : 
  &lt;br /&gt;&lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Categories {
  &lt;span class="kwrd"&gt;public&lt;/span&gt; global::System.Collections.ObjectModel.Collection&amp;lt;Products&amp;gt; Products {
            get {
                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._Products;
            }
            set {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; ((&lt;span class="kwrd"&gt;value&lt;/span&gt; != &lt;span class="kwrd"&gt;null&lt;/span&gt;))
                {
                    &lt;span class="kwrd"&gt;this&lt;/span&gt;._Products = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
                }
            }
        }
}&lt;/pre&gt;

&lt;p&gt;Now that we have set the stage, lets look at how one would use the client library to interact with the related types.&lt;/p&gt;

&lt;h5&gt;Creating the link between the entities &lt;/h5&gt;

&lt;p&gt;You have new instances of Individual , Contact and Customer and you want to persist the new entities and their relation to the store. &lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;DataServiceContext dsc = &lt;span class="kwrd"&gt;new&lt;/span&gt; DataServiceContext(&lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;http://ServiceEndpoint&amp;quot;&lt;/span&gt;));
Categories freeProductsCategory = &lt;span class="kwrd"&gt;new&lt;/span&gt; Categories()
{
    CategoryName = &lt;span class="str"&gt;&amp;quot;Free Products&amp;quot;&lt;/span&gt;,
    Description = &lt;span class="str"&gt;&amp;quot;These products are free&amp;quot;&lt;/span&gt;,
};
Products freeHeadPhones = Products.CreateProducts(0, &lt;span class="str"&gt;&amp;quot;Free Headphones&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);
Products freeZune = Products.CreateProducts(1, &lt;span class="str"&gt;&amp;quot;Free Zune&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);

dsc.AddObject(&lt;span class="str"&gt;&amp;quot;Categories&amp;quot;&lt;/span&gt;, freeProductsCategory);
dsc.AddObject(&lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;, freeHeadPhones);
dsc.AddObject(&lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;, freeZune);&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Now what ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since the entities are created and added to the store can’t I just set the Customer and Contact Property to the Individual object and that will save the relation ? 
  &lt;br /&gt;&lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;freeProductsCategory.Products.Add(freeHeadPhones);
freeProductsCategory.Products.Add(freeZune);
dsc.SaveChanges();&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Shouldn’t this be enough ? 
  &lt;br /&gt;Nope , that’s not enough to save the relation to the store. 

  &lt;br /&gt;

  &lt;br /&gt;&lt;strong&gt;Why ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Remember that the client context only gives you POCO access to entities in the store and any new entities that you create. 
  &lt;br /&gt;The Client context &lt;strong&gt;does not track any relations &lt;/strong&gt;unless you &lt;strong&gt;explicitly ask it to do so !&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Neat&amp;#160; , now how do I ask it to do that ? &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You use the AddLink method defined on the context. 
  &lt;br /&gt;&lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;dsc.AddLink(freeProductsCategory, &lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;, freeHeadPhones);
dsc.AddLink(freeProductsCategory, &lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;, freeZune);&lt;/pre&gt;

&lt;p&gt;The signature and the intent of the SetLink method is lucid , it binds 2 entities into a relation , its kinda like a priest at a wedding, 
  &lt;br /&gt;This is what it looks like .. 

  &lt;br /&gt;

  &lt;br /&gt;AddLink ( Parentinstance ,”ChildPropertyName”,ChildInstance)&lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;Deleting the parent entity&lt;/strong&gt;&lt;/h5&gt;

&lt;p&gt;Deleting the parent entity means that the relations with the child entities are also removed. 
  &lt;br /&gt;Think of this as the argument of “How do I delete an entity that has 1..1 links with other&amp;#160;&amp;#160; entities?” 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happens if I delete the parent entity without deleting the links ?&lt;/strong&gt;&lt;/p&gt;
It depends on your store . If your database is configured to do a cascade delete on deletion of the parent entity ,you might get away with deleting the entity without removing the links 

&lt;p&gt;The right way to delete the entity is to remove all the links it has with the child entities and then delete the entity itself. 
  &lt;br /&gt;The code would look like this.. 

  &lt;br /&gt;&lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;DataServiceContext dsc = &lt;span class="kwrd"&gt;new&lt;/span&gt; DataServiceContext(&lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;http://ServiceEndpoint&amp;quot;&lt;/span&gt;));
&lt;span class="rem"&gt;//Load an existing parent&lt;/span&gt;
Categories freeProductsCategory = dsc&lt;br /&gt;                                  .CreateQuery&amp;lt;Categories&amp;gt;(&lt;span class="str"&gt;&amp;quot;Categories&amp;quot;&lt;/span&gt;)
                                  .Where(cat =&amp;gt; cat.CategoryName == &lt;span class="str"&gt;&amp;quot;Free Products&amp;quot;&lt;/span&gt;)
                                  .First();
&lt;span class="rem"&gt;//Load all the related child Entities&lt;/span&gt;
dsc.LoadProperty(freeProductsCategory, &lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;);

&lt;span class="rem"&gt;//Delete the links with each of the child entities&lt;/span&gt;
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Products freeProduct &lt;span class="kwrd"&gt;in&lt;/span&gt; freeProductsCategory.Products) {
    dsc.DeleteLink(freeProductsCategory, &lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;, freeProduct);
}
&lt;span class="rem"&gt;//Delete the parent entity&lt;/span&gt;
dsc.DeleteObject(freeProductsCategory);
dsc.SaveChanges();&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Wait !! what’s with the LoadProperty there ? 
    &lt;br /&gt;&lt;/strong&gt;Well, if you need to delete the relation , the way to identify the relation or the link is to have both the right and the left end of the relations to be materialized. without the expand , the Contact and the Customer property are null , and we don’t know which relation to delete.In case of 1..1 relations it might be easy to predict based on the relation name , 

  &lt;br /&gt;but wouldn’t be easy in case of 1..n relations. 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deleting the child entity&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;&lt;span class="rem"&gt;//Load an existing parent&lt;/span&gt;
Categories freeProductsCategory = dsc
                                   .CreateQuery&amp;lt;Categories&amp;gt;(&lt;span class="str"&gt;&amp;quot;Categories&amp;quot;&lt;/span&gt;)
                                   .Where(cat =&amp;gt; cat.CategoryName == &lt;span class="str"&gt;&amp;quot;Free Products&amp;quot;&lt;/span&gt;)
                                   .First();
&lt;span class="rem"&gt;//Load all the related child Entities&lt;/span&gt;
dsc.LoadProperty(freeProductsCategory, &lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;);

&lt;span class="rem"&gt;//Delete the links with each of the child entities&lt;/span&gt;
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Products freeProduct &lt;span class="kwrd"&gt;in&lt;/span&gt; freeProductsCategory.Products) {
    dsc.DeleteLink(freeProductsCategory, &lt;span class="str"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;, freeProduct);
&lt;span class="rem"&gt;    //Delete the child entity&lt;/span&gt;
    dsc.DeleteObject(freeProduct);
}
dsc.SaveChanges();&lt;/pre&gt;

&lt;h5&gt;Special Consideration for Navigation properties that are named differently than the Entity Type&lt;/h5&gt;

&lt;p&gt;This is a special case because of a bug in the client library at the time of writing this post. 
  &lt;br /&gt;If you have a navigation property&amp;#160; which is named differently than the EntityType and try to delete the link between the source 

  &lt;br /&gt;and the navigation property , we produce an invalid URI for the DELETE of the link. 

  &lt;br /&gt;

  &lt;br /&gt;ex: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Entity Type :&lt;/strong&gt; Customers , &lt;strong&gt;EntitySet Name : &lt;/strong&gt;Customers 

  &lt;br /&gt;&lt;strong&gt;Navigation Properties : 
    &lt;br /&gt;&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;EntityType :&lt;/strong&gt; Orders , &lt;strong&gt;Navigation Property Name :&lt;/strong&gt; CashOrders 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;EntityType : &lt;/strong&gt;Orders , &lt;strong&gt;Navigation Property Name : &lt;/strong&gt;CreditCardOrders 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;Upon calling , 

  &lt;br /&gt;dataContext.DeleteLink(customerInstance,”CashOrders”,orderInstance); 

  &lt;br /&gt;

  &lt;br /&gt;We generate a DELETE to the URI 

  &lt;br /&gt;/Customers(Key)/$links/Orders(Key) 

  &lt;br /&gt;This is wrong , the DELETE should go to : 

  &lt;br /&gt;/Customers(Key)/$links/CashOrders(Key) 

  &lt;br /&gt;

  &lt;br /&gt;We will address this issue in a future release .In the meantime ,&amp;#160; use the workaround below , &lt;/p&gt;

&lt;pre class="darkcsharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; YourDataContextClass {

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DeleteLinkManual(&lt;span class="kwrd"&gt;object&lt;/span&gt; source, &lt;span class="kwrd"&gt;string&lt;/span&gt; associationName, &lt;span class="kwrd"&gt;object&lt;/span&gt; target) {
            System.Uri UriOfParent;
            System.Uri UriOfChild;
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.TryGetUri(source, &lt;span class="kwrd"&gt;out&lt;/span&gt; UriOfParent);
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.TryGetUri(target, &lt;span class="kwrd"&gt;out&lt;/span&gt; UriOfChild);

            &lt;span class="rem"&gt;//Get the Segment of the URI with the Key &lt;/span&gt;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; strChildAssociationURI = UriOfChild.Segments.Last();
            strChildAssociationURI = strChildAssociationURI.Substring(strChildAssociationURI.IndexOf(&lt;span class="str"&gt;'('&lt;/span&gt;));

            &lt;span class="rem"&gt;//ServiceEndPoint/ParentEntitySet/$links/NavigationPropertyName(ChildEntitySetKey)&lt;/span&gt;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; requestUriForLinkDelete = System.String.Format(&lt;span class="str"&gt;&amp;quot;{0}/{1}/$links/{2}{3}&amp;quot;&lt;/span&gt;,
                                             &lt;span class="kwrd"&gt;this&lt;/span&gt;.BaseUri.OriginalString,
                                             UriOfParent.Segments.Last(),
                                             associationName,
                                             strChildAssociationURI);

            System.Net.WebRequest request = System.Net.WebRequest.Create(requestUriForLinkDelete);
            request.Method = &lt;span class="str"&gt;&amp;quot;DELETE&amp;quot;&lt;/span&gt;;
            System.Net.HttpWebResponse response = request.GetResponse() &lt;span class="kwrd"&gt;as&lt;/span&gt; System.Net.HttpWebResponse;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (response.StatusCode != System.Net.HttpStatusCode.NoContent)
            {
                &lt;span class="kwrd"&gt;throw&lt;/span&gt; (&lt;span class="kwrd"&gt;new&lt;/span&gt; System.Data.Services.Client.DataServiceClientException(&lt;span class="str"&gt;&amp;quot;Delete Failed&amp;quot;&lt;/span&gt;));
            }
            &lt;span class="rem"&gt;//Detach this link as the link no longer exists and keep the context consistent&lt;/span&gt;
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.DetachLink(source, associationName, target);
      }
}&lt;/pre&gt;

&lt;p&gt;and then , in your code , &lt;/p&gt;

&lt;br /&gt;

&lt;pre class="darkcsharpcode"&gt;YourDataContext dataContext = new YourDataContext(&amp;lt;Uri&amp;gt;);&lt;br /&gt;dataContext.DeleteLinkManual(customerInstance,”CashOrders”,orderInstance);&lt;/pre&gt;

&lt;p&gt;This will produce the right URI : &lt;/p&gt;

&lt;br /&gt;

&lt;pre class="darkcsharpcode"&gt;ServiceEndPoint/Customers(Key)/$links/CashOrders(Key) &lt;/pre&gt;

&lt;p&gt;Note that this doesnt work in Silverlight , I will create a separate sample for Silverlight later . 
  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;If you have any questions&amp;#160; , leave a comment . If you have any issues with code and need help , please post your query on the astoria forums as I can’t promise that I will be able to reply to emails sent to me directly .The whole team is very active on the forums and the more eyes on a problem the better. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9013687" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Astoria/default.aspx">Astoria</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Demo/default.aspx">Demo</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Documentation/default.aspx">Documentation</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/DataServiceContext/default.aspx">DataServiceContext</category></item><item><title>Astoria &amp; IIS , HTTP 400 Bad Request on Posting Large Payloads to an astoria service</title><link>http://blogs.msdn.com/phaniraj/archive/2008/10/06/astoria-iis-http-400-bad-request-on-posting-large-payloads-to-an-astoria-service.aspx</link><pubDate>Mon, 06 Oct 2008 05:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8977408</guid><dc:creator>PhaniRajuYN</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/phaniraj/comments/8977408.aspx</comments><wfw:commentRss>http://blogs.msdn.com/phaniraj/commentrss.aspx?PostID=8977408</wfw:commentRss><description>&lt;P&gt;We’ve seen this in the labs and also reported by our customers on the forums, If you try to post&amp;nbsp; a large amount of data to &lt;BR&gt;an astoria data service , you might receive a response code Http 400 Bad Request even though the response data is valid. &lt;BR&gt;&lt;BR&gt;An ADO.NET Data Service sits on top of Windows Communication Foundation , although most of the required plumbing is hidden away from you&amp;nbsp; ,&amp;nbsp; you can still see the effects in scenarios such as this .&lt;/P&gt;
&lt;H6&gt;Why do I get a generic 400 Bad Request in this case ?&lt;/H6&gt;When the request size ( including headers ) exceeds 64 K ( 65536 bytes ) , WCF chokes on the request as this request size is greater than the limit on the maximum size of the request the service can recieve and sends out a HTTP 400 Bad Request as the request is invalid. &lt;BR&gt;&lt;BR&gt;
&lt;H6&gt;How do I fix this ?&lt;/H6&gt;
&lt;P&gt;a)The maximum size of the request which a WCF service can process is controlled by the &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.transportbindingelement.maxreceivedmessagesize.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.transportbindingelement.maxreceivedmessagesize.aspx"&gt;MaxReceivedMessageSize&lt;/A&gt; property on the WCF binding. &lt;BR&gt;The default value is 65536 ,exceeding which you get the 400 response code. &lt;BR&gt;&lt;BR&gt;In the web.config of the web site hosting the service , add the following node in the &amp;lt;System.ServiceModel&amp;gt; section. &lt;/P&gt;&lt;BR&gt;&lt;PRE class=darkcsharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;system.serviceModel&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;services&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
  &lt;SPAN class=rem&gt;&amp;lt;!-- The name of the service --&amp;gt;&lt;/SPAN&gt; 
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;service&lt;/SPAN&gt; &lt;SPAN class=attr&gt;name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="NorthwindService"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
    &lt;SPAN class=rem&gt;&amp;lt;!-- you can leave the address blank or specify your end point URI --&amp;gt;&lt;/SPAN&gt; 
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;endpoint&lt;/SPAN&gt; &lt;SPAN class=attr&gt;address&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;="YourServiceEndpoint"&lt;/SPAN&gt; 
              &lt;SPAN class=attr&gt;binding&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="webHttpBinding"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;bindingConfiguration&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="higherMessageSize"&lt;/SPAN&gt; 
     &lt;SPAN class=attr&gt;contract&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;="System.Data.Services.IRequestHandler"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
    &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;endpoint&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;service&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;services&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;bindings&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;webHttpBinding&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
    &lt;SPAN class=rem&gt;&amp;lt;!-- configure the maxReceivedMessageSize  value to suit the max size of &lt;/SPAN&gt;
&lt;SPAN class=rem&gt;         the request ( in bytes ) you want the service to recieve--&amp;gt;&lt;/SPAN&gt; 
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;binding&lt;/SPAN&gt; &lt;SPAN class=attr&gt;name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="higherMessageSize"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;maxReceivedMessageSize&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;="MaxMessageSize"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt; 
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;webHttpBinding&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;bindings&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;serviceHostingEnvironment&lt;/SPAN&gt; &lt;SPAN class=attr&gt;aspNetCompatibilityEnabled&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="true"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt; 
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;system.serviceModel&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;b) If hosted on IIS , the ASP.Net Request Size restriction can also cause a large request to be rejected, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; You will need to set the &lt;A href="http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx"&gt;HttpRuntimeSection.MaxRequestLength&lt;/A&gt; Property. &lt;/P&gt;&lt;PRE class=darkcsharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;system.web&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; 
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;httpRuntime&lt;/SPAN&gt; &lt;SPAN class=attr&gt;MaxRequestLength&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="ValueInKiloBytes"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;system.web&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;H6&gt;How do I Identify if the reason I am seeing a 400 Bad Request&amp;nbsp; is because of the request size ?&lt;/H6&gt;
&lt;P&gt;Identify if WCF is throwing an exception under the covers which is not being surfaced to you at the HTTP level. &lt;BR&gt;You can &lt;A title="Instructions on configuring WCF tracing" href="http://msdn.microsoft.com/en-us/library/ms733025.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/ms733025.aspx"&gt;configure WCF tracing&lt;/A&gt; on the server-side to log the necessary information from the WCF layer. &lt;BR&gt;Once you have tracing setup and you reproduced the failure , check if the log contains one or both of these exception messages.&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;PRE class=darkcsharpcode&gt;System.ServiceModel.ProtocolException
&lt;SPAN class=str&gt;"The maximum message size quota for incoming messages (65536) has been exceeded. 
To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element."
&lt;/SPAN&gt;
System.Web.HttpException
&lt;SPAN class=str&gt;"Maximum request length exceeded."&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;If you see that the log does contain this message , then you can be sure that the failure is because of the message size &lt;BR&gt;and apply this fix accordingly.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8977408" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Astoria/default.aspx">Astoria</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Documentation/default.aspx">Documentation</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/IIS/default.aspx">IIS</category></item><item><title>Debugging Ado.net Data Services</title><link>http://blogs.msdn.com/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx</link><pubDate>Wed, 18 Jun 2008 21:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8617799</guid><dc:creator>PhaniRajuYN</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/phaniraj/comments/8617799.aspx</comments><wfw:commentRss>http://blogs.msdn.com/phaniraj/commentrss.aspx?PostID=8617799</wfw:commentRss><description>&lt;p&gt;When developing your service , you might run into some issues and you want to debug your service.    &lt;br /&gt;Imagine that you are inserting data into the store using astoria and you start getting DataServiceExceptions in your client code.     &lt;br /&gt;The normal error message would be ..&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;standalone&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;yes&amp;quot;&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;error&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt; &lt;span class="attr"&gt;xml:lang&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;en-US&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;An error occurred while processing this request.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;error&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;While debugging the service , you would need more information that just this generic error message . 
  &lt;br /&gt;To switch to the dev error mode, you can use the following config settings.&amp;#160; &lt;/p&gt;

&lt;h5&gt;1) Set UseVerboseErrors to true in the ServiceConfiguration&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; YoruService : DataService&amp;lt;YourProvider&amp;gt; {&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeService(IDataServiceConfiguration config) {       
       config.UseVerboseErrors = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        . . . . . .        
    }&lt;br /&gt;......&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;}&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;Once this mode is setup , your error messages look like this ..&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;standalone&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;yes&amp;quot;&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;error&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt; &lt;span class="attr"&gt;xml:lang&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;en-US&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;An error occurred while processing this request.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;innererror&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;xmlns&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;An error occurred while updating the entries. See the InnerException for details.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.Data.UpdateException&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;stacktrace&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&amp;amp;#xD;
   at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)&amp;amp;#xD;
   at System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()&amp;amp;#xD;
   at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description)&amp;amp;#xD;
   at System.Data.Services.DataService`1.HandleRequest()&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;stacktrace&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;internalexception&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Violation of PRIMARY KEY constraint 'PK_Region'. Cannot insert duplicate key in object 'dbo.Region'.&amp;amp;#xD;
The statement has been terminated.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;message&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.Data.SqlClient.SqlException&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;stacktrace&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)&amp;amp;#xD;
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)&amp;amp;#xD;
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)&amp;amp;#xD;
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)&amp;amp;#xD;
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)&amp;amp;#xD;
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)&amp;amp;#xD;
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)&amp;amp;#xD;
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()&amp;amp;#xD;
   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)&amp;amp;#xD;
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;stacktrace&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;internalexception&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;innererror&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;error&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;pre class="csharpcode"&gt;This works well when you have a working service all prepped up and ready to go .&lt;br /&gt;&lt;strong&gt;But , what do you do when your service won't even start ?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;If the service fails to initialize due to some error , you get the generic WCF Error message,&lt;br /&gt;which is seldom useful .&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;The error message would look like this ..&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/DebuggingAdo.netDataServices_9B10/WCF_Generic_Error_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="264" alt="WCF_Generic_Error" src="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/DebuggingAdo.netDataServices_9B10/WCF_Generic_Error_thumb.jpg" width="644" border="0" /&gt;&lt;/a&gt; &lt;br /&gt;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&amp;#160;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;The reason you see this and not the Pretty formatted error message from astoria is that the astoria framework&lt;br /&gt;never initialized the service and failed in the WCF pipeline.&lt;br /&gt;To see the error at this layer , you will need to &lt;/pre&gt;

&lt;h5&gt;2) Configure your Servicebehavior with the &lt;a href="http://msdn.microsoft.com/en-gb/library/system.servicemodel.description.servicedebugbehavior.includeexceptiondetailinfaults.aspx" target="_blank"&gt;IncludeExceptionDetailInFaults&lt;/a&gt; attribute.&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;strong&gt;Via Code :&lt;/strong&gt;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]   
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; YourService : DataService&amp;lt;YourProvider&amp;gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;pre class="csharpcode"&gt;&lt;strong&gt;Via Config :&lt;br /&gt;&lt;/strong&gt;&lt;/pre&gt;
&lt;strong&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.serviceModel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;services&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;service&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ServiceNamespace.ServiceClassName&amp;quot;&lt;/span&gt;  &lt;span class="attr"&gt;behaviorConfiguration&lt;/span&gt; &lt;span class="kwrd"&gt;=&amp;quot;DebugEnabled&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;service&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;services&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;behaviors&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;serviceBehaviors&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;behavior&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;DebugEnabled&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;serviceDebug&lt;/span&gt; &lt;span class="attr"&gt;includeExceptionDetailInFaults&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;True&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;behavior&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;serviceBehaviors&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;behaviors&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;serviceHostingEnvironment&lt;/span&gt; &lt;span class="attr"&gt;aspNetCompatibilityEnabled&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;system.serviceModel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/strong&gt;

&lt;pre class="csharpcode"&gt;After setting this config , this is what the error  looks like ..&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/DebuggingAdo.netDataServices_9B10/WCF_Detailed_Error_6.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="232" alt="WCF_Detailed_Error" src="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/DebuggingAdo.netDataServices_9B10/WCF_Detailed_Error_thumb_2.jpg" width="736" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&amp;#160;&lt;br /&gt;Happy Debugging !&lt;/pre&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8617799" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Astoria/default.aspx">Astoria</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Debugging/default.aspx">Debugging</category></item></channel></rss>