# Monday, 31 October 2011

What’s new in .NET Framework 4.5

Just came across this great picture of what’s new in .NET Framework 4.5 (click for larger version):

WhatsNewNET45-en

#    Comments [4] |
# Wednesday, 05 October 2011

Visual Studio 2010: Debugging a x86 WCF service on a x64 machine

I just ran into an issue where I have a WCF service that depends on a .NET assembly that is compiled specifically for x86. In order to use that assembly I need to compile the service as a x86 service. No problem, but now when I want to test or add a service reference to this WCF service I ran into the problem that WcfSvcHost and WcfTestClient both will run a x64 because I’m running Windows 7 x64.

How to solve this? I found the answer in the forums and adapted the answer for my specific problem:

1.Copy WcfSvcHost.exe and WcfTestClient.exe from C:\program files (x86)\Microsoft Visual Studio 10.0\Common7\IDE to a local directory. Keep a backup copy of this file, of course.
2.Start a Visual Studio 2010 Command Prompt (one of the links from the start menu -> Visaul Studio 2010)
3."cd" to the directory where your copy of WcfSvcHost is located.
4.Execute the command "corflags /32BIT+ /FORCE WcfSvcHost.exe"
5.Copy the files back to where you found it.
 
Now your WcfSvcHost and WcfTestClient will be running in 32 bit mode.

#    Comments [3] |
# Tuesday, 18 January 2011

Entity Framework 4 : ContextOption.LazyLoadingEnabled

Entity Framework 4 enables lazy loading of entities by default. I’m not going to argue about whether that is good or bad, but there is something you should be aware of. If you’re like me, then over the years you’ll have learned that most classes that implement IDisposable should be used using a ‘using’ statement. This is especially true if you’re connecting to a database. So using Entity Framework your code may look something like this:

1: using  ( MyEntities  db = new  MyEntities () )
2: {
3:     var  query = from  e in  db.Employees
4:                 where e.EmployeeId == employeeId
5:                 select  e;
6: 
7:     return  query.SingleOrDefault();
8: }
9: 

This code will run fine and return a single employee. If however the Employee entity has a relationship things get a little interesting. Suppose the Employee has a relationship with a Department entity (via a property named ‘Department’). The department does not get loaded because db.LazyLoadingEnabled is true (by default).

Now suppose the above code is part of a WCF operation. The WCF operation want to return the Employee to the caller. Here is what happens: When WCF starts serializing the Employee object it sees the Department property and will try to access the property, since it setup to do lazy loading it will now go back to the database to load the department row. BUT hold on, the ‘using’-statement has already disposed of the object context and closed the database connection. What is the error that you’ll see? Well it starts with a CommunicationException: “The underlying connection was closed: The connection closed unexpectedly”.

image

This may throw you off and make you look for a WCF issue, but you’ll be looking in the wrong area. The actual cause of the error is on the server and the fact that the ObjectContext has been disposed yet is being used.

One solution would be to increase the lifespan of the object context and not dispose of it. This, to me, feels wrong. So instead I’ll just disable lazy loading.This can be done in the code:

1: using  ( MyEntities  db = new  MyEntities () )
2: {
3:     db.ContextOptions.LazyLoadingEnabled = false ;
4: 
5:     var  query = from  e in  db.Employees
6:                 where  e.EmployeeId == employeeId
7:                 select  e;
8: 
9:     return  query.SingleOrDefault();
10: }

Or you can go to the designer and change the default:

image

Note: I’m note sure why lazy loading enabled is set to ‘True’ by default (but it is on my machine). According to the documentation (http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontextoptions.lazyloadingenabled(VS.100).aspx) it should default to false.

#    Comments [5] |
# Wednesday, 21 May 2008

WCF serialization and RemotingFormat

Anyone familiar with building applications using .NET Remoting (in .NET 2.0) knows that the DataSet and DataTable classes have a property called RemotingFormat. This could be set to either XML or Binary where Binary would give you a performance boost since it is more compact.

Windows Communication Foundation does not use this property at all. The service binding dictates the serialization format, no matter what you set the RemotingFormat property to be, even if your using a NetHttpBinding.

The following configuration will setup a service to use .NET Binary formatting over Http:

<system.serviceModel>
  <bindings>
    <customBinding>
      <binding name="NetHttpBinding">
        <reliableSession />
        <compositeDuplex />
        <oneWay  />
        <binaryMessageEncoding  />
        <httpTransport />
      </binding>
    </customBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="ServiceBehavior" name="MyService">
      <endpoint address="" binding="customBinding"
        bindingConfiguration="NetHttpBinding" name="HttpBinding" contract="IService" />
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehavior">
        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
        <serviceMetadata httpGetEnabled="true"/>
        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

So even if your service looks something like this:

public DataSet GetData()
{
    DataSet ds = BuildDataSet();   // retrieve some data here
    ds.RemotingFormat = SerializationFormat.Xml;
    return ds;
}

The DataSet will still get serialized as a .NET binary as defined in the binding ('binaryMessageEncoding').

#    Comments [0] |
# Friday, 09 May 2008

Building a RSS aggregator using LINQ to Objects and WCF 3.5

