DCSIMG
Ido Flatow's Blog Veni Vidi Scripsi

Ido Flatow's Blog

Veni Vidi Scripsi

News

Have you heard me speak?
Powered
<style type='text/css' media='screen' id='sm_css'> #smix {overflow: visible;height: auto;border-radius: 10px;max-width: 250px;background-color: #323232;text-align: left;font-size: 12px;line-height: 16px;font-family:'Lucida Sans Unicode','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;} #smix a {color: #0056CC;text-decoration: none;} #smix .sm_head {color: #fff; line-height: 1em;font-size: 1.4em;padding: 10px;color: #fff;} #smix .sm_lanyard_wrapper {background-color: #fff;;clear: both;width: 97%;margin: 0 auto;margin-bottom: 0px;} #smix .sm_lanyard_content {padding: 7px;}#smix button.sm_rec, #smix a.sm_rec, #smix input[type=submit].sm_rec { padding: 6px 10px; -webkit-border-radius: 2px 2px;-moz-border-radius: 2px; border-radius: 2px; border: solid 1px rgb(153, 153, 153); background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(255, 255, 255)), to(rgb(221, 221, 221))); color: #333; text-decoration: none; cursor: pointer; display: inline-block; text-align: center; text-shadow: 0px 1px 1px rgba(255,255,255,1); line-height: 1; }#smix .sm_rec:hover { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(248, 248, 248)), to(rgb(221, 221, 221))); }#smix .sm_rec:active { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(204, 204, 204)), to(rgb(221, 221, 221))); }#smix .sm_rec.medium { padding: 3px 7px; font-size: 13px; }#smix .sm_rec span.icon.thumbs_up {background-position: 0px 36px;vertical-align: text-top;display: inline-block;margin-right: 4px;height: 18px;width: 16px;background-image: url(http://speakermix.com/images/new/thumbsold.png);}#smix .sm_rec:hover span.icon.thumbs_up {background-position: 0px 18px;} #smix .sm_events {padding:2px 0px 4px 0px;} #smix .sm_section {font-size: 10px; border-bottom: 1px solid silver; margin-bottom: 6px;} #smix .sm_subline {font-size:120%;margin-top:4px;font-weight:bold} #smix .powered {text-align: right} #smix .powered img {margin: 7px} </style>
Sela Technology Center

Advertisement

Wrapping up SDP Nov. 2012

logo

Wow, a conference that lasts 8 days, that a first. So here’s the gist of what I taught in 5 of these days:

  • What’s new in WCF 4.5
    In this 1-hour session I covered some of the important new features of WCF 4.5, such as Intellisense for configuration, UDP and WebSockets bindings, and improved support for streaming and compression.
  • Debugging the Web with Fiddler
    In this 1-day tutorial we saw how to use Fiddler to debug, test, and improve Web application. We saw how to work with the session list, use various inspectors, understand the meaning of statistics and timeline, manipulate messages with replays, breakpoints, and auto responder. We also saw how to create traffic with composer and how to create custom inspectors.
  • Building an App with Windows 8, Windows Azure and Visual Studio 2012 (with Michael Haberman)
    The purpose of this 1-day tutorial was to show an end-to-end development of a distributed application that has a Windows 8 client-side, Entity Framework for database access, ASP.NET Wep API for server-side services, and hosting the entire server-side in Windows Azure (including SQL Database).
    This tutorial day was not intended to be a deep dive into each technology, but rather show how the technologies work and fit together, and to discuss some best practices.
  • HTTP Fundamentals and ASP.NET Web API (with Yaniv Rodenski)
    You wanted a deep-dive? you got it. This 1-day tutorial, which we taught twice during the conference, covers a whole lot of HTTP and its implementation with ASP.NET Web API. We talked about the HTTP requests and responses, verbs, URIs, and mapping it to Web API routing, controllers, and actions. We talked about Web API binders, action filters, and message handlers. We talked about HTTP concepts, such as caching, streaming, compression, and persistent connections. We talked about security, IIS hosting, the HTTP.SYS listener, WebSockets, and about so much more.

All the slide decks and demo code from these days is available online at http://sdrv.ms/WhqCfT

See you in the next SDP.

BUILD 2012 – Day 4 Overview

First session of Day 4 was about advanced cloud service development. I came in a bit late so I missed the introduction, but the part about diagnostics and IIS 8 was nice – IIS 8 and Windows Server 2012 offer new stuff such as .NET 4.5 on web/worker roles, SignalR, and Websocket support. CDN and traffic manager cover was shallow, but the Service Bus part was nice where he showed service bus topics.

2012-11-02 08.52.27

Session number 2 was about Windows Azure Storage. There are a couple of new things coming to Azure Storage, and new stuff that will be available in a couple of weeks/months (see images).

2012-11-02 10.04.37

2012-11-02 10.31.33

2012-11-02 11.15.26-1

Session number 3 was about Windows Azure Service Bus. When the session began it seemed like an introduction session so I bailed. I understand that at the end of the session he mentioned that Service Bus now supports AMQP (advanced message queue protocol).

2012-11-02 12.44.45

Last session of the day/conference was about ASP.NET and VS 2012 by Scott Hanselman and John Galloway. Great session with great interaction between Scott and John, really worth watching. The new stuff which were introduced were the new features of ASP.NET which is going to be released soon – new Web API features, new templates for single-page web apps, SignalR as part of the Microsoft stack for ASP.NET, VS 2012 improvements, and on.

2012-11-02 14.30.17

So that’s it for Build. Continuing to the Windows Azure Insiders post-con where I learning new stuff that I can’t talk about Smile. You’ll find out about it soon…

BUILD 2012 – Day 3 Overview

Day 3 began with a great keynote session by Scott Hanselman, one hour that recapped my entire webdev history (being doing this stuff for the past 15 years).

The bottom line of the session was – remember that you also have a very powerful machine on your desk (or legs if it’s a laptop), so don’t just throw everything on the server, try to also use javascript to do stuff.

2012-11-01 08.30.14

After Scott’s session I stayed in the hall for the TypeScript session. Writing JavaScript with JavaScript – well, as long as it doesn’t like feel I’m writing JavaScript, it’s great (I don’t like JavaScript that much). I’m having a hard time finding thinking of a similar concept from previous technologies where you created a new language to hide syntax difficulties of another language (or in this case, use the same language for both), but the idea is nice, I’ll give it a couple of months to see if it catches up. Anyway, using TypeScript to create JavaScript code for Node.js is nice.

(Sorry, no photo)

I then moved on to a Windows Azure Internals session by Mark Russinovich. It’s the same session he had in TechEd 2012, but it was nice hearing it again. For server guys, looking at the internal of Windows Azure is what looking into the internals of Windows 8 is for client devs. In the session he also covered how they handled the famous 29-Feb (leap year) bug they had this year (note to self: don’t do nextyear = date.Year + 1 – it doesn’t work with leap years, duh!).

2012-11-01 12.00.45

This was actually my last session for the day, as I had lots of work piling up which had to be handled.

See you in tomorrows post about day 4.

2012-11-01 14.10.53

BUILD 2012 – Day 2 Overview

So day 2 started with a keynote that had “Windows Azure” written all over the stage, so that was promising (see overview of day 1).

2012-10-31 10.21.56

By the way, if you’ll compare the stage to yesterday’s keynote you’ll see the stage is empty – that is because all the demos are in the cloud Smile

Some of the stuff shown in the keynote:

  • Mobile Services.
    this feature was rolled out in August, but the demo was very good and managed to show the highlights of Mobile Services in a couple of minutes. Really great demo, I recommend you watch it.
  • Web API and SignalR.
    Most of these stuff are already known for a couple of months, but the release of SignalR in ASP.NET 4.5 is a bit new.
  • Media Services.
    A not so used feature of Windows Azure (at least by me), but sounds cool for people wanting to publish media through Windows Azure.
  • TFS.
    Team Foundation Services (not to be confused with the other TFS – Team Foundation Server) has been released. If you have a team of under 5 people the service is free, for bigger groups, it is currently free, but next year prices will be announced (free for MSDN subscribers).
  • Hadoop and HDInsight.
    There is a new SDK and HIVE is also queryable now with LINQ.

Some other announcements:

  • Caching – The Windows Azure Caching is out of preview and is now GA (general availability)
  • SDK 1.8 – The October SDK is out with some new stuff
  • .NET 4.5 in Cloud Services – Last week it was announced that Web Sites support .NET 4.5, and now Cloud Services (Worker/Web roles) also support it.

After this very long but cool keynote I went on to pick my sessions. Had to skip the first set of sessions because they were mostly introduction-level. After lunch I went on to see Mark Russinovich’s session about IaaS in Windows Azure (Virtual Machines and Virtual Networks). I’m familiar with some of this stuff, but hearing Mark is always much fun, and you also get to see some of the inner tools that are used by the Windows Azure team.

2012-10-31 14.50.32

My next session was the Advanced IaaS session. It was a lot more technical than the introductory session (duh). Using PowerShell and other Command-Line tools to control Virtual Machines in Azure.

2012-10-31 16.31.04

Sorry for the lousy picture, I sat in the back. Also for the odd slide, I missed the photo-op of the summary slide.

My last session for the day was about building highly available solutions in Windows Azure (part II). The session was nice, showing some tips for properly using retry policies for SQL Databases and Windows Azure Caching. There was also some tips about diagnostics in Windows Azure Web apps, nice content.

2012-10-31 18.15.34

Waiting for day 3 for more Windows Azure sessions, and for our Windows Azure Insiders Post-Conference.

BUILD 2012 – Day 1 Overview

Ok, first, just in case you aren’t familiar with my work – I’m a server/web/cloud guy, not a client guy, so all the hype about Windows 8 and Phone 8 sounds to me like “bla bla bla”. I’m into servers and Windows Azure, so day 2 and on is more to my like than Day 1.

So day 1 began with the keynote where Steve Ballmer did his “shopping channel” appearance, showing the various hardware running Windows 8.

2012-10-30 09.34.282012-10-30 09.09.48

2012-10-30 09.50.512012-10-30 09.33.362012-10-30 09.37.542012-10-30 09.41.092012-10-30 09.44.402012-10-30 09.47.00

BTW, in the first photo, on the left, you can notice an 82” surface Smile.

Then the keynote continued to showing off the new Windows Phone 8, and the announcement that the SDK has been released, yey (as mentioned, I’m not a client guy).

2012-10-30 09.36.27

The rest of the day was mostly Windows 8 / Phone 8 sessions, so I looked for the one session that was about .NET stuff, and found the Entity Framework session which was cool, since at the end of it we saw some of the new features of Entity Framework 6 which was released today in Alpha.

2012-10-30 15.03.112012-10-30 15.04.17

I really like some of the new features, especially the conventions that will make life easier for developers that have to deal with DBAs that are strict about database conventions.

The rest of the day was spent on talking to people about Windows Azure, and checking out our (Sasha Goldstein, Dima Zurbalev, and yours truly) new “Pro .NET Performance” book in the Microsoft Store (4th from the right):

2012-10-30 16.57.21

Oh, and we also got some SWAG – a new Windows 8 surface and a Windows 8 phone.

Day 2 and on is server and Azure, so good stuff to come…

Two months speaking spree

If you’ve been checking my blog in the last couple of weeks, you might have noticed I haven’t been posting much. In the past two months I have been traveling around the world, speaking in conferences and local user groups.

So to sum up this intensive, fun times, here’s a list of all conferences I visited and links to all the material I showed.

image_thumb12_thumb

  • What’s new in WCF 4.5
  • Building scalable, low-latency web apps with Windows Azure
  • Embracing HTTP with the ASP.NET Web API

You can download the slides and code samples from here

10-5-2012 6-52-54 PM

  • Debugging the web with Fiddler

  • Building scalable, low-latency web apps with Windows Azure
  • Debugging the web with Fiddler

You can download the slides and code samples from here

image

  • Embracing HTTP with the ASP.NET Web API

  • Building scalable, low-latency web apps with Windows Azure

You can download the slides and code samples from here

image

  • Building scalable, low-latency web apps with Windows Azure
  • The new face of ASP.NET: ASP.NET MVC, Razor, and jQuery
  • Embracing HTTP with the ASP.NET Web API

You can download the slides and code samples from here

As for future conferences, I’m going to have a couple of sessions and tutorials in the SDP (Sela Developer Practice) conference which will be in November in Israel. I will have a 1-hour session about the new features of WCF 4.5, and the following three tutorial days:

image

 

So to sum up this bunch of conferences and the previous one:

1 year, 3 continents, 7 countries, 9 conferences, 24 sessions.

That’s it for 2012 conferences.

kick it on DotNetKicks.com Shout it

Slides and demo from my Azure UG session

Early this week I had a session about building scalable, low-latency, secured web applications with Windows Azure.

During the session, and despite all the network problems we had, I showed how to migrate an ASP.NET 4 MVC app to Windows Azure and incorporate the following features:

  • Cloud services (compute)
  • Storage
  • CDNs
  • Full IIS support
  • Diagnostics
  • ACS and claim-based identities with WIF
  • SQL Azure
  • Caching worker roles

The demo code and the few slides I shown are available online at: http://sdrv.ms/OmSA2n

I’m going to have this session three more times during the next couple of months in the US, Germany, and Denmark, so if you happen to attend one of these places, come and say hi.

Also, if anyone wish to have me speak at their local user group while I’m attending these conferences, let me know via email or Twitter (@IdoFlatow).

image_thumb12

kick it on DotNetKicks.com Shout it

Fixing issues with IIS Express 8 after installing Windows Azure SDK 1.7

If you are having problems using IIS Express 8 to host local Azure deployments after installing the Windows Azure SDK 1.7, you should probably continue reading this…

One of the new features of the Azure SDK 1.7 is the support for running local emulator with IIS Express 8 instead of the full IIS. This is supported for both Windows 7 and Windows 8.

At first when I tried this feature it simply didn’t wok. I previously had IIS Express 7.5 on my machine, and after installing the new SDK, I created a new web role, and when trying to run it locally, IIS Express did not fire up, and the browser opened showing it is unable to locate the requested address.

I then uninstalled the SDK, followed by uninstalling IIS Express. After making sure I don’t have any IIS Express “leftovers” in my machine, I reinstalled the Azure 1.7 SDK, making sure it also installed the new version of IIS Express.

Next, I opened the previously created project, and this time when I tried to run it, the following message appeared:

“The Windows Azure Emulator could not start the web server. The properties for this project specify that it should run on IIS Express, but no compatible version of IIS Express was found. To run this project, either install IIS Express 8 or higher, or change the ‘Use web server’ property for this project to ‘Local IIS web server’. See output window for more information.”

Since IIS Express 8 was successfully installed and it did work (VS 2010 SP1 was able to run web sites on it), it was time for the big guns – using reflector to find out what csrun.exe does. It seems that one of the first things csrun does is to create a COM object called Microsoft.IIS.VersionManager to check which version of IIS is installed on the machine. After opening the registry and finding the key, I discovered that I don’t have permission to read the key. Eureka!

Following this forum thread, I was able to add permissions to the key. After setting the permissions, I returned to Visual Studio, and this time I had no problem getting the emulator to use IIS Express 8 to host my web site. Problem solved!

Note: If you need to look for the registry key, it is located under HKEY_LOCAL_MACHINE\Software\Microsoft\Classes\Microsoft.IIS.VersionManager (make sure you also check permission on the second key, Microsoft.IIS.VersionManager.1)

Until the next bug…

kick it on DotNetKicks.com Shout it

Hi, my name is Ido Flatow, and in this session we will talk about…

In the next couple of months, I’m going to say that phrase many times.

My speaking engagements last year at VSLive in Redmond and Orlando, and in the North-American MCT Summit in San-Francisco marked my first steps into the world of becoming an international speaker. Until that time, most of my speaker experience came from speaking back home in Israel in courses and conferences, and here and there teaching a course abroad in Sela’s branches in the world. Speaking in a conference is somewhat similar to a course, only 10x times the size of the audience, and more pressure since you only have one hour to make a good impression instead of an entire week.

And now it’s time for the 2012 conferences. These next couple of months are going to be quite hectic, with conferences worldwide, and me skipping from one continent to the other. So here is my appearance list, just in case you are one of my groupies and wish to attend each of my sessions Smile.

image

March 25-29 – Sela Developer Practice (SDP). Sela, Israel.

In our annual conference I will deliver two workshops:

1. Advanced WCF – In this 1-day workshop I will show monitoring techniques for WCF, discuss about WCF performance, demonstrate various WCF security concepts, and show some useful tips for accessing WCF from client applications. In addition, I will demonstrate how to extend WCF by creating custom behaviors.

2. Debugging the web with Fiddler – In this 1-day workshop I will show you how to use Fiddler from bottom to top: how to inspect sessions, filter requests and responses, check statistics and timeline, use the composer and auto-responder, and how to create your own inspector and custom handler for Fiddler.

gids

April 17-20 - Great Indian Developer Summit (GIDS). Bangalore, India.

This will be my first appearance in GIDS, and my first time to India, so I’m twice as happy to speak at the conference. In GIDS I will have four sessions:

1. What’s new in WCF 4 – simplified configuration, better IIS hosting, routing and discovery services and more. Many groups are still in the process of migrating from previous versions of .NET (1.1, 2, 3.5) to .NET 4, so if you are migrating to WCF 4, or already using it and want to learn more about it, this session is for you.

2. What’s new in WCF 4.5 – UDP transport, WebSocket support, configuration Intellisense, streaming improvements, etc.. If you want to learn about the new features which will be available late this year in .NET 4.5 you should definitely come (or at the least start by reading my posts about it).

3. ASP.NET MVC Razor And jQuery: The New Face Of ASP.NET – so many new frameworks and concept that will make every web developer’s head spin. Unfortunately this is only a 1-hour session, so we won’t be able to touch ground on all the technologies, but at least we will have enough time to talk about MVC, jQuery, and Razor, as the new way for building web applications.

4. Introducing The Windows Azure HPC Scheduler – doing HPC using Windows HPC Server? with the new release of the Windows Azure HPC Scheduler, you can migrate your entire solution to the cloud without using any local cluster, and use the Windows Azure cloud elasticity to increase/reduce resources as needed.

image

May 14-17 – Visual Studio Live (VSLive). New York, USA.

This will be my third appearance at VSLive, but I’m as excited as ever, as I will be presenting three sessions, back-to-back, all of them about WCF:

1. What's new in WCF 4 – as mentioned before, a very useful session if you are currently migrating to .NET 4.

2. What’s new in WCF 4.5 – just imagine being in a conference room and listening to 2 hours of new WCF features (4 & 4.5). If I were you, I would just wait to get back to the office to try it out.

3. Monitoring and troubleshooting WCF services – trace files, message logs, ETW, WMI, performance counters, AppFabric monitoring … Come and see all the ways for monitoring your WCF services. In addition we will discuss various aspects of instance modes and concurrency in WCF, and go over many of the settings in WCF that can affect the performance of your services.

NDC_logo_date_2012

June 6-8 – Norwegian Developers Conference (NDC). Oslo, Norway.

First time to NDC, but this time I will not fly alone. My colleague, Yaniv Rodenski, is also a speaker at NDC where he will have a session on Hadoop on Windows Azure. As for me, I will only have one session this time, so I’ll have more time to visit Oslo:

Debugging the Web with Fiddler – learn to use Fiddler, the famous Web debugger, to inspect requests and response, catch message as they pass and alter them, create responses without ever reaching the server, and write your own extensions to Fiddler. I recommend you install Fiddler on your laptop prior to the session so you can try it out during my talk.

image

August 6-10 – Visual Studio Live (VSLive). Redmond, WA, USA.

Back again to the Microsoft campus on Redmond, this will be my fourth VSLive conference, and second time as a speaker in the Redmond event. Luckily for me, I will not have to endure the 16 hours of flight (not including layovers) alone, since I will be accompanied by Gil Fink, another one of my colleagues, who is also speaking at VSLive on HTML5 and Entity Framework. As for me, my sessions are:

1. Building secured, scalable, low-latency web applications with the Windows Azure Platform – in the past years, whenever you saw a session about Web apps in Windows Azure, you always got a lot of slides, and a bit of demo code. Well no more! this is a 1-hour code only session where I will show how to create a Web application for Windows Azure that utilizes everything Azure has to offer – compute, storage, CDN, ACS, AppFabric Cache, SQL Azure, Full IIS, WCF worker roles and more.

2. Embracing HTTP with ASP.NET Web APIs – although I’ve always been the “WCF guy”, I’m in the business of Web technologies, and you can’t build proper Web apps without harnessing the full power of the HTTP protocol. In this session I will show you what the new ASP.NET Web API is all about, and how you can use it to build HTTP services.

That’s the agenda for now, and I guess that there will be more updates in the next couple of weeks. My calendar for September-December is still opened, so if you want to book me for a conference, let me know.

I will also tweet with live updates from the conferences, so don’t forget to follow me @IdoFlatow

kick it on DotNetKicks.com Shout it

What’s new in WCF 4.5? WebSocket support (Part 2 of 2)

It’s time for post No. 12 in the WCF 4.5 series. Part 1 of 2 was about WebSocket support with SOAP-based messages. Part 2 is about WebSocket support with plain text messages that enables the interaction between web browsers and WCF.

Previous posts:

1. What’s new in WCF 4.5? let’s start with WCF configuration

2. What’s new in WCF 4.5? a single WSDL file

3. What’s new in WCF 4.5? Configuration tooltips and intellisense in config files

4. What’s new in WCF 4.5? Configuration validations

5. What’s new in WCF 4.5? Multiple authentication support on a single endpoint in IIS

6. What’s new in WCF 4.5? Automatic HTTPS endpoint for IIS

7. What’s new in WCF 4.5? BasicHttpsBinding

8. What’s new in WCF 4.5? Changed default for ASP.NET compatibility mode

9. What’s new in WCF 4.5? Improved streaming in IIS hosting

10. What’s new in WCF 4.5? UDP transport support

11. What’s new in WCF 4.5? WebSocket support (Part 1 of 2)

If you haven’t read part 1, please go over it first so you can get the gist about WebSockets, NetHttpBinding, and how it is used in WCF.

In part 1 I demonstrated how to create both binary encoded SOAP bindings and text encoded SOAP bindings with WebSockets. Problem is that in JavaScript it can get difficult to create and parse SOAP messages - this is why we tend to use XML/JSON based bindings (such as WebHttpBinding) instead of SOAP-based bindings (BasicHttpBinding/WsHttpBinding) when calling WCF services from JavaScript.

Creating a duplex service with WebSockets, NetHttpBinding, and plain text messages, is just like creating any other WCF service:

  1. Define the contract and callback contract
  2. Implement the service
  3. Configure the host
  4. Consume the service from a client app

First we will create our contract. Since we need to receive and send messages, we will create a duplex contract, each contract with a single method which we will mark with action=”*”:

Contracts
  1. [ServiceContract]
  2. public interface IWebSocketEchoCallback
  3. {        
  4.     [OperationContract(IsOneWay = true, Action = "*")]        
  5.     void Send(Message message);
  6. }
  7. [ServiceContract(CallbackContract = typeof(IWebSocketEchoCallback))]
  8. public interface IWebSocketEcho
  9. {
  10.     [OperationContract(IsOneWay = true, Action = "*")]
  11.     void Receive(Message message);
  12. }

The echo service itself is a simple implementation that receives the message and sends it back to the client:

EchoService
  1. public class EchoService : IWebSocketEcho
  2.     {
  3.         IWebSocketEchoCallback _callback = null;
  4.         public EchoService()
  5.         {
  6.             _callback =
  7.                 OperationContext.Current.GetCallbackChannel<IWebSocketEchoCallback>();
  8.         }
  9.         public void Receive(Message message)
  10.         {
  11.             if (message == null)
  12.             {
  13.                 throw new ArgumentNullException("message");
  14.             }
  15.             WebSocketMessageProperty property =
  16.                 (WebSocketMessageProperty)message.Properties["WebSocketMessageProperty"];
  17.             WebSocketContext context = property.WebSocketContext;
  18.             var queryParameters = HttpUtility.ParseQueryString(context.RequestUri.Query);
  19.             string content = string.Empty;
  20.             if (!message.IsEmpty)
  21.             {
  22.                 byte[] body = message.GetBody<byte[]>();
  23.                 content = Encoding.UTF8.GetString(body);                
  24.             }
  25.             // Do something with the content/queryParams
  26.             // ...
  27.             
  28.             string str = null;
  29.             if (string.IsNullOrEmpty(content)) // Connection open message
  30.             {
  31.                 str = "Opening connection from user " +
  32.                     queryParameters["Name"].ToString();                
  33.             }
  34.             else // Message received from client
  35.             {
  36.                 str = "Received message: " + content;                
  37.             }
  38.             _callback.Send(CreateMessage(str));            
  39.         }
  40.         private Message CreateMessage(string content)
  41.         {
  42.             Message message = ByteStreamMessage.CreateMessage(
  43.                 new ArraySegment<byte>(
  44.                     Encoding.UTF8.GetBytes(content)));
  45.             message.Properties["WebSocketMessageProperty"] =
  46.                 new WebSocketMessageProperty
  47.                 { MessageType = WebSocketMessageType.Text };
  48.                         
  49.             return message;
  50.         }
  51.     }

The Receive method handles two types of calls:

1. The first “connection upgrade” message – when the client first connects to the service and tries to upgrade the connection from HTTP to WebSocket. In this call the request is sent using HTTP GET, and therefore there is no body, but we can access the URL’s query string.

2. The second message and on are the messages being sent by the client over the WebSocket transport – these messages contain a message body, with no special query string.

Line 5-9 shows how to create a standard duplex service by storing the callback channel in a local variable. The callback channel will be used later on in the code in order to send messages back to the client. The service uses the default instancing mode which is PerSession, so a new instance will be created for each client, and the local variable will point to a different callback channel in each service instance.

Lines 17-27 demonstrates the technique of parsing the message – either by checking its query string or by reading the byte array from the message and transforming it to a string.

Lines 32-43 checks which type of message is being handled, the first connection request, or a consequent message from the client. In each case the service responds by echoing the message back to the client.

Line 46-56 demonstrates how to create a Message object with a simple string content when using the byte stream encoding.

Note: to use the ByteStreamMessage type, add a reference to the System.ServiceModel.Channels assembly.

Note: WebSocket messages can be either text or binary, so if you are planning on using binary messages you will need to change the code to work with byte arrays instead of strings.

Now that we have the contracts and the service, we need to define our host and endpoint. In this example I will use IIS as the host and I will use the routing mechanism of ASP.NET to create a service URL address that doesn’t contain the annoying “.svc” extension. The following global.asax code shows how to do that:

Global.Asax
  1. public class Global : System.Web.HttpApplication
  2. {
  3.     protected void Application_Start(object sender, EventArgs e)
  4.     {
  5.         RouteTable.Routes.Add(new ServiceRoute("echo",
  6.             new ServiceHostFactory(),
  7.             typeof(EchoService)));
  8.     }
  9. }

And now for the endpoint configuration. Since NetHttpBinding uses SOAP messages, and there is no “WebSocketHttpBinding” for passing plain byte streams, we need to create a custom binding that will allow us to receive messages over WebSocket where the message can either be a text message or a binary message (the WebSocket API supports both types).

The standard encodings of WCF - text, binary, and MTOM, will not enable us to receive non-SOAP byte streams, that is why we need to use a new encoding which was introduced in WCF 4 – the ByteStreamMessageEncoding.

The following endpoint and binding configuration will allow us to open a WebSocket listener that receives simple byte streams:

Service Configuration
  1. <system.serviceModel>
  2.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  3.                                multipleSiteBindingsEnabled="true" />
  4.     <services>
  5.       <service name="UsingWebSockets.EchoService">
  6.         <endpoint address=""
  7.                   binding="customBinding"
  8.                   bindingConfiguration="webSocket"
  9.                   contract="UsingWebSockets.IWebSocketEcho" />
  10.       </service>
  11.     </services>
  12.     <bindings>
  13.       <customBinding>
  14.         <binding name="webSocket">          
  15.           <byteStreamMessageEncoding/>            
  16.           <httpTransport>            
  17.             <webSocketSettings transportUsage="Always"
  18.                                createNotificationOnConnection="true"/>
  19.           </httpTransport>
  20.         </binding>
  21.       </customBinding>
  22.     </bindings>
  23.   </system.serviceModel>

The important part in the configuration is lines 13-21:

1. We set the transportUsage to Always to force the usage of WebSocket rather than HTTP.

2. We set the createNotificationOnConnection to true to allow our Receive method to be invoked for the connection request message (the first GET request which is sent to the service).

3. We use the byteStreamMessageEncoding which allows the service to receive simple byte streams as input instead of complex SOAP structures.

To test our code we can add an HTML page to our project. The following code is based on the StockTicker demo from the HTML5 Labs website:

Echo Client
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title>Echo Demo</title>
  5.     <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
  6.     <script>
  7.         $(document).ready(function () {
  8.             if (!window.WebSocket && window.MozWebSocket) {
  9.                 window.WebSocket = window.MozWebSocket;
  10.             }
  11.             $('#echoForm').submit(function (event) {
  12.                 $('#echoForm')
  13.                     .add('#echoForm > *')
  14.                     .attr('disabled', 'disabled');
  15.                 var uri = 'ws://' + window.location.hostname +
  16.                     window.location.pathname.replace('EchoDemo.html', 'echo') +
  17.                     '?Name=' + $("#name").val();
  18.                 connect(uri);
  19.                 event.preventDefault();
  20.             });
  21.         });
  22.         function connect(uri) {
  23.             $('#messages').prepend('<div>Connecting...</div>');
  24.             var websocket = new WebSocket(uri);
  25.             
  26.             websocket.onopen = function () {
  27.                 window.focus();
  28.                 $('#echoForm').hide();
  29.                 $('#outputArea').show();
  30.                 window.setInterval(function()
  31.                 {
  32.                    websocket.send("the time is " + new Date());
  33.                 }, 1000);                
  34.                 $('#messages').html(
  35.                     '<div>Connected. Waiting for messages...</div>');
  36.             };
  37.             websocket.onclose = function () {
  38.                 if (document.readyState == "complete") {
  39.                     var warn = $('<div>').html(
  40.                         'Connection lost. Refresh the page to start again.').
  41.                         css('color', 'red');
  42.                     $('#messages').append(warn);
  43.                 }
  44.             };
  45.             websocket.onmessage = function (event) {
  46.                 $("#messages").append(event.data + "<br>");
  47.             };
  48.         };
  49. </script>
  50. </head>
  51. <body>
  52.     <form id="echoForm" action="">
  53.     <input type="text" id="name" placeholder="type your name" />
  54.     </form>
  55.             <div id="outputArea" style="display: none">
  56.         <div id="messages" style="height: 80%; overflow: hidden">
  57.         </div>
  58.     </div>
  59. </body>
  60. </html>

Most of the above code is jQuery stuff to handle the incoming message, so let’s point out the important parts:

Lines 18-20 – In these lines we create the URI of the service. Note the use of the ws:// scheme – this is the scheme of WebSocket, but it works just fine even when our service base address is set to HTTP.

Lines 27-56 – the connect function basically does all the rest. The WebSocket functions are based on the WebSocket API

  • Line 30 – create the WebSocket object
  • Lines 32-42 – open the connection
  • Lines 36-49 – run a function every 1 second that sends the current time to the service
  • Lines 44-51 – handle the WebSocket channel closing
  • Lines 53-55 – handle a received message (a message sent from the service to the client)

Running the client will show the following output:

image

To conclude, in order to create a service that can receive and send message to browsers using WebSockets we need to do the following:

  1. Create the duplex contract which contains a simple Receive and Send methods (or any other names you like).
  2. Implement the contract in a service like you’ll implement any other duplex service. The only thing you need to take care of is how to read and write the message.
  3. Create an endpoint which uses a custom binding which supports WebSockets and simple byte-stream encoding.

Although the above works quite well, there is another way to create this type of service – by creating a service class that inherits from the Microsoft.WebSockets.WebSocketService type. The Microsoft.WebSockets package, available from NuGet, enables the creation of WebSocket-based services. Once inheriting your service from WebSocketService, you can override methods such as OnMessage, OnOpen, OnClose, and OnError. Working with these methods is quite easy, as demonstrated in the following code:

EchoService2
  1. public class EchoService2 :
  2.      Microsoft.ServiceModel.WebSockets.WebSocketService
  3.  {
  4.      public override void OnMessage(string message)
  5.      {
  6.          string str = "Received message: " + message;
  7.          Send(str);
  8.      }
  9.      
  10.      public override void OnOpen()
  11.      {
  12.          var queryParameters = this.QueryParameters;
  13.          string str = "Opening connection from user " +
  14.              queryParameters["Name"].ToString();
  15.           Send(str);                        
  16.      }
  17.      protected override void OnClose()
  18.      {
  19.          base.OnClose();            
  20.      }
  21.      protected override void OnError()
  22.      {
  23.          base.OnError();            
  24.      }
  25.  }

As you can see, in this case you don’t have to work directly with byte arrays. In order to host this service you also don’t need to define a special endpoint configuration, as this package includes a WebSocketHost class that automatically creates and configures a WebSocket endpoint. To create a WebSocketHost and provide it to IIS, we need to create a class that inherits from ServiceHostFactory, as demonstrated in the following code :

WebSocketServiceHostFactory
  1. public class WebSocketServiceHostFactory : ServiceHostFactory
  2.   {
  3.       protected override ServiceHost CreateServiceHost(Type serviceType,
  4.           Uri[] baseAddresses)
  5.       {
  6.           var host = new WebSocketHost(serviceType, baseAddresses);
  7.           
  8.           host.AddWebSocketEndpoint();
  9.           return host;
  10.       }        
  11.   }    

Note: the ServiceHostFactory is declared in the System.ServiceModel.Activation assembly, so don’t forget to add a reference to it.

Once we have the new factory, we can register it with the routing mechanism (lines 5-7):

Global.Asax
  1. public class Global : System.Web.HttpApplication
  2.   {
  3.       protected void Application_Start(object sender, EventArgs e)
  4.       {
  5.           RouteTable.Routes.Add(new ServiceRoute("echo2",
  6.               new WebSocketServiceHostFactory(),
  7.               typeof(EchoService2)));
  8.           RouteTable.Routes.Add(new ServiceRoute("echo",
  9.               new ServiceHostFactory(),
  10.               typeof(EchoService)));
  11.       }
  12.   }

