WCF Message can only be read once

April 23, 2010 11:01 by bryan

WCF is a very powerful tool for developers, and I'm sure as you dive in to the development environment you will come across the WCF Message, which is part of System.ServiceModel.Channels Namespace. You should be aware that a message body can only be accessed once and a message can only be written once.  This will in turn can cause a problem if you are creating your own IDispatchMessageInspector, as at some you will need to read the message and once you have read it, you are buggered!

So in order to use the Message you will have to copy the message in to a buffer using the CreateBufferCopy method, here is how you go about it

public void Validate(ref Message message)
{
    MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);
   
try
    {
    ValidateInput(buffer.CreateMessage());
    }
   
catch (Exception)
    {
   
throw;
    }
   
message = buffer.CreateMessage();
}


XSD to Objects

April 16, 2010 14:54 by bryan

I've been dealing with REST services within WCF, and one thing that has come to my attention is that the calling service or third party only has access to the REST interface and the XSD's.

The XSD's holds all the contract information, therefore if you need to generate a concrete class you are going to have to translate the XSD to an object. 

I've have come across the XSD Object Generator frm Microsoft which appears to do the job.


Passing Validation exceptions via WCF REST

April 6, 2010 09:43 by bryan

When using WCF and Rest one thing you're going to have to consider is the validation of the object, the way to do this is using the Validation Application Block included in the Microsoft Enterprise Library to validate the data transfer objects. This way you can decorate the objects with attributes to validated the objects

Here is an example

public static void Validate<T>(T obj)
{
     ValidationResults results = Validation.Validate(obj);

     if (!results.IsValid)
         
string[] errors = results
          .Select(r => r.Message)
         
.ToArray();

     WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
     WebOperationContext.Current.OutgoingResponse.StatusDescription = String.Concat(errors);
}

thanks to Enrico Campidoglio for helping out on this


Using REST via WCF

March 23, 2010 11:26 by bryan

With WCF now being around for some time, it supports many protocols, mainly SOAP, but it does also support Rest, the question is how do you do it?  Well I have got the sample working from the MSDN Magazine.  If you'd like the full detailed explaintation then you can go to An Introduction To RESTful Services With WCF 

MSDNMagazine.zip (64.82 kb)

 


Creating a Simple WCF application

March 11, 2009 16:04 by bryan

After looking around and playing with WCF for a while, it had come to my notice that all the examples that I could find were over complex, then I came across Ralf's Sudelbücher blog entry for "A truely simple example to get started with WCF", so simple, so to help you along here is the C# source code that I have written, with a few additions in that the solution also has a client console that calls the WCF application to show a true sence of what is happening.

SimpleWCF.zip (21.68 kb)


WCF getting started

January 24, 2009 22:39 by bryan

I'm not currently working on WCF, but I when I find some useful links I'll add them here

Set of links for WCF

Getting Started with Windows Communication Foundation (WCF)

If you're going to do any extensive WCF services it is worth getting hold of Programming WCF Services by Juval Lowy