in

Dé specialist in .NET trainingen en consultancy

Thomas Huijer - Compiler says no....

september 2011 - Posts

  • WCF and LINQ: beware of deferred execution

    When I was teaching WCF in Oslo this week, a student got an Exception from the client when calling this method on the server:
     
    public IEnumerable<Product> GetProductsForProductModel(int productModelID)         
    {
      using (AdventureWorks2008R2Entities context = new AdventureWorks2008R2Entities())
      {
        return
          from Product p in context.Product
          where p.ProductModel.ProductModelID == productModelID
          select p;
      }
    }
    

    At first there seemed nothing wrong with the code and I assmumed it was caused by the maxReceivedMessageSize setting. But increasing that didn’t help. Then it struck me…we are returning a query and not a resultset. So when the DataContractSerializer started to fetch the results from the query (when it was serializing our result), the used ObjectContext was already disposed. When we enabled Tracing and MessageLogging we found out that this was indeed the case: we got an ObjectDisposedException from the ObjectContext. The fix was easy, calling ToList() on the query before returning did the trick…

    Deferred execution is a great feature…sometimes it just bites you.

  • WCF Service in PreCompiled ASP.NET web application

    I got an email from a customer that reported a bugin the Web Deployment Tool 2010. Maybe it saves you some time if you ever encounter this. This is the translated text:

     

    When we deploy our webapplication, we precompile it first using the Web Deployment Tool 2010. While testing, we found out that the service could not be located. After some digging it turned out the problem was in the .compiled file of the service.

    See this post fromTom Fuller : http://social.msdn.microsoft.com/forums/en-US/wcf/thread/8c897f8e-2143-450e-a9f4-97d1f8702da7

    We solved it by using MSBuildTasks in the Web Deployment project (http://msbuildtasks.tigris.org/

    With that, we replacing the absolute location of the file with the “~” sign.

    <FileUpdate Files="..\..\Output\Web\Deployment\bin\documentservice.svc.989dc2fb.compiled" Regex="/Donau Web.csproj" ReplacementText="~" />

    We also remove all references to the ASP.NET folders like “App_”:

    <FileUpdate Files="..\..\Output\Web\Deployment\bin\documentservice.svc.989dc2fb.compiled" Regex="\|App_global.asax, Version=0\.0\.0\.0, Culture=neutral, PublicKeyToken=417399dd0d17e25b\|" ReplacementText="|" />

        <FileUpdate Files="..\..\Output\Web\Deployment\bin\documentservice.svc.989dc2fb.compiled" Regex="\|App_GlobalResources, Version=0\.0\.0\.0, Culture=neutral, PublicKeyToken=417399dd0d17e25b\|" ReplacementText="|" />