Okay, so you read the title of this post. Perhaps you're expecting huge amounts of code, but guess what. As it turns out, this is so ridiculously easy. This will be a very short post.

Step one is to have a method that loads an RSS feed. WCF offers a new class called SyndicationFeed.

private SyndicationFeed Load( string url )

{

  XmlReader reader = XmlReader.Create( url );

  SyndicationFeed feed = SyndicationFeed.Load( reader );

  return feed;

}

 

The above method will take a url and use to load a feed. Now suppose I have a list of urls and I want to take all the items in all the feeds, sort them and use them to generate a new, aggregated feed. Sounds like a fair amount of work, right.

Here is the code:

 

private SyndicationFeed Aggregate( List<string> urls )

{

  var items = from url in urls

              from item in Load( url ).Items

              orderby item.PublishDate descending

              select item;

 

  SyndicationFeed feed = new SyndicationFeed( items );

  return feed;

}

 

Cool!

#    Comments [0] |
# Thursday, 27 March 2008

Learning WCF

A friend of mine asked me about what book I'd recommend for learning Windows Communication Foundation.

I've read 'Learning WCF' from Michele Leroux Bustamante and think it's great and I still use it as reference material.

#    Comments [0] |
# Sunday, 13 January 2008

System.ServiceModel.Web

In the .NET Framework, most of the time, the name of an assembly matches the namespace of the classes in that assembly.

Since WCF is kind of an add-on to the .NET 2.0 Framework this is not quite true for the assembly System.ServiceModel.Web.dll

Below a screenshot of what .NET Reflector shows to be inside this assembly.

As you can see this assembly extends a number of namespaces like System.Runtime.Serialization and System.Collections.ObjectModel.

The Json serialization classes are also in this assembly.

#    Comments [0] |
# Sunday, 11 November 2007

iSDC & RONUA Community Workshop

Yesterday I spoke at the iSDC and Ronua Community Workshop. We had a great day and it was a pleasure meeting the people at iSDC, meeting the developers in Cluj and surroundings (some drove over 200km to make it to the meeting) and Petru Jucovschi (the DPE for Microsoft Romania).

Here are the two presentations that I did:

11-09-2007 - Developing Windows Vista gadgets.pptx (431.43 KB)

11-10-2007 - 2008.NET.pptx (252.91 KB)

In the Visual Studio 2008 presentation I also talked about the work I've done for AOL. The AOL developer site can be found at http://dev.aol.com and for more reference material on Vista gadgets you can visit my AOL blog.

#    Comments [2] |
# Thursday, 20 September 2007

TRUVEO - Complete ASP.NET sample

I've completed a comprehensive sample application integrating Windows Communication Foundation, ASP.NET and TRUVEO search into a custom search engine. Check it out here.

#    Comments [0] |
# Wednesday, 22 August 2007

AOL Video - C#, REST and WCF 3.5

I've just posted a new blog entry on my AOL blog. I think it turned out really cool!

I use Windows Communication Foundation 3.5 (beta 2) to create a service contract, data contract and client channel and then connect to a non WCF, REST based service.

More on: http://dev.aol.com/node/595.

#    Comments [0] |
# Friday, 10 August 2007

Is SOAP over and done with?

Big companies, like Amazon and AOL, offering 'Software as a Service' seem to focus on REST more than on SOAP. Having a Microsoft background myself I found this strange... ASP.NET webservices work great, so therefor why not use SOAP?

The number one great thing about REST is the ability to easily test a service by... typing the URL in you browser. This will at least allow you to test the GET operation on your REST service.

Microsoft's number one dude on SOAP, Don Box seems to be moving away from SOAP himself (or is he running?). Below is an abstract of Don's words during Microsoft Tech Summit 07 taken from Ben Galbraith's blog. Read the full item here.

Q: What is the future of REST?

Don: Interesting word that means different things to different people, such as:

1. Get the WSDL and XSD out of my face
2. Get the SOAP out of my face
3. Put the URI in my face
4. Respect GET
5. Embrace PUT and DELETE

“It turns out a lot of the headache people have with Web Services or WS-* is tied to XSD. XSD is more flawed than most technologies that roam the earth. I was on the committee that created it, and that was back when I made my money explaining complicated technologies to people for money, and man, I could hear the cash registers ringing in my ears.”

“Now my job is making things simple, which is unfortunate since I’m stuck with XSD.”

“XSD was a standard-committee driven piece of ####ing crap.”

“If you’re Sun, if you’re Microsoft, if you’re IBM, you can just throw a bunch of engineers in a room and make it all work. Sun is committed to making their stuff interop with WCF with Project Tango. But if you’re Matz, or DHH, or Larry Wall, you’re screwed, because you don’t have time to build out this stack and then make it interoperate.”

#    Comments [0] |

Windows Communication Foundation 3.5 and REST/POX

Googling for some good examples on how to implement a REST style service using WCF does not provide many useful hits. Microsoft seems to not like the acronym REST very well instead using either the term POX (plain old XML) or 'the Web Programming Model' (WPM???).

Anyway Bryan Bell has a pretty decent post showing the very basics of REST/POX/WPM as made available using WCF 3.5.

Read more on: http://hyperthink.net/blog/2007/03/05/HTTPPOX+Programming+Basics.aspx.

#    Comments [0] |