WCF contracts

WCF contracts define the behavior of WCF services. They are created in code by service developers, and are exposed to clients in the service metadata. The five types of contracts:
  1. Service Contracts
  2. Operation Contracts
  3. Data Contracts
  4. Message Contracts
  5. Fault Contracts
Slide 23
§
1.A service contract defines the operations that a service supports, and maps to a portType in Web Service Description Language (WSDL). Service contracts are implemented as .NET Framework interfaces that are annotated with the ServiceContract attribute. [ServiceContract] public interface IMyContract { ...} 2.Operation contracts define the individual operations that a service supports and map to operations in WSDL. Operations are defined by adding methods to a Service Contract interface that is annotated with the OperationContract attribute. [OperationContract] void SomeOperation(); 3.Data contracts define how complex types are serialized when they are used in WCF service operations. They are defined by applying the DataContract and DataMember attributes to classes. [DataContract] public class SomeType{ [DataMember] public int ID;} 4.Message contracts describe the entire SOAP message format. They can use data contracts and serializable types to emit schema for complex types, and they also make it possible to control the SOAP message headers and body explicitly, by using a single type. Message contracts provide a simple method to add custom SOAP headers to incoming and outgoing messages. [MessageContract] public class MyRequest { [MessageHeader] public string field1; [MessageBody] public string field2; } 5.A WCF service reports errors by using Fault objects. Fault contracts document the errors that WCF code is likely to produce, and WCF maps Fault objects to SOAP faults. Note that the type specified in the FaultContract does not have to be an exception, although it often will be. Example [DataContract] public class Eval { [DataMember] public string Id; [DataMember] public string Comments; [DataMember] public string Submitter; [DataMember] public DateTime TimeSubmitted; } [ServiceContract] public interface IEvalService { [OperationContract] void SubmitEval(Eval eval); [OperationContract] List GetEval(); [OperationContract] void RemoveEval(string id); } [ServiceBehavior(InstanceContextMode= InstanceContextMode.Single)] public class EvalService : IEvalService { #region IEvalService Members List evals = new List(); public void SubmitEval(Eval eval) { eval.Id = Guid.NewGuid().ToString(); evals.Add(eval); } public List GetEval() { return evals; } public void RemoveEval(string id) { evals.Remove(evals.Find(e => e.Id.Equals(id))); } #endregion }

Comments

Post a Comment

Popular posts from this blog

How to get motherboard serial number, Processor ID, VolumeSerialNumber using C#?

Fiserv Interview Questions.

AngularJs - Simple Example