Making a small WCF test program which is based on a Store that has customers and each customer has a balance of special points. For now I want to focus on having a fixed number of customers, say 3. Problem is I can’t wrap my head around where would I create those customers and how would I access them from the service’s functions. I was thinking of creating an array of customers inside main() of the service so that they are available to the client later on, but is it a good idea? Very fresh to WCF and couldn’t find any examples that would show how to implement something like that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
namespace StoreService
{
public class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:6112/StoreService");
ServiceHost host = new ServiceHost(typeof(Store), baseAddress);
try
{
host.AddServiceEndpoint(typeof(IStore), new WSHttpBinding(), "Store");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate the service.");
Console.WriteLine();
Console.ReadLine();
host.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
host.Abort();
}
}
}
[ServiceContract(Namespace="http://StoreService")]
public interface IStore
{
[OperationContract]
void AddPoints(string accNum, double points);
[OperationContract]
void SubtractPoints(string accNum, double points);
[OperationContract]
int CustomerPoints(string accNum);
}
[DataContract]
public class Customer
{
private string accountNumber;
private string name;
private double points;
[DataMember]
public int AccountNumber
{
get { return accountNumber; }
set { accountNumber = value; }
}
[DataMember]
public string Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public int Points
{
get { return points; }
set { points = value; }
}
}
public class Store : IStore
{
public void AddPoints(string accNum, double points)
{
//Add points logic
}
public void SubtractPoints(string accNum, double points);
{
//Substract points logic
}
public int CustomerPoints(string accNum)
{
//Show points
}
}
}
Your going to need an Interface to your service and the code for each of the methods exposed by your service.
This is off the top of my head, untested, the Microsoft examples will show you this stuff posted by Bryan
For example, add 2 new classes: