Skip to main content

Understanding different types of WCF Contracts

WCF contract specify the service and its operations. WCF has five types of contracts: service contract, operation contract, data contract, message contract and fault contract.


  • Service Contract

    A service contract defines the operations which are exposed by the service to the outside world. A service contract is the interface of the WCF service and it tells the outside world what the service can do. It may have service-level settings, such as the name of the service and namespace for the service.
    1. [ServiceContract]
    2. interface IMyContract
    3. {
    4. [OperationContract]
    5. string MyMethod();
    6. }
    7.  
    8. class MyService : IMyContract
    9. {
    10. public string MyMethod()
    11. {
    12. return "Hello World";
    13. }
    14. }
  • Operation Contract

    An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the op-eration, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.
    1. [ServiceContract]
    2. interface IMyContract
    3. {
    4. [FaultContract(typeof(MyFaultContract))]
    5. [OperationContract]
    6. string MyMethod();
    7. }
  • Data Contract

    A data contract defines the data type of the information that will be exchange be-tween the client and the service. A data contract can be used by an operation contract as a parameter or return type, or it can be used by a message contract to define elements.
    1. [DataContract]
    2. class Person
    3. {
    4. [DataMember]
    5. public string ID;
    6. [DataMember]
    7. public string Name;
    8. }
    9. [ServiceContract]
    10. interface IMyContract
    11. {
    12. [OperationContract]
    13. Person GetPerson(int ID);
    14. }
  • Message Contract

    When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract. A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings, such as the level of message security.
    Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP body.
    1. [ServiceContract]
    2. public interface IRentalService
    3. {
    4. [OperationContract]
    5. double CalPrice(PriceCalculate request);
    6. }
    7.  
    8. [MessageContract]
    9. public class PriceCalculate
    10. {
    11. [MessageHeader]
    12. public MyHeader SoapHeader { get; set; }
    13. [MessageBodyMember]
    14. public PriceCal PriceCalculation { get; set; }
    15. }
    16.  
    17. [DataContract]
    18. public class MyHeader
    19. {
    20. [DataMember]
    21. public string UserID { get; set; }
    22. }
    23.  
    24. [DataContract]
    25. public class PriceCal
    26. {
    27. [DataMember]
    28. public DateTime PickupDateTime { get; set; }
    29. [DataMember]
    30. public DateTime ReturnDateTime { get; set; }
    31. [DataMember]
    32. public string PickupLocation { get; set; }
    33. [DataMember]
    34. public string ReturnLocation { get; set; }
    35. }
  • Fault Contract

    A fault contract defines errors raised by the service, and how the service handles and propagates errors to its clients. An operation contract can have zero or more fault contracts associated with it.
    1. [ServiceContract]
    2. interface IMyContract
    3. {
    4. [FaultContract(typeof(MyFaultContract1))]
    5. [FaultContract(typeof(MyFaultContract2))]
    6. [OperationContract]
    7. string MyMethod();
    8. [OperationContract]
    9. string MyShow();
    10. }

  • Comments

    Popular posts from this blog

    gcAllowVeryLargeObjects Element

    There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {          Re=re;          Im=im;      } } As we know this structure consumes about 16

    Support for debugging lambda expressions with Visual Studio 2015

    Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitations.

    How to allow a very large object in .net application?

    Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio, and create C# Console Application. Lets create simple struct with two double members like example below: public struct ComplexNumber { public double Re; public double Im; public ComplexNumber(double re, double im) { Re = re; Im = im; } } As we know this structure consumes about 16 bytes of memory. So if we want to create array of this type which consume more than 2GB we need to create array at least with 134217728 instances. So this sample program below creates 130000000 (about 1,97 GB) of array. int maxCount = 130000000; ComplexNumber[] arr = null; try { arr = new ComplexNumber[maxCount]; } catch (Exception ex) { Console.WriteLine(ex.Message); } So if we run t