All is left is to change the client HTML code in line 19 to call the ‘echo2’ service instead of ‘echo’.

You can see more examples on how to use this package in Paul Batum’s blog post, and in his //BUILD session.

So as you can see, it is quite easy to create a WCF service that can receive messages from a browser and push messages to a browser by using WebSockets. Farewell long polling, I hope we never meet again Smile

You can download the above code (both versions) from my SkyDrive. The source code also includes a sample self-hosted WebSocket service and an HTML page that uses it instead of the IIS-hosted service.

WCF or ASP.NET Web APIs? My two cents on the subject

A couple of weeks ago (around Feb. 16) the WCF WebAPIs - a framework for building RESTful/Hypermedia/HTTP services, which was in development over the past 1.5 years as a side-project on CodePlex, has been formally integrated into ASP.NET and its name changed to the ASP.NET Web API.

These past two weeks, there has been a lot of questions among WCF developers: What does it mean that the Web APIs are no longer a part of WCF – is WCF dead? Has SOAP gone bankrupted? is HTTP the new way to go for interoperability?

To get a better understanding of what happened and what is the way to go, we need to answer a couple of questions:

1. What is the purpose of the WebAPIs?

2. Why do we need REST HTTP services? What’s wrong with SOAP-over-HTTP?

3. Why did the WebAPIs move from WCF to ASP.NET MVC?

4. Is there still a use for WCF? When should I choose Web APIs over WCF?

What is the purpose of the WebAPIs?

When WCF was conceived back in its Indigo and .NET 3 days, the main goal was to support SOAP + WS-* over a wide variety of transports. However, over time it became clear that although SOAP is wide spread and supported on many platforms, it is not the only way to go when creating services. There is also a need to also support non-SOAP services, especially over HTTP, where you can harness the power of the HTTP protocol to create HTTP services: services that are activated by simple GET requests, or by passing plain XML over POST, and respond with non-SOAP content such as plain XML, a JSON string, or any other content that can be used by the consumer. Support for non-SOAP services was very much needed in WCF back then, mostly because some clients, such as web browsers, were not that suitable to handle SOAP messages (plenty of XML parsing and DOM manipulation).

So in WCF 3.5 we got the WebHttpBinding – a new binding that helped us create this kind of non-SOAP service over HTTP, better known as a RESTful service.

The WebHttpBinding was not enough, and after WCF 3.5 was released, a new set of tools was created – the WCF REST Starter Kit. The REST starter kit was an attempt to enrich the support of WCF 3.5 for HTTP services – add better client-side support for .NET apps, extend the server side support for other content types, enable response and request caching, inspection of messages and so forth. Unfortunately, this great toolkit was never officially released and ended its product cycle as “Preview 2”, although it’s still being used today in some of Microsoft’s products that are built with .NET 3.5.

Although not released, some of the service-side features of the REST starter kit were integrated into WCF 4 – we didn’t get any of the client-side libraries, but we did get most of the service-side features (excluding the new inspectors). Some were well-integrated into WCF while others required the use of ASP.NET (by turning on the ASP.NET compatibility mode).

So with WCF 4 we had some support for “Web” HTTP services, but it wasn’t that perfect – to get some of the features you needed IIS hosting and ASP.NET, not all types of requests were supported easily (ever tried posting HTML form data to a WCF HTTP service?), the overuse of CLR attributes to define the POST/GET/PUT/DELETE was tedious, not to mention the configuration required to create this type of services with all of the endpoint behavior. And even after all of that we didn’t actually get full control over the HTTP messages.

That was the main goal of the Web APIs, known back then as the WCF Web APIs: to stop looking at HTTP through the eyes of WCF - as just a transport protocol to pass requests. Rather, it allows us to look at it as the real application-level protocol it is – a rich, interoperable, resource-oriented protocol. The purpose of the Web APIs was to properly use URIs, HTTP headers, and body to create HTTP services for the web, and for everyone else that wished to embrace HTTP as its protocol and lifelong friend.

Why do we need REST HTTP services? What’s wrong with SOAP-over-HTTP?

The world of SOAP and the world of HTTP services are very different. SOAP allows us to place all the knowledge required by our service in the message itself, disregarding its transport protocol, whether it is TCP, HTTP, UDP, PGM, Named Pipes… But unlike TCP, UDP and the other level 4-5 protocols, HTTP is an application-level protocol, and as such it offers a wide variety of features:

  • It supports verbs that define the action - query information using GET, place new information and update existing using POST or PUT, remove information using DELETE etc.
  • It contains message headers that are very meaningful and descriptive - headers that suggest the content type of the message’s body, headers that explain how to cache information, how to secure it etc.
  • It contains a body that can be used for any type of content, not just XML content as SOAP enforces (and if you want something else – encode it to base64 strings and place it in the SOAP’s XML content). The body of HTTP messages can be anything you want – HTML, plain XML, JSON, binary files (images, videos, documents…) …
  • It uses URIs for identifying both information paths (resources) and actions – the URI templates initiative is catching on and is rapidly becoming the standard way of representing requests for resources and hypermedia URIs.

The use of HTTP has evolved over the years. Application-level protocol architectural styles such as REST Hypermedia APIs have emerged on top of HTTP. These, in turn, harness the power of HTTP to create resource-oriented services, and better define the stateless interaction between clients and services.

The Web APIs therefore were intended to allow all of these approaches – you can use it to create HTTP services that only use the standard HTTP concepts (URIs and verbs), and to to create services that use more advanced HTTP features – request/response headers, hypermedia concepts etc.

So HTTP is a lot more than a transport protocol. It is an application-level protocol, and the fact is that although many platforms know how to use SOAP, many more platforms know how to use HTTP! among the HTTP supporting platforms which do not support SOAP that well are the browsers – probably the most important platforms for web developers (and users). And if you don’t believe me that REST and hypermedia are useful, maybe Martin Fowler can convince you better than me.

This, of course, does not mean that SOAP is redundant – SOAP is still useful for building messages when you don’t have an alternative application-level protocol at your disposal, or when you want to use SOAP across the board while considering HTTP as no more than another way to pass messages (for example, use HTTP because it can cross firewalls more easily than TCP).

Why did the WebAPIs move from WCF to ASP.NET MVC?

Back to the story of WCF and the WCF Web APIs (we are still before the merger). Another goal of the WCF Web APIs was to incorporate known concepts that would help developers to overcome some of the drawbacks they faced with WCF, such as huge configurations, overuse of attributes, and the WCF infrastructure that did not support testing well. Thus the Web APIs used IoC, enabled convention-over-configuration, and tried to offer simpler configuration environment.

The problem was that at that point in time there were several approaches for constructing HTTP services:

1. WCF with the WebHttp binding and REST support.

2. The new WCF Web APIs, soon to be ASP.NET Web APIs.

3. A not-so-new framework, ASP.NET MVC, which took a break from being HTML-oriented (getting requests from HTML pages and returning HTML/JSON) to being Resource-oriented – people started realizing that they can consider controllers as services and use the MVC infrastructure to define the control requests, responses, and better control the HTTP message.

4. Open source frameworks such as OpenRasta and ServiceStack.

In addition to that, as time passed, the WCF Web APIs had a lot of trouble adapting WCF to the “native” HTTP world. As WCF was primarily designed for SOAP-based XML messages, and the “open-heart” surgery that was required to make the Web API work as part of WCF was a bit too much (or so I understand from people who were involved in creating the Web APIs). On the other hand, the ASP.NET MVC infrastructure with its elegant handling of HTTP requests and responses, and its support of easy-to-create controllers seemed like the proper way to go for creating this new type of services.

So the fact was we had too many options and therefore too much confusion. What were we to do? We merge teams! (Kind of reminds us of the time of LINQ-to-SQL and Entity Framework, WCF and Ado.Net Data Services and other such examples). So the WCF team and the ASP.NET team joined forces and created a new framework focused on the world of REST/Hypermedia/HTTP services for the web world and thus came out the ASP.NET Web APIs.

I’m still not so sure about the choice of names, as the new Web APIs can also work outside of ASP.NET with the use of WCF, but I guess that the name “WCF ASP.NET Web API” was a bit long. Maybe “WASP Web API”? “WAWAPI” (Wcf Aspnet Web API)? Or maybe simply call it “Hypermedia Web API”?

So this merger is intended to reduce confusion, not induce it. I guess that if it was explained at that time, it might have caused less confusion over time (see the Silverlight is dead slip of PDC 2010). Does Microsoft need a new DevDiv PR team?

Is there still use for WCF? when should I choose Web APIs over WCF?

