Archive for category Uncategorized

Versioning REST Services

I’ve always been intrigued by the versioning problem. The versioning problem works like this: you build an API (Application Programming Interface) to get some work done. That API is successful and consumers ask for enhancements to the API. At this point, you have a problem: how do you support everyone who uses the current API while adding enhancements? If you have been down this road, you know that you have to solve the problem early-before success.

The way you handle versioning varies. With compiled code and libraries, you send out a new DLL/Assembly/JAR file and tell folks to convert existing code when they have a chance. With Web services, including SOAP, REST, and just HTTP accessible ones, you have other issues. You typically want a single code base to consume the old and updated messages. Given that many of us are now writing REST services, I talked to InformIT about the need for an article explaining how to version these JSON/XML speaking endpoints. That article is now live and available for your reading at http://www.informit.com/articles/article.aspx?p=1566460.

Leave a comment

Back at Work

Friends and folks in the .NET community learned that I got hit pretty hard by the flu last week. I was hospitalized for 3 days while they made sure I didn’t break any other internal systems. I just wanted to let folks know that I’m back at work. The rest of the family has been hit by the flu as well, though not as hard as me (thank goodness!). I appreciated all the offers of help from folks around me.

Other questions: was this H1N1? I don’t know, but I did get the vaccine cocktail for everything but H1N1. You do the math-it was either H1N1 or something that I just couldn’t handle.

Why was I hospitalized? I was losing fluids for 12 hours straight. That scared me and I went to the doctor. He thought it might be appendicitis. I went to the hospital and they ruled out appendicitis and a few other diseases. I was held mostly because of the severe loss of fluids to make sure it was only the flu.

Again, thanks to everyone who offered help. It is appreciated!

Leave a comment

I’m such a friggin idiot—Day of Mobile has that Jay Freeman!

I’m hosting a conference in Chicago called Day of Mobile. At the end of the day, we are having a big time keynote from Jay Freeman. These days, he’s a rock star in the iPhone universe. For some reason, and I’m a complete moron for not realizing this until a few minutes ago, he is the same guy who brought the .NET world this little tool: Anakrino. If you are scratching your head and wondering why that would impress me, it’s because I’m getting older and I’ve used every .NET tool in the world. Anakrino was a mainstay at Microsoft to decompile .NET assemblies to read in C# and VB.NET. It was easier to use than grabbing the source code.

So, in the last few minutes, I’ve gone from realizing that we had a really awesome iPhone ninja speaking at the conference to realizing that we had one of those legendary brainiac developers at the conference. And, while I didn’t want to pimp the conference to my usual .NET readers, I now feel I should because he’s such a part of all of our everyday lives as .NET developers. I mean, the guy who single handedly wrote Anakrino. Holy friggin’ crap!

Leave a comment

Article on JSON/XML Went up at InformIT.com

If you develop on ASP.NET MVC and have a need to return the ViewData as JSON and XML as well as HTML, take a look at the article I posted over at InformIT.com today (2/10/10): http://www.informit.com/articles/article.aspx?p=1554970. This is a shortcut to providing REST endpoints without doing a whole lot of extra work. This article doesn’t worry about versioning but does tackle the whole “how do I avoid screen scraping and extra coding” problem. Enjoy!

1 Comment

My first DimeCast is up!

Thanks to Derik Whittaker for bugging me to do one of these. It was fun. Go check it out. It’s a presentation on how to use the WebCam on Silverlight 4.

http://www.dimecasts.net/Casts/CastDetails/162

Leave a comment

Visual Studio 2010 Beta–>RC install and ASP.NET

I installed the VS 2010 RC last night, after uninstalling the VS 2010 Beta. If you see that ASP.NET isn’t coming up/working on your machine, you may need to remap the ASP.NET handlers into IIS. To do that, go to the Microsoft.NET 4.0 folder on your machine (typically C:WindowsMicrosoft.NETFramework64v4.0.30128 on a 64-bit Windows install, C:WindowsMicrosoft.NETFrameworkv4.0.30128 on a 32-bit install). Once in that folder, run the following:

aspnet_regiis.exe -i

After doing this, your ASP.NET applications should start running again. This is the same trick that worked on earlier versions of .NET, but it’s been a while since we had a .NET upgrade. I thought this might help out a few of you out there.

Leave a comment

DataContract, Partial Types and Generated Code

The other day, a friend had an issue with a DataContract. He had an Entity Model that used DataContract serialization. The code, stripped down to its essence, had something like this:

[DataContract]
public partial class SomeClass
{
    [DataMember]
    public int MyInt { get; set; }
}

 

And this was all well and good. When transmitting SomeClass over the wire, the fields on the entity model were transmitted. Now, my friend is really smart, and he knew that his version of serialization had implicit serialization. So, when adding new fields, he relied on implicit field serialization by augmenting SomeClass in a separate file as:

public partial class SomeClass
{
    public string MyString { get; set; }
}

 

Lo and behold, the field MyString didn’t appear on the wire. Why was this? This is because partial classes are a compiler trick that hides itself at the assembly level. The assembly indicated that SomeClass is a type that has the following C# implementation:

