2012-02-12 26 views
0

製作一個小型的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 
     } 
    } 
} 

回答

2

您將需要一個接口到您的服務以及您的服務公開的每個方法的代碼。

這是從我的頭頂,未經檢驗的,微軟的例子會告訴你這個東西貼由Bryan

例如,添加2個新的類別:

// First the Interface, things in here decorated with the Operation contract gets exposed, things with serializable are available to the client 

namespace MyService 
{ 
    [ServiceContract] 
    public interface ICustomer 
    { 

     [OperationContract] 
     int GetBalance(Guid CustomerId); 
    } 

    [Serializable] 
    public class Customer 
    { 
     public Guid Id; 
     public int Balance; 
    } 
} 




// 2nd class file is the code mapping to the interface 
namespace MyService 
{ 
    private List<Customer> Customers = new List<Customer>(); 
    public class MyService : ICustomer 
    { 
     public int GetBalance(Guid CustomerId) 
     { 
      foreach(Customer c in Customers) 
      { 
       if(c.Id == CustomerId) 
       { 
        return c.Balance; 
       } 
      } 
     } 
    } 
} 
+0

嗯。看起來很有趣。我必須使用Guid作爲客戶的ID嗎?我打算按照我的源代碼使用字符串。 – Bob 2012-02-12 21:56:49

+1

嗨鮑勃,根本沒有,你可以使用任何你喜歡的客戶ID。這只是一個例子,所以你可以看到更深入的類。 – Jeggs 2012-02-12 22:00:07

+0

好的。謝謝! – Bob 2012-02-12 23:36:33

0

首先,從here's MS在WCF一堆好樣的,讓你開始。對於這個演示軟件,我會考慮創建一種方法來將所有'測試'數據生成爲某種集合,並使用它來在您的服務中進行遊玩。一旦您對這些概念感到滿意,就可以將數據庫用作數據存儲。另外看看here用於初始化WCF服務。

也看這個相似的post

相關問題