Recall my points from before - HTTP is a lot more than a transport protocol; use SOAP across the board and consider HTTP as no more than another way to pass messages.

  • If your intention is to create services that support special scenarios – one way messaging, message queues, duplex communication etc, then you’re better of picking WCF
  • If you want to create services that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transports are unavailable, then you’re better off with WCF and using both SOAP-based bindings and the WebHttp binding.
  • If you want to create resource-oriented services over HTTP that can use the full features of HTTP – define cache control for browsers, versioning and concurrency using ETags, pass various content types such as images, documents, HTML pages etc., use URI templates to include Task URIs in your responses, then the new Web APIs are the best choice for you.
  • If you want to create a multi-target service that can be used as both resource-oriented service over HTTP and as RPC-style SOAP service over TCP – talk to me first, so I’ll give you some pointers.

I hope this helped you removing some of the confusion over this topic.

What’s new in WCF 4.5? WebSocket support (Part 1 of 2)

This is the 11th post in the WCF 4.5 series. The previous post was about the new UDP transport support, and this new post is also about new transports – the WebSocket transport.

This post is part 1 of 2. This post will be about the WebSocket support between .NET apps using WCF (SOAP-based), and the next post will be about using WebSockets between browsers and WCF (non-SOAP).

Previous posts:

1. What’s new in WCF 4.5? let’s start with WCF configuration

2. What’s new in WCF 4.5? a single WSDL file

3. What’s new in WCF 4.5? Configuration tooltips and intellisense in config files

4. What’s new in WCF 4.5? Configuration validations

5. What’s new in WCF 4.5? Multiple authentication support on a single endpoint in IIS

6. What’s new in WCF 4.5? Automatic HTTPS endpoint for IIS

7. What’s new in WCF 4.5? BasicHttpsBinding

8. What’s new in WCF 4.5? Changed default for ASP.NET compatibility mode

9. What’s new in WCF 4.5? Improved streaming in IIS hosting

10. What’s new in WCF 4.5? UDP transport support

A (very) short introduction to WebSockets – WebSocket is a bi-directional (two-way), full-duplex channel. WebSocket channels start as normal HTTP channels, and then use handshakes to upgrade the channel to WebSocket, allowing two-way TCP communication between client and server, thus overcoming several limitations enforced by firewalls. In order not to repeat all that is already written about WebSockets, I suggest you check the following websites:

http://en.wikipedia.org/wiki/WebSocket

http://websocket.org/

https://datatracker.ietf.org/doc/rfc6455/

Note: This post was written according to the WebSocket support of WCF 4.5 with .NET 4.5 Beta and Visual Studio 11 Beta. If you are still using the Developer Preview version, you might see some different configuration sections and different behavior of the “Add Service Reference” feature.

The support of WebSocket in WCF 4.5 is achieved through the new NetHttpBinding. The NetHttpBinding was first introduced in the WCF 4 samples as a custom binding that uses binary-encoded SOAP messages over HTTP(S). The NetHttpBinding in WCF 4.5 is an improved binding that uses binary-encoded SOAP messages over HTTP(S) or WebSocket transports. The NetHttpBinding can be used in any of the following ways:

  1. Request-Response over HTTP. This mode does not use WebSockets, but rather a simple HTTP/HTTPS channel with binary-encoded SOAP messages.
    This mode is the default mode when using the binding without a duplex contract.
  2. Duplex over WebSocket. This mode uses WebSockets, and allows two-way communication between client and service.
    This mode is automatically used when you declare your service contract with a callback contract (duplex).
  3. Request-Response over WebSocket. This mode uses WebSockets, but it does not take advantage of the two-way communication support of the channel (since we still use the request-response pattern).
    This mode is used when doing one of the following:
    1. Changing the SessionMode of the contract to Required. The binding will upgrade automatically from HTTP to WebSocket, since HTTP is not sessionful and WebSocket is.
    2. Manually forcing WebSocket by changing the binding configuration and setting the WebSocket transport usage to Always.

Since WebSocket is sessionful, you automatically get session support in your service, if you haven’t changed the instance context mode from the default PerSession setting. If you’ve ever needed a sessionful HTTP channel and had to use WsHttpBinding with WS-ReliableMessaging, you now have another option which doesn’t require passing extra WS-RM messages.

NetHttpBinding with WebSocket can in fact replace the use of WsDualHttpBinding, since WebSocket provides a duplex channel which also supports sessions – this is better than WsDualHttpBinding which uses two channels, and require the use of WS-ReliableMessaging for session management.

All three modes of the binding are non-interoperable, because they use binary-encoded SOAP messages which is a proprietary Microsoft encoding technique. If you want to learn more about binary encoding, I suggest you read Nicholas Allen’s posts on the subject. However, this does not mean we cannot use WebSockets as an interoperable transport with text-based SOAP messages – we can change the binding configuration to use text instead of binary, thus making in interoperable, as shown later on.

To demonstrate the usage of NetHttpBinding, I’ve created a simple duplex contract:

[ServiceContract(CallbackContract=typeof(IDuplexCallbackContract))]
public interface IDuplexContract
{
    [OperationContract]
    string SayHelloDuplex(string name);
}

[ServiceContract]
public interface IDuplexCallbackContract
{
    [OperationContract]
    void SayingHello(string message);
}
And a service that implements the contract:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class WebSocketSampleService : IRegularContract, IDuplexContract
{
    public string SayHelloDuplex(string name)
    {
        OperationContext.Current.
            GetCallbackChannel<IDuplexCallbackContract>().
            SayingHello("Hello " + name + " by WebSockets");

        return "Hello " + name;
    }
}

The endpoint configuration is quite simple:

<endpoint address="http://localhost:8083"
          binding="netHttpBinding"
          contract="Contracts.IDuplexContract"/>

Note: It appear that the WCF Service Configuration Editor doesn’t recognize the NetHttpBinding’s WebSocket configuration, so you’ll need to use Visual Studio’s XML editor (the binding configuration is supported from VS11 Beta).

And as for the client-side code, just add a service reference and call the service:

Services.IDuplexContract duplexProxy;
Console.WriteLine("Press enter when service is ready");
Console.ReadLine();

// Use the generated proxy class
duplexProxy = new Services.DuplexContractClient(
    callbackContext,
    "DuplexContract");
Console.WriteLine("Calling the duplex contract:");
Console.WriteLine(duplexProxy.SayHelloDuplex("ido"));

// Or use a DuplexChannelFactory
DuplexChannelFactory<Services.IDuplexContract> dchf =
    new DuplexChannelFactory<Services.IDuplexContract>(
        callbackContext,
        new NetHttpBinding(),
new EndpointAddress("http://localhost:8083/")); duplexProxy = dchf.CreateChannel(); Console.WriteLine("Calling the duplex contract using text encoded messages:"); Console.WriteLine(duplexProxy.SayHelloDuplex("ido"));

Note: When specifying the endpoint address in the service, you need to use the http:// scheme, but on the client-side you can use either http:// or ws://. When generating a service reference, the client configuration will use the ws:// scheme.

Note: If your service endpoint uses NetHttpBinding but you contract is a non-duplex service contract (without a callback contract), you can still use NetHttpBinding, but the channel will be HTTP and not WebSockets – in this case the generated client configuration will create a custom binding instead of NetHttpBinding. The custom binding is a perfect match of the NetHttpBinding so things will still work, but it may be a bit confusing the first time you see it. I hope this will be resolved in the RTM version of VS 11.

When you add a service reference to the service, the client side configuration will be generated with a custom binding instead of the NetHttpBinding - you can either leave it as-is, or remove the custom binding and rewrite the configuration to use the NetHttpBinding (of course you might need to do it again when you update the service reference).

It is also quite easy to define a new service endpoint which uses the NetHttpBinding with text-based SOAP messages instead of binary:

<service name="Host.WebSocketSampleService">
    <endpoint address="http://localhost:8084"
              binding="netHttpBinding"
              bindingConfiguration="TextOverWebSockets"
              contract="Contracts.IDuplexContract"/>
</service>
<bindings>
  <netHttpBinding>
    <binding name="TextOverWebSockets" messageEncoding="Text"/>
  </netHttpBinding>
</bindings>

So as you can see, we have the ability to use both binary and text, but how can we be sure it actually passes text instead of binary? for that we need a network sniffer that can show us the WebSocket messages. You can use Wireshark or any other TCP sniffer to check that, however I always found those sniffers to be a bit hard to manage, especially for localhost communication. Luckily for us, we can use Fiddler, the famous HTTP sniffer, which now supports WebSocket messages (although only for watching).

This is how the binary message looks like when sent over WebSockets:

image

And this is how the text message looks like:

image

And this is the HTTP connection upgrade request:

image

(notice the GET request for WebSocket upgrade, and the corresponding HTTP 101 – Switching Protocols response)

If you want to use WebSockets for a simple Request-Response contract, you can also do that, but you’ll need to set the binding configuration of your endpoint to require WebSocket communication:

<bindings>
    <netHttpBinding>
        <binding name="ReqResWithWebSockets">
             <webSocketSettings transportUsage="Always"/>
        </binding>
    </netHttpBinding>      
</bindings>

You can download the sample code from my SkyDrive which demonstrates all of the above including duplex, request-response, sessions, forcing WebSockets, and text encoded messages.

When considering the use of WebSocket via NetHttpBinding for duplex communication vs. WsDualHttpBinding and NetTcpBinding, the benefits of NetHttpBindings are:

  1. Like NetTcp it requires one channel instead of two, as in the case of WsDualHttp.
  2. Like NetTcp it has a sessionful channel, unlike WsDualHttp which requires the use of WS-ReliableMessaging.
  3. Like NetTcp, it uses binary encoding which reduces the message size, unlike the text encoding of WsDualHttp.
  4. Unlike NetTcp, it can overcome some firewall restrictions that prohibit TCP communication.

As we’ve seen so far, the WebSocket support in WCF 4.5 uses SOAP-based messages. In the next post I will cover how to use WebSockets in WCF 4.5 to communicate with non-SOAP clients, such as web browsers, using simple text over WebSockets.

What’s new in WCF 4.5? UDP transport support

This is the tenth post in the WCF 4.5 series. I’ve started this series of posts 4 months ago when .NET 4.5 developer preview was announced; The Beta/RC/RTM version is still to come, but hopefully it will be available soon, and you will be able to use the new WCF 4.5 features in your projects.

Until now, I’ve shown new features in configuration easiness and hosting improvements. In this post and the next one I will cover new transport features, starting with the support for the UDP transport.

Previous posts:

1. What’s new in WCF 4.5? let’s start with WCF configuration

2. What’s new in WCF 4.5? a single WSDL file

3. What’s new in WCF 4.5? Configuration tooltips and intellisense in config files

4. What’s new in WCF 4.5? Configuration validations

5. What’s new in WCF 4.5? Multiple authentication support on a single endpoint in IIS

6. What’s new in WCF 4.5? Automatic HTTPS endpoint for IIS

7. What’s new in WCF 4.5? BasicHttpsBinding

8. What’s new in WCF 4.5? Changed default for ASP.NET compatibility mode

9. What’s new in WCF 4.5? Improved streaming in IIS hosting

I’ve been teaching WCF for several years now, and almost every time I explain to people about the different types of bindings and supported transports someone asks me if there is a built-in support for a UDP transport. Until now my answer was “It isn’t supported out-of-the-box, but there is a UDP transport sample in the WCF/WF samples”. From now on my new answer is “In WCF 4/3.5 there is a sample implementation, but it is now out-of-the-box in WCF 4.5”.

You can get some basic info about the binding in the System-Provided Bindings page on MSDN (make sure you look at the 4.5 version), unfortunately there is no documentation on the UdpBinding type yet, but hopefully MS will create it by the time WCF 4.5 is released.

Declaring an endpoint that uses UDP is quite simple:

<endpoint address="soap.udp://localhost:8080/" binding="udpBinding" contract="UdpHost.IService" />

Some facts about the new UDP binding:

  1. The address scheme for this binding is soap.udp://
  2. The binding is supported only by .NET (since it does uses text encoding for the SOAP messages, it can be considered interoperable, although currently no other SOAP-based services technology support UDP)
  3. Security is not supported (neither transport or message)
  4. Sessions, transactions, streaming, and duplex are not supported (I was hoping for a duplex UDP using one-way messages)
  5. Supported encoding is text
  6. The binding can be used in code by adding a reference to the System.ServiceModel.Channels assembly
  7. The binding is not supported in IIS/WAS, since there is still no UDP shared listener for WAS
  8. In the case of One-Way messages, this is a true one-way unidirectional call - the client won’t throw an exception if the service is unavailable
  9. If you specify a multicast address in the endpoint, such as 224.0.0.1 or 239.255.255.255 (the later is used by UDP discovery endpoints), you can create multiple listeners on the same address+protocol even from different machines – one client can send a single message that will be received by multiple listeners – this can be a great way to synchronize server state, do pub-sub (notification) calls etc...

Using a multicast listening address is also quite simple:

<endpoint address="soap.udp://239.255.255.255:8083/" binding="udpBinding" contract="UdpHost.IService" />

As for performance, I’ve create a simple client which sends 5000 messages using request-response and one-way (simple and multicast), with HTTP (basicHttp), TCP, and UDP bindings. For the purpose of the test I’ve removed all security settings from the NetTcp binding. The result is shown below:

Calling 5000 iterations one way using UDP
One-way using UDP took 0 seconds, average is 0.1792ms

Calling 5000 iterations one way using HTTP
One-way using HTTP took 6 seconds, average is 1.3ms

Calling 5000 iterations one way using TCP
One-way using TCP took 2 seconds, average is 0.5876ms

Calling 5000 iterations one way using UDP Multicast
One-way using UDP Multicast took 3 seconds, average is 0.736ms

Calling 5000 iterations of request-response using UDP
Request-response using UDP took 7 seconds, average is 1.4738ms

Calling 5000 iterations of request-response using HTTP
Request-response using HTTP took 8 seconds, average is 1.7784ms

Calling 5000 iterations of request-response using TCP
Request-response using TCP took 4 seconds, average is 0.9518ms

Conclusions:

  1. One-way messages are fast when using UDP – about 7 times faster than HTTP, and 3 times faster than TCP. I assume this is because when using UDP we don’t need to wait for the TCP ACK (also used in HTTP).
  2. One-way multicast messages in UDP are slower than direct messages – about 4 times slower
  3. Request-response message in UDP are slower than TCP, but faster than HTTP. I assume the reason for being slower than TCP is that UDP uses two channels to create request-response whereas TCP requires only one channel.

Trying to run the same sample when the server has not been started produces the following results:

Calling 5000 iterations one way using UDP
One-way using UDP took 0 seconds, average is 0.1216ms

Calling 5000 iterations one way using HTTP
One-way using HTTP failed

Calling 5000 iterations one way using TCP
One-way using TCP failed

Calling 5000 iterations one way using UDP Multicast
One-way using UDP Multicast took 1 seconds, average is 0.376ms

Calling 5000 iterations of request-response using UDP
Request-response using UDP failed

Calling 5000 iterations of request-response using HTTP
Request-response using HTTP failed

Calling 5000 iterations of request-response using TCP
Request-response using TCP failed

As you can see, UDP one-way messages are unidirectional!!!

The sample code is available on my SkyDrive.

Calling a WCF service from a client without having the contract interface

I was asked yesterday in the Hebrew C#/.NET Framework MSDN forums a tough question – is it possible to dynamically call a WCF service using only the contract name, operation name, and metadata address?

At first I agreed with the answer given in the forum – move from SOAP bindings to WebHttpBinding (“REST”). This of course makes things a lot easier, only requiring you to create a WebHttpRequest and parse the response. However the question remains - is it possible to do this in the case of a SOAP-based service endpoint?

The short answer is – YES!

The full answer is – YES, but you’ll need to do a lot of coding to make it work properly, and even more coding for complex scenarios (who said passing a data contract?)

How is it done you ask?

First let’s start with the contract – you have a simple contract that looks like so:

   1:  [ServiceContract]
   2:  public interface ICalculator
   3:  {
   4:    [OperationContract]
   5:    double Add(double n1, double n2);
   6:    [OperationContract]
   7:    double Subtract(double n1, double n2);
   8:    [OperationContract]
   9:    double Multiply(double n1, double n2);
  10:    [OperationContract]
  11:    double Divide(double n1, double n2);
  12:  }

At this point the implementation doesn’t matter, but you can assume the service compiles and loads successfully.

Second, make sure your service has either a MEX endpoint or metadata exposed over HTTP GET. Read here for more info about the difference between the two.

Third – do the client coding!!! to create the client code I took some ideas from the following links:

http://msdn.microsoft.com/en-us/library/ms733780.aspx – generating client-side type information for WCF contracts

