Archive

Posts Tagged ‘WCF’

WCF DataContract versionning issue

March 20, 2009 Leave a comment

I get recently face to a problem during my development work of an application around WCF service. The problem I face was about different version in place of WCF contracts.

This happen during my development phase because I get different test environment but this is something you might be face in an improvement phase of an existing deployed services.

 

Scenario :

You have prepared a fully functional object that will be used in your DataContract.

Let call that object “Contact” which contain a single member “FirstName” and “LastName”

 

This object is then consumed by a client application which is then deployed at your customer site.

 

In a normal evolution of software we usually add functionalities. What happen then if from our object “Contact”

we create a second version of it with more members ?

 

The effect will be that your new member will be set to its default value of 0 when deserialized at the Client. This is absolutely not what we want.

 

Solution :

implement the IExtensibleDataObject interface, you must implement the ExtensionData property. The property holds data from future versions of the class for backward compatibility.

 

[DataContract(

    Name = “Contact”,

    Namespace = http://yourcomapny.com/Contacts”)]

class Contact :IExtensibleDataObject

{

 private ExtensionDataObject extensionDataObject_value;

    public ExtensionDataObject ExtensionData

    {

        get

        {

            return extensionDataObject_value;

        }

        set

        {

            extensionDataObject_value = value;

        }

    }

    [DataMember]

    public string FirsName;

 

    [DataMember]

    public string LastName;

               

} 

 

For more details, a nice example describes the situation at :

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.extensiondataobject.aspx

Categories: Uncategorized Tags:

System.ServiceModel.FaultException`1 was unhandled by user code

November 25, 2008 55 comments
Managing a correct error handling process in an application is a nightmare if you want to do it properly and I would say that it is not always the best exciting part, but it is absolutly needed to prevent as much as possible unhandle situation. In a normal application you simply place the code inside try-catch block with the exception type, and there are handle as normal .net exception object that you can bubble up.
With WCF service I have discover around different reading that it is a bit different, simply because WCF need to guaranty interoperability with any client application in order that they are able to catch error returned.
For that WCF need to convert the “normal .NEt exception” into a SOAP message exception that will be understandable from client application.
To achive this you need to specify a FaultContract attrribute in your service either declaratively or imperatively way. For my case I have done it with declarative.
You specify the declarative FaultContrat as follow for your service method:
  [OperationContract] [FaultContract (typeof(MyError))] Boolean myMethod();
MyError is here a custom type which define an error message
After having configured the service FaultContractAttribute, next you need to raise the exception from your server side service method code as follow :
Server side
catch (Exception exc) {   MyError ErrLog = new Maillefer.Nomos.Types.Common.MyError (“This is an error”,”Critical”);   FaultException<MyError> fe = new FaultException<MyError >(ErrLog, new FaultReason(ErrLog.Message));   throw fe; }

So far so good. Then from the client application side you simply need to catch the FaultException error type as you normally do and retrive the message return by the Servcie SOAP message as follow:

Client side

catch (FaultException MyError) {   string msg = Error.Detail.Message;    MessageBox.Show (msg);   wcfclient.Close(); }

And that’s it. You then receive the error message send from your server, inside your client application.. Hmmm this is what I was expecting but it was not behaving as expected. I spend days to cross check my code and verify impementation to get the exception correctly thrown but when runnig my application my service was stoping at the time it was throwing the exception (throw fe). The error return from that execution was something strange like :

System.ServiceModel.FaultException`1 was unhandled by user code

After a lot of research I find out the solution on a post mentionning that it was due to some setting of debugging scenario of my VS environement, which make my code execution stop at each exception with not really logic message wich was giving a lot of confusion.

To remove this behaviour it was advise to uncheck the 2 following options from the Tools->Option menu of IDE:

 0654_Capture

 Hoping this post will save you a lot of research time. Once again, every thing works until you hit the point that gives you nightmare 🙂

Categories: WCF Tags: