WCF - Service Application Demo - 2


WCF - Service Application Demo - 2

  • Double click on the IService1.cs to open the service application.

    Iservice1 is a service interface to define the signature of a web service to the clients.
    Import the Library “System.Runtime.Serialization”. This specifies that the code can implement a data contract and can be serialized using DataContractSerializer
  The [ServiceContract] attribute similar to [WebService] Indicates that an interface or a class outlines a service contract with a definition for the operations provided by that service.
  The [OperationContact] attribute is used explicitly in WCF to declare the method as an operation to be exposed by a ServiceContract.
    A [DataContract] is a way of agreement between a service and a client about the data to be exchanged. The client and the service must share the same types and the data contract precisely defines what data is serialized (turned into XML) to be exchanged. WCF uses a serialization engine called the Data Contract Serializer to serialize and deserialize data.
    The [DataMember] attribute is used to expose the data along with its contract. Without [DataMember], then service won’t be able to transfer the data.
  • Double click the Service1.svc.cs file to open it.

  The Service1.svc is created automatically as part of the solution for WCF Service Application to provide access to a client on the service operations.
    Import the Library “System.Runtime.Serialization”. This specifies that the code can implement a data contract and can be serialized by DataContractSerializer
  The Service1 class implements the interface IService1 to provide method definitions for the Interface method signatures.
  The method public string GetData() takes as input an integer variable and returns a formatted string output.
  The method public CompositeType GetDataUsingDataContract() takes as input a CompositeType and process it and returns a CompositeType value.
  • Execute the service application by clicking “Start Debugging” (F5) from Visual Studio and in the browser select the “Service1.svc” file the output appears.



    Service1 Service is the name of the exposed WCF Service.
    The URL http://localhost:12684/Service1.svc?wsdl defines the service URL for access by the client.
  The Main() indicates the way to create a service instance and access service operations for the C# client.
    The same way Main() method usage is depicted for Visual Basic users.