Jeff W. Barnes

Ramblings on C#, WCF, and random .NET

September 2006 - Posts

Registration Opened for Alabama Code Camp #3

I just received word from Joe Healy that registration has been opened for the Alabama Code Camp #3.  You can register here.  We have a healthy number of speakers, but we could still use more.  If you know of someone that would be interested in delivering a presentation, direct them to the website at http://www.alabamacodecamp.com.

Please help spread the word about this event.  Remember that it is completely free and there are always a variety of nice give-aways at the end of the day.  Be sure to come check out Todd Miranda's series of WPF presentations, Eric Ray's presentation on Windows Cardspace, and my presentation on WCF.  Todd and I are also working on a very sweet joint demo that illustrates some of the capabilities of WPF and WCF.

It should be a fun day.  I hope to see you there.

Overloading Methods in WCF

Yesterday, I attended a local MSDN event in the Birmingham area.  It covered the .NET 2.0 System.Net namespace, an introduction to WCF, and a look at Atlas.  During the course of the WCF overview, someone asked the presenter whether it was possible to overload a method in WCF.  The presenter wasn't completely sure and there were mixed answers from people in the audience.  So, I thought I would provide the correct answer.

The short answer is yes.  Technically, it is possible to overload a method in WCF.  However, when you want to expose overloaded methods as operations in a WCF contract, there is more to it than simply writing two or more methods with the same name.

Consider the following example:

[ServiceContract]
public interface ICalendarService
{
   [OperationContract]
   ScheduledEvent[] GetScheduledEvents(DateTime date);

   [OperationContract]
   ScheduledEvent[] GetScheduledEvents(DateTime start, DateTime end);
}

Now, try to actually implement this interface and host it as a WCF service.  Kaboom!  An InvalidOperationException will be raised when you invoke ServiceHost.Open.  Jeff, I thought you said that methods could be overloaded in WCF.  What gives?

As far as the C# compiler is concerned, this is a completely legal piece of code.  The interface contains two methods.  The methods have the same name, but their signatures are unique since their parameters differ.  The interface and methods are decorated with attributes.  However, WCF doesn't have any special hooks into the compiler.  Consequently, the compiler doesn't have any special knowledge about the attributes.  It is simply going to emit their information in the assembly metadata.

When WCF attempts to start the service, it essentially interrogates the metadata to generate a WSDL contract.  WSDL is all about technology neutral, message based communication.  It doesn't support object-oriented concepts such as inheritance and overloading.  (Actually, I think the earliest versions of WSDL allowed overloading to a certain extent, but it has since been removed in the newer versions.)  So, WCF basically detects there are two methods with the same name and raises an exception to indicate this isn't allowed even though the code compiled without any errors. 

Regardless, WCF still provides the ability to overload methods.  This can be achieved by using the name property of the OperationContract attribute.  By default, the name of the method will be used for the value of the name property.  However, you can explicitly set the value of the name property to provide a unique value.  Under the covers, WCF will use the value you supply as the name of the corresponding operation in the generated WSDL. 

Here is our previous example revised to use alias method names:

[ServiceContract]
public interface ICalendarService
{
   [OperationContract(Name = "GetScheduledEventsByDate")]
   ScheduledEvent[] GetScheduledEvents(DateTime date);

   [OperationContract(Name = "GetScheduledEventsByDateRange")]
   ScheduledEvent[] GetScheduledEvents(DateTime start, DateTime end);
}

You may be wondering how the method will appear to the consumer of the service.  Will it appear as two different names (based on the the alias) or as overloaded methods with the same name?  This will depend upon the proxy class used by your client.  If you use the proxy class that is automatically generated by svcutility.exe, the alias method names will be used.  However, you can manually edit the generated proxy class to achieve the appearance of overloaded methods on the client as well.  This can be accomplished by applying the same attributes to the methods defined in the interface that is used by the proxy class.

As you can see, overloading of methods is possible in WCF.  However, there is another question that remains to be answered.  Just because you have the ability to overload methods exposed in the contract, should you do it?  There are a lot of varied opinions about this type of question.  It is hard to say whether any of them are definitively right or wrong.  Personally, I am in the camp that you should avoid it unless there is a compelling reason. 

Don't get me wrong.  There is absolutely nothing wrong with method overloading in the realm of object oriented programming.  However, object oriented development doesn't translate directly to service oriented development.  I don't think it is generally a good idea to expose overloaded methods as part of a service contract.  It is a specific object-oriented concept that doesn't really fit into the semantics of service orientation.

