Monday, November 20, 2006

* WCF : Defining & Implementing Contracts

The below example shows how to define service, operation and data contract and also implements the same :

 

[ServiceContract()]
public interface IBank
{
[OperationContract]
Account Deposit(Account depAccount, int amount);
[OperationContract]
Account GetBalance(string accountNum);
[OperationContract]
Account WithDraw(Account wdAccount, int amount);
} 






 

public class BankService : IBank
{
public Account Deposit(Account depAccount,int depValue)
{
depAccount.Balance = depAccount.Balance + depValue;
return depAccount;
}
public Account WithDraw(Account wdAccount, int wdValue)
{
wdAccount.Balance = wdAccount.Balance - wdValue;
return wdAccount;
}
public Account GetBalance(string accountNum)
{
Account acnt = new Account();
acnt.AccountNumber = accountNum;
acnt.Balance = 12345;
return acnt;
}
}


 

[DataContract]
public class Account
{
string accountNumber;
int balance;

[DataMember]
public string AccountNumber
{
get { return accountNumber; }
set { accountNumber = value; }
}
[DataMember]
public int Balance
{
get { return balance; }
set { balance = value; }
}
}

}









Technorati tags: , ,

No comments:

Post a Comment