[DataContract]
public partial class SomeClass
{
    [DataMember]
    public int MyInt { get; set; }

    public string MyString { get; set; }
}
 

This shows that MyString is not part of the data contract, so the field doesn’t get serialized. At the assembly level, no one knows that you separated the implementation across two files.

So, next time you are wondering why part of your object isn’t being serialized, see if you made any declarations about serialization on the type. If you did, you have turned off the automatic “magic” parts of serialization. Sometimes, wizards will make those decisions for you. One way to recognize that the work was done for you: look to see if the generated type has [DataContract] on the partial class you are augmenting.

Leave a comment

Programmer Productivity

It’s been almost 4 years since I last programmed professionally with C++. C++ was my language of choice from 1994 through the end of 2000. It is a language that, once upon a time, I knew REALLY well. How well? I could read template errors and code the fix based on the build output-if you are a .NET developer, that’s kind of hard to do. Since then, I’ve done a lot of development with three languages: VB.NET, C#, and F#. Most recently, I was doing my hobbyist development in F#. I was impressed by the incremental boost in development productivity in F# over C#. The productivity boost at first was just noticeable and has recently become something where I can express F# algorithms in significantly less time that the C# equivalent.

Since mid November 2009, I started using C++ because a hobby project I wanted to work on required it. The project is a .NET profiler, and you can’t write a .NET profiler in .NET-you have to go to an unmanaged language. I noticed that I’m expressing C++ code at about the same rate as in F# and C#. At first, I was frustrated at the amount of functionality I could code in a few hours of C++ programming versus the same time slice in C#/F#. After talking to some friends, I was reminded that this is a thing that most people know but few people experience: each individual writes code at their own pace. Once they reach a level of expertise across 2 or more languages, they will write more functionality in the language that offers a higher level of abstraction. Unfortunately, not all languages can be used in all situations. The highest level of abstraction I can use for my profiler is C++.

If you do nothing else this year, learn a functional language to compliment the work you do in C#, VB.NET, Java, or C++. F#, Haskell, Clojure, Scala, and LISP all await to increase the amount of functionality you deliver every day. 

Leave a comment

Speaking Calendar: January 26-February 25

I’m going to be assisting the Microsoft office by doing some presentations of the PDC Roadshow.

January 26, 6 PM: Rockford .NET Users Group

February 3rd, 6 PM: Madison .NET Users Group

February 25, 6:30 PM: Lake County .NET Users Group

 

If you have a user group in the Wisconsin/Illinois/Indiana area and want to see this content, let me know. I can either come out to your group or help find a presenter who can deliver the content.

Leave a comment

Incorporating Video into a Silverlight 4 Application

html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightcontrolhost {
height: 100%;
text-align:center;
}

Over the past week, I spent some time with Silverlight 4. I’m really impressed by how easy it is to incorporate video into an application. This post shows how to capture video from the camera and display the video on the screen. The post also handles grabbing single frames of video. You might use this type of arrangement to allow users to upload images, hold impromptu web casts, and to do video conferencing.

I’ve uploaded a barebones application that turns on video capture and displays the video:

 

To understand how this works, you need to be familiar with the following objects:

CaptureSource

CaptureDeviceConfiguration

VideoBrush

The CaptureSource type encapsulates the methods needed to interact with audio and video devices.

CaptureDeviceConfiguration acts as gatekeeper to the webcam and microphone. CaptureDeviceConfiguration knows how to ask the user for permission to use the webcam or microphone. It also remembers what the user said about using the webcam or microphone during the current session.

VideoBrush knows how to paint using a video as the source.

In order to interact with the camera, you need to know whether the user gave you permission and if not, you have to ask for permission. Once you have permission, you just need to capture video from the default camera, create the brush, and start capturing the video.

private CaptureSource _captureSource = null;
private void btnStartCapture_Click(object sender, RoutedEventArgs e)
{
    if (_captureSource != null && 
        _captureSource.State == CaptureState.Started)
    {
        return;
    }
    if(!CaptureDeviceConfiguration.AllowedDeviceAccess)
    {
        if(!CaptureDeviceConfiguration.RequestDeviceAccess())
        {
            return;
        }
    }

    _captureSource = new CaptureSource {
         VideoCaptureDevice = 
 CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice()
       };
    var brush = new VideoBrush();
    brush.SetSource(_captureSource);
    _captureSource.Start();
    rectVideo.Fill = brush;
}

Later on, when the user wants to capture the current video as an image, you just do something like this to put the current frame into an image control:

 

private void btnGrabImage_Click(object sender, RoutedEventArgs e)
{
    if(_captureSource == null || _captureSource.State != CaptureState.Started)
    {
        return;
    }
    _captureSource.AsyncCaptureImage(
        image => imgCapture.Source = image
        );
}

As someone who spent way too much time in the C++/MFC era writing code to capture video from devices, I am incredibly impressed with the brevity of the code. I think the SL4 team did an awesome job here. Way to go!

 

You can grab the sample project here.

Leave a comment