http://www.codeproject.com/Articles/42278/Call-a-Web-Service-Without-Adding-a-Web-Reference – the same concept of dynamic calls, but for ASP.NET web services (ASMX).

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.ServiceModel;
   5:  using System.ServiceModel.Description;
   6:  using System.Globalization;
   7:  using System.Collections.ObjectModel;
   8:  using System.CodeDom.Compiler;
   9:   
  10:  namespace Client
  11:  {
  12:      class Program
  13:      {
  14:          static void Main(string[] args)
  15:          {
  16:              // Define the metadata address, contract name, operation name, and parameters. 
  17:              // You can choose between MEX endpoint and HTTP GET by changing the address and enum value.
  18:              Uri mexAddress = new Uri("http://localhost:8732/CalculatorService/?wsdl");
  19:              // For MEX endpoints use a MEX address and a mexMode of .MetadataExchange
  20:              MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;            
  21:              string contractName = "ICalculator";
  22:              string operationName = "Add";
  23:              object[] operationParameters = new object[] { 1, 2 };
  24:   
  25:              // Get the metadata file from the service.
  26:              MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress, mexMode);
  27:              mexClient.ResolveMetadataReferences = true;
  28:              MetadataSet metaSet = mexClient.GetMetadata();
  29:   
  30:              // Import all contracts and endpoints
  31:              WsdlImporter importer = new WsdlImporter(metaSet);
  32:              Collection<ContractDescription> contracts = importer.ImportAllContracts();
  33:              ServiceEndpointCollection allEndpoints = importer.ImportAllEndpoints();
  34:   
  35:              // Generate type information for each contract
  36:              ServiceContractGenerator generator = new ServiceContractGenerator();
  37:              var endpointsForContracts = new Dictionary<string, IEnumerable<ServiceEndpoint>>();
  38:   
  39:              foreach (ContractDescription contract in contracts)
  40:              {
  41:                  generator.GenerateServiceContractType(contract);
  42:                  // Keep a list of each contract's endpoints
  43:                  endpointsForContracts[contract.Name] = allEndpoints.Where(
  44:                      se => se.Contract.Name == contract.Name).ToList();
  45:              }
  46:   
  47:              if (generator.Errors.Count != 0)
  48:                  throw new Exception("There were errors during code compilation.");
  49:   
  50:              // Generate a code file for the contracts 
  51:              CodeGeneratorOptions options = new CodeGeneratorOptions();
  52:              options.BracingStyle = "C";
  53:              CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");
  54:   
  55:              // Compile the code file to an in-memory assembly
  56:              // Don't forget to add all WCF-related assemblies as references
  57:              CompilerParameters compilerParameters = new CompilerParameters(
  58:                  new string[] { 
  59:                      "System.dll", "System.ServiceModel.dll", 
  60:                      "System.Runtime.Serialization.dll" });
  61:              compilerParameters.GenerateInMemory = true;
  62:   
  63:              CompilerResults results = codeDomProvider.CompileAssemblyFromDom(
  64:                  compilerParameters, generator.TargetCompileUnit);
  65:   
  66:              if (results.Errors.Count > 0)
  67:              {
  68:                  throw new Exception("There were errors during generated code compilation");
  69:              }
  70:              else
  71:              {
  72:                  // Find the proxy type that was generated for the specified contract
  73:                  // (identified by a class that implements the contract and ICommunicationbject)
  74:                  Type clientProxyType = results.CompiledAssembly.GetTypes().First(
  75:                      t => t.IsClass &&
  76:                          t.GetInterface(contractName) != null &&
  77:                          t.GetInterface(typeof(ICommunicationObject).Name) != null);
  78:                          
  79:                  // Get the first service endpoint for the contract
  80:                  ServiceEndpoint se = endpointsForContracts[contractName].First();                    
  81:   
  82:                  // Create an instance of the proxy
  83:                  // Pass the endpoint's binding and address as parameters
  84:                  // to the ctor
  85:                  object instance = results.CompiledAssembly.CreateInstance(
  86:                      clientProxyType.Name, 
  87:                      false, 
  88:                      System.Reflection.BindingFlags.CreateInstance, 
  89:                      null,
  90:                      new object[] { se.Binding, se.Address }, 
  91:                      CultureInfo.CurrentCulture, null);
  92:                  
  93:                  // Get the operation's method, invoke it, and get the return value
  94:                  object retVal = instance.GetType().GetMethod(operationName).
  95:                      Invoke(instance, operationParameters);
  96:   
  97:                  Console.WriteLine(retVal.ToString());
  98:              }
  99:          }
 100:      }
 101:  }

I’ve placed comments that describe the code, but basically it imports the WSDL, generates types for the contract (service + data), generates C# code from it, compiles it, and uses reflection to create a proxy and invoke the correct method.

If you want to use this technique to call methods that require a data contract, you will need some extra work to create the correct type and initialize it.

A compiled and running version of this code (+ the service) can be found here: https://skydrive.live.com/redir.aspx?cid=5ef5be1ab30a6056&resid=5EF5BE1AB30A6056!466&parid=5EF5BE1AB30A6056!129

Hope you find this piece of code useful.

Feb-12:

Just found out this great blog post that uses the same implementation only to enable calling WCF services from PowerShell. 
http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/

WCF/ASMX Interoperability – Removing the Annoying xxxSpecified when Adding a Web Reference to a WCF Service

Today I answered a question in the Hebrew MSDN forums about consuming WCF from a .NET 2 client, using the “Add Web Reference” option of Visual Studio.

Just in case you don’t know Hebrew I’ll sum it up for you – when adding a web reference to a WCF service that exposes a method of the following sort:

int UseScalarTypes(int value1, int value2)

The generated method signature in the client app will look like so:

public void UseScalarTypes(
  int value1, bool value1Specified, 
  int value2, bool value2Specified, 
  out int UseScalarTypesResult, out bool UseScalarTypesResultSpecified)

The question was why this happens and how this can be fixed to look like the service’s method signature.

Before we continue to see why this happens and how to fix it, the short answer is – Yes, you can make it look like the original contract by using message contracts. Continue reading to see how.

Why this happens:

In WCF the WSDL generated for the above method looks like so:

<xs:element name="UseScalarTypes">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="0" name="value1" type="xs:int" /> 
      <xs:element minOccurs="0" name="value2" type="xs:int" /> 
    </xs:sequence>
  </xs:complexType>
</xs:element>

Notice the minOccurs? int is a value type which means it doesn’t accept null values, but still WCF marks it as optional. When you use the “Add Service Reference” option of WCF in Visual Studio, the generator ignores that attribute and creates the method declaration in the client side the same way it is declared in the service contract. However, if you use the old “Add Web Reference” option, the generator checks the minOccurs, realizes that the variable is optional, and since this is a value type, it translates the optional into a set of variables: value + xxxSpecified.

By the way, this is how the WSDL is created for the same method declaration when it is used in an ASMX-style web service:

<s:element name="UseScalarTypes">
  <s:complexType>
    <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="value1" type="s:int" /> 
      <s:element minOccurs="1" maxOccurs="1" name="value2" type="s:int" /> 
  </s:sequence>
  </s:complexType>
</s:element>

Note 1: With ASMX web service, the minOccurs/maxOccurs is set properly because the “Add Web Reference” expects that, and therefore we don’t see this behavior with ASMX web service + add web reference.

Note 2: Since WCF ignores the minOccurs, using the “Add Service Reference” to consume that ASMX web service will result in a client-side method with the same signature as declared in the service (without the xxxSpecified).

So now that we know why this happens, let’s see how to fix it.

Step 1: Create a set of message contracts, one for the request and one for the response (if you have one).

Step 2: In the request message contract class, apply the MessageContract attribute and set the IsWrapped parameter to false, like so:

[MessageContract(IsWrapped = false)]
public class UseScalarTypesRequest

Setting the IsWrapped to false will create the XML without a wrapping element, making the properties look like they are actually method parameters.

Step 3: Add each parameter of the method to the class as a property, and apply the MessageBodyMember attribute to it. You can use this step to rename the property to use the naming convention of parameters by using the Name parameter in the attribute. The result should look like so:

[MessageContract(IsWrapped = false)]
public class UseScalarTypesRequest
{
  [MessageBodyMember(Name = "value1")]
  public int Value1 { get; set; }
  [MessageBodyMember(Name = "value2")]
  public int Value2 { get; set; }
}

Step 4: Do the same for the response message contract, this time you just need one property for the return type of the method, for example:

[MessageContract(IsWrapped = false)]
public class UseScalarTypesResponse
{
  [MessageBodyMember]
  public int Result { get; set; }
}

Step 5: Change the method signature in the contract and service from using parameters to using the message contracts you’ve created, like so:

UseScalarTypesResponse UseScalarTypes(UseScalarTypesRequest parameters)

Of course the immediate step will be to also change the implementation of your code to reflect the parameters being moved to a wrapper object - for convenience, you can leave your existing code in the service as is, change the contract, and then create the new methods which will simply call the older ones, like so:

UseScalarTypesResponse UseScalarTypes(UseScalarTypesRequest parameters)
{
  UseScalarTypesResponse result = new UseScalarTypesResponse();
  result.Result = UseScalarTypes(parameters.Value1, parameters.Value2);
  return result;
}

That’s it, update your web reference in the client side, and watch how the method signature in the client side is without the xxxSpecified.

More Posts Next page »