At any rate, to answer the question of the person at the MSDN event...yes, it is possible to overload methods exposed as operation contracts in WCF.

Posted: Sep 20 2006, 10:33 PM by jeff.barnes | with 1 comment(s)
Filed under:
Reminder: Local MSDN Event Tomorrow (9/19)

Just a reminder to those of you in the Birmingham area (or within driving distance).  There will be an MSDN event in Trussville at the Regal Cinemas tomorrow (September 19th).  It is scheduled from 1PM to 5PM.  The topics will be working with System.Net in .NET 2.0, Windows Communication Foundation, and Atlas.  If you haven't already registered, then check out the MSDN Events site to see if there is still any space available.

Here is a link to the event.

If you can attend, please do so.  The only way to ensure we continue to get these free MSDN events is to have solid attendance.  Hopefully, it will be filled to capacity.  Pass along the information to anyone that might be interested.

I will see you there!

New Local Blogger

One of my passions, other than software development, is the continued growth of the involvement among the local developer community.  I really believe the technical industry within the Birmingham area (and possibly the entire state) will reap the benefits as more and more local developers become interested in networking with other local developers.  In my opinion, blogging is one such form of this networking effort.

To that end, I wanted to pass along the word that another local blog has recently popped up on the radar.  Eric Ray, a friend and former co-worker, has recently setup a blog entitled Thinking Loudly.  He has been getting into Windows Cardspace quite a bit.  So, if that is something that interests you, check out his blog.  As he gets the site started, you might find it as a useful source of information.

Alabama Code Camp #3

I recently mentioned that the next Alabama Code Camp has been planned for next month.  The website has finally been updated and the call for speakers is now open.  If you are interested in delivering a presentation or know of someone that would be, download the speaker form from the website and follow the instructions.  Registration is not open yet.  Keep an eye on the website over the new few weeks.  Once it opens up, I will be sure to pass the word along.

I have submitted two presentations for the event: "Introduction to WCF" and "Extensibility in WCF".  The first presentation will basically be a crash course in the fundamental concepts that should give you enough information to get started playing around with the technology.  The second presentation will expand upon the introduction by taking a closer look at some of the advanced concepts of WCF.  It will specifically focus on the major extensibility points such as channels, behaviors, and security.

The event is still planned for Saturday, October 28th at the Auburn University Campus in Montgomery.  Put it on your calendar and make plans to attend.  Come on out to support the local developer community, encourage some new speakers, network with other developers, learn something new, and you might just enjoy yourself as well.  The event has always been great, but it has also consistently improved with each iteration.  Don't miss out on this opportunity.

I hope to see you there!

Upcoming Local Events

There are a few upcoming events in the Birmingham area that I wanted to pass along to those that might be interested.

Alabama Code Camp #3
I have received word that the next code camp has been tentatively planned for Saturday, October 28th.  This time the event will be held in Auburn University Campus in Montgomery, AL.  The details are still being discussed, but you should go ahead and mark it down on your calendar.  Stay tuned to the website at http://www.alabamacodecamp.com for additional information.  It hasn't been updated yet, but it should be in the near future.

Birmingham Tech Mixer
Another area tech mixer has been planned for Thursday, September 21.  If you aren't familiar with this or didn't make it to the first one, it is basically a large networking event for all of the various user groups within the area.  It is also an excellent opportunity to mingle with local developers.  It will be held at the McWane Science Center from 6PM to 8PM.  There is no charge for admission.  Free drinks and appetizers will be provided, but a cash bar will be available as well.  You can get more information and register here.

Birmingham .NET User Group September Meeting
The next meeting will be on Tuesday, September 12.  It will be held at the CTS office at 6:30 PM.  Ahmed Moiz, head of the Software Engineering and Quality Assurance departments for Marketron, will be presenting "Software Architecture and Design: A Programmer's Approach".  For more information, check out the user group website at http://www.bugdotnet.com.

 

I have been rather distracted over the last few weeks between final preparations for the imminent newborn as well as various issues for a client.  There are a couple of pet project ideas that I have been tossing around in my head.  Hopefully, I will be able to flesh those out a bit within the next week or two.  Once I have it more clearly defined, I will probably be blogging about them.  In case you are wondering...it will be tied into SOA and WCF.



Disclaimer:The opinions and views expressed within this blog are solely my own and do not represent those of my employer or anyone else.