製作一個小型的WCF
測試程序,該程序基於擁有客戶的商店並且每個客戶都有一個特殊點的餘額。現在我想關注的是擁有固定數量的客戶,比如說3。問題是我不能籠絡我的頭,我會在哪裏創建這些客戶,以及如何從服務的功能訪問它們。我正在考慮在服務的main()
內部創建一系列客戶,以便稍後客戶可以使用它們,但這是一個好主意嗎?非常新鮮到WCF
,並找不到任何示例來展示如何實現類似的東西。如何訪問自定義類對象並從WCF服務更改其數據?
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
}
}
}
嗯。看起來很有趣。我必須使用Guid作爲客戶的ID嗎?我打算按照我的源代碼使用字符串。 – Bob 2012-02-12 21:56:49
嗨鮑勃,根本沒有,你可以使用任何你喜歡的客戶ID。這只是一個例子,所以你可以看到更深入的類。 – Jeggs 2012-02-12 22:00:07
好的。謝謝! – Bob 2012-02-12 23:36:33