Geeks With Blogs

News Add to Technorati Favorites
Steve Loethen When I grow up I want to code in c#

First, a choice of tools..

I am going to use Visual Studio 2008.  I know, it is in Beta, Beta 1 in fact.  But Beta 2 should be out soon, and it should release q4 of 2007, q1 of 2008.  There might be a tighter date published, but I haven't looked today.

It also has a better support for WCF and the .Net 3.5 stuff in general.  So, that is where we are going to live.

Contract definition

As far as I can tell, WCF has four kinds of contracts.   (btw, thanks Juval Lowy's book for the list that is coming)

Service Contracts:  Support the operations you want your service to perform. 

Data Contracts: This allows you to go beyond built-in types for data transfer.  WCF already supports all the built in ones.  You can support complex data types, but they have to be supportable on both ends.

Fault Contracts:  Defines which errors are raised and how they are handled. 

Message Contracts:  Let's the service dive into a message, very useful when someone or something is defining a message structure, and you have to deal with it.

We are going to define a simple service contract first.  I have seen several different ways to do it.  But most sources that I have seen, must demo's I have witnessed, seem to favor the 1) define an interface, 2) decorate the interface with attributes of the service, and 3) implement the interface in a class.  You could just define the class and decorate it, but it should be discouraged.  a seperate interface definition allows you to use the interface in other contexts. 

My interface and implementation without attributes looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCFContract
{
//this is the interface that we are going to expand and explore as we develop our WCF knowledge
   interface myContractInterface
   {
   string SayHello(string name);

   string AllCaps(string str);

   string AllLower(string str);
   }

//this is the implementation of the interface we are going to explore.

   public class myContractImplementation : myContractInterface
   {
      public string SayHello(string name)
      {
         return "hello " + name;
      }

      public string AllCaps(string str)
      {
         str.ToUpper();

         return str;

      }

      public string AllLower(string str)
      {
         str.ToLower();

         return str;
      }
   }

}

(my apologized for the inaccurate coloring.  I did my best, but live writer didn't copy and paste color correct.  If someone knows how to do this, including preserving indentation, it would be nice to know).

Now that we have decided on our contract, we need to decorate it with attributes to make it a WCF service.  First, add a reference to your project for System.ServiceModel.  Then add in a using directive to let the file know it is there.

After the project and the file knows about service support, we need to decorate our code.

 

using System;
using System.Collections.Generic;

using System.ServiceModel;
using System.Linq;
using System.Text;

namespace WCFContract
{
//this is the interface that we are going to expand and explore as we develop our WCF knowledge

   [Service Contract]
   public interface myContractInterface
   {

   [Operation Contract]
   string SayHello(string name);

   [Operation Contract]

   string AllCaps(string str);

   [Operation Contract]

   string AllLower(string str);
   }

//this is the implementation of the interface we are going to explore.

   public class myContractImplementation : myContractInterface
   {
      public string SayHello(string name)
      {
         return "hello " + name;
      }

      public string AllCaps(string str)
      {
         str.ToUpper();

         return str;

      }

      public string AllLower(string str)
      {
         str.ToLower();

         return str;
      }
   }

}

Nothing changed in the implementation.  We just decorated the interface with [ServiceContract] and the methods with [OperationContract] .  Now we are ready to host the service.  We have many choices.  That will be the next post.  Perhaps later tonight.

Posted on Monday, June 18, 2007 1:41 PM | Back to top


Comments on this post: I am taking a contract out on WCF...

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Steve Loethen | Powered by: GeeksWithBlogs.net