Simple Demonstration of WCF Services

Hmanjarawala
Posted by Hmanjarawala under WCF category on | Points: 40 | Views : 2845
WCFLibrary

IService


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFLibrary
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(string value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}

public enum Operation { ADD=0, MINUS, MULTIPLY, DIVIDE};

// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
double operand1 = 0, operand2 = 0, result = 0;
Operation operater;

[DataMember]
public double Operand1 { get; set; }
[DataMember]
public double Operand2 { get; set; }
[DataMember]
public Operation Operator { get; set; }
[DataMember]
public double Result { get; set; }
}
}

Service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFLibrary
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class Service : IService
{
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
switch (composite.Operator)
{
case Operation.ADD:
composite.Result = composite.Operand1 + composite.Operand2;
break;
case Operation.MINUS:
composite.Result = composite.Operand1 - composite.Operand2;
break;
case Operation.MULTIPLY:
composite.Result = composite.Operand1 * composite.Operand2;
break;
case Operation.DIVIDE:
if (composite.Operand2 != 0)
composite.Result = composite.Operand1 / composite.Operand2;
else
composite.Result = -0;
break;
}
return composite;
}
}
}

Window Application

Imports System.ComponentModel

Public Class Form1
Private OperatorList As New BindingList(Of JobService.Operation)
Private Sub btnMethod1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMethod1.Click

Dim obj As New JobService.ServiceClient()

If Not String.IsNullOrEmpty(tbInputParam.Text.Trim) Then
tbOutput.Text &= obj.GetData(tbInputParam.Text.Trim) & vbCrLf
Else
tbOutput.Text &= obj.GetData("Hello World") & vbCrLf
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

OperatorList.Add(JobService.Operation.ADD)
OperatorList.Add(JobService.Operation.MINUS)
OperatorList.Add(JobService.Operation.MULTIPLY)
OperatorList.Add(JobService.Operation.DIVIDE)

cmbOperator.DataSource = OperatorList
End Sub

Private Sub btnMethod2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMethod2.Click
Dim operand1, operand2 As Double

operand1 = 0
operand2 = 0

If Not String.IsNullOrEmpty(tbOperand1.Text.Trim) Then
operand1 = Convert.ToDouble(tbOperand1.Text.Trim)
End If

If Not String.IsNullOrEmpty(tbOperand2.Text.Trim) Then
operand2 = Convert.ToDouble(tbOperand2.Text.Trim)
End If

Dim composite As New JobService.CompositeType
composite.Operand1 = operand1
composite.Operand2 = operand2
composite.Operator = cmbOperator.SelectedValue

Dim obj As New JobService.ServiceClient()

composite = obj.GetDataUsingDataContract(composite)

tbOutput.Text &= cmbOperator.SelectedValue.ToString & "(" & composite.Operand1.ToString() & ", " & composite.Operand2.ToString() & ") = " & composite.Result.ToString & vbCrLf

End Sub
End Class

Comments or Responses

Login to post response