Daily Archives: January 25, 2008

Creating and consuming a WCF service to test the EntityBag class

Danny Simmons has posted the code for his EntityBag class (aka “Perseus”) on the MSDN Code Gallery Site here.

I grabbed it last night and set up a test project so that I could see what the payload looks like.

The results of that test are in this blog post, Testing out the EntityBag.

There were a few tricks for getting the web service to work properly if you are not familiar with using WCF, so I wanted to show that code.

I started by creating a DLL library just to host my EDM.

Then I created a WCF Service. The WCF service references the EDM project and the Perseus project.

I created my interface along with a HelloWorld op which is always handy for testing.

<OperationContract()> _
   <ServiceKnownType(GetType(SalesOrderHeader))> _
   <ServiceKnownType(GetType(SalesOrderDetail))> _
 Function GetData(ByVal value As Integer) As Perseus.EntityBag(Of SalesOrderHeader)

 <OperationContract()> _
 Function HelloWorld() As String

There’s something critical in the above example. Without the ServiceKnownType declarations for the SalesOrderHeader and SalesOrderDetail, you will get an error when the service tries to serialize the return data. You can read more about this here. In C#, you use typeof() where I have GetType().

Then I created the methods that implement these interfaces in my Service class.

 Public Function GetData(ByVal value As Integer) As Perseus.EntityBag(Of SalesOrderHeader) _
  Implements IService1.GetData
    Using aw As New awEntities
      Dim order = (From ord In aw.SalesOrderHeaders.Include(“SalesOrderDetails”) _
                  Where ord.SalesOrderID = value _
                  Select ord).First
      Return aw.CreateEntityBag(Of SalesOrderHeader)(order) 
    End Using 
  End Function

  Public Function HelloWorld() As String Implements IService1.HelloWorld
    Return “Hello World”
  End Function

Next is the ConsoleApp that will call the service.

Nothing fancy here, I make a service reference to the service in the project and a references to the Perseus project and the project that hosts my EDM. In code, I instantiate the service, then call HelloWorld to make sure things are wired up properly and finally the GetData method passing in an  OrderNumber. Not sure why I didn’t use orderid. Just too focused on other things at the time!

   Dim ordEntityBag As Perseus.EntityBag(Of SalesOrderHeader)
   Dim svc = New ServiceReference1.Service1Client
   Dim s = svc.HelloWorld
   Try
     ordEntityBag = svc.GetData(71780)
   Catch timeout As TimeoutException
     svc.Abort()
   Catch commException As System.ServiceModel.CommunicationException
     svc.Abort()
   End Try

This returns an unmodified object. Next I wanted to see what it looked like if it was modified so I just added some code into the GetData method in the service.

Public Function GetData(ByVal value As Integer) As Perseus.EntityBag(Of SalesOrderHeader) Implements IService1.GetData
  Using aw As New awEntities
    Dim order = (From ord In aw.SalesOrderHeaders.Include(“SalesOrderDetails”) _
                Where ord.SalesOrderID = value _
                Select ord).First
    order.Comment = “12345”
    Dim i As Int32
    For Each sd As SalesOrderDetail In order.SalesOrderDetails
      sd.OrderQty = 25
      If i = 10 Or i = 12 Then
        sd.OrderQty = 0
      End If
      i += 1
    Next
    Dim sod = From s In order.SalesOrderDetails Where s.OrderQty = 0 Select s
    For i = sod.Count To 1 Step -1
      aw.DeleteObject(sod(i – 1))
    Next 
    Return aw.CreateEntityBag(Of SalesOrderHeader)(order)

  End Using

End Function

Here’s what the EntityBag for this looks like when it gets back to the client:

Entity Framework Toolkits and Extensions collection on MSDN Code Gallery

Zlatko wrote the eSQLBlast tool.

Danny‘s been writing Extension Methods and the ENtityBag class.

There is also a new tool for examining how code generation is done from an EDMX file. (As well as a blog by Sanjay about how code gen is done.)

All of these and future stuff are now collected under one umbrella, the Entity Framework Toolkits and Extensions page on the new MSDN Code Sharing site.

It also has an RSS feed, so you can see what gets added in the future.

Thanks Diego for pointing it out.

After three years, I discover how to choose a ClickOnce Icon in MageUI

This is completely insane. Someone from Romania emailed me about selecting an icon file for a ClickOnce deployment so that the application icon is used for the installation and for the shortcut.

He had tried my hack that I have blogged about, written about in CoDe Magazine and shown in conference presentations. The hack requires manual editing of the manifest file and I learned it in the forums.

I had never been able to discover any other way to do it.

There is a new version of the MAGEUI tool, the UI for building ClickOnce manifests, so I went in to see if there was anything different with respect to that.

On the file page, things looked the same as always:

So I was staring at it and looking at the file type column and wishing I could use it. But it’s always appeared to be disabled. While I was staring and thinking, I was clicking on that and on the second click, a drop down list appeared.

And when I clicked the arrow, there it was!

Understand, that in a normal UI property sheet like this, the arrow would be visible when you first click the cell.

So I went back to the previous version of MAGEUI and after two clicks, no dropdown. But guess what, one more click and there it was!

All this time? All of the blog posts I wrote, the article in CoDe Magazine. Nobody every emailed me to tell me “oh, here’s how”. You’d think…  Clearly it’s not THAT obvious! Oh well.

I haven’t ever seen this documented (though I sure wish there was a copy of Brian Noyes ClickOnce book nearby to see if he knew).

Well, there you have it.