Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




QuestPond: Interview Questions and Answers on learning WCF (Windows Communication Foundation)

What are the different contracts available in WCF?

There are five kinds of contracts: -
  1. Service contracts – Defines the set of operations exposed by the service to the outside world.
  2. Operation Contract – define the operation within the service contract. In simple technical word service contract is an interface which will be implemented by service and every method in interface is operation contract.

    [ServiceContarct]
    
    public interface IMyService { [OpearationContract] void method1(int x); [OperationContract] Intmethod2(int x, int y); } . . . public class MyService:IMyService { public void method1(int x){…} publicInt method2(intx,int y){…} }


  3. Data Contract –Data to be exchanged between client and service. It’s simply a class decorated with DataContract attribute and act as an agreement between client and service quoting “We will pass these things to each other”)
  4. Message Contract – In WCF Communication occurs via SOAP messages. Sometimes developer came across a situation where he want to control how SOAP messages will be structured. Like what should go into the header? What into the body etc. In this situation we use Message Contract.

  5. [MessageContract]
    public class MyClass
    {
      [MessageHeader] public intmyProperty1;
      [MessageHeader] public DateTimemyProperty2;
      [MessageBodyMember] private stringmyProperty3;
      [MessageBodyMember] private float myProperty1;
      [MessageBodyMember] public DateTime myProperty1;
    }


  6. Fault contract
  • In WCF whenever exception occurs a standard exception error message will returned to client. It look like this
Untitled
  • But sometimes developer want to send customized error then they can use Fault Exception

  • We can create custom types and pass error messages to clients using FaultContracts.
[DataContract()]
public class MyException
    {
        [DataMember()]
public string MyMessage;
    }
[ServiceContract()]
public interface IMyService
    {
        [OperationContract()]
        [FaultContract(typeof(MyException))]
intMyMethod(int num1, int num2);
    }
.
.
.

 

Client code
try
   {
MyServiceProxy proxy 
      = new MyServiceProxy();
Console.WriteLine("Sum of two numbers... 5+5 =" + proxy.MyMethod(5, 5));
Console.ReadLine();
    }
catch (FaultException ex)
     {
        //Process the Exception
     }

 

publicintMyMethod(int num1, int num2)
{
MyException ex = new MyException ();
ex.MyMessage= "My Error Message”
throw new FaultException(ex) ;
}

For technical trainings on various topics like WCF, MVC, Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com

For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla

See following video on WCF (Windows Communication Foundation) endpoints, address, contracts and bindings: -

 

Click and see here for more stuffs complete learning tutorials on WCF(Windows Communication Foundation).

Helpful Website Url
http://questpond.com/
Share this article   |    Print    |    Article read by 3243 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: No related interview question