WCF走你~一個簡單的例子,根據用戶ID,從用戶模塊(用戶服務器)獲得實體


標題有有意思,說是再次看WCF,意思是說之前也簡單的看過,但沒有用到實際項目中來,這一次,准備大規模的體驗一下WCF。

今天主要是一個例子,從用戶服務器,根據指定用戶ID,還回它的用戶實體,即,用戶模塊的代碼是寫好的,可以供其它項目,業務去調用,提供一個開放的服務平台,先看一下結構圖

在WCF Service文件夾中,Contracts是指定義服務契約(Service Contract),引用System.ServiceMode程序集

Service提供對WCF服務的實現。即在契約Contracts的實現,所以Services具有對Contracts項目的引用;

Hosting實現對定義在Services項目中的服務的寄宿,它可以由控制台程序實現,也可以發布到IIS上。

Client是一個客戶端程序,它有對Service的服務引用,主要從Service中獲取服務

Entity和VCommons都是公用項目,在我的這篇博文中有說明

下面我們來一步一步創建這個WCF程序

一  建立Contracts契約,代碼如下:

 [ServiceContract(Name = "UserService", Namespace = "http://www.wcf.com/")]
    public interface IUser
    {
        [OperationContract]
        bool Register(Entity.UserBases entity);
        [OperationContract]
        byte[] GetUserByID(int userID);
        [OperationContract]
        UserBases GetUserBaseByID(int userID);
    }

我們看到,它實事上就是一種操作的規范,我們在WCF里叫它服務規范或服務契約

二  建立Service項目,代碼如下:

 public class UserService : IUser
    {
        static Entity.Car_RentalDataContext db;
        static UserService()
        {
            db = new Entity.Car_RentalDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["Car_Rental"].ToString());
        }
        

        public bool Register(Entity.UserBases entity)
        {
            try
            {
                db.UserBases.InsertOnSubmit(entity);
                db.SubmitChanges();
                return true;
            }
            catch (System.Exception)
            {
                return false;
                throw;
            }

        }

        public byte[] GetUserByID(int userID)
        {
            return VCommons.SerializationHelper.ObjectToByteArray(db.GetTable<UserBases>().Select(i => new UserBases_Ext
            {
                UserID = i.UserID,
                Name = i.Name,
            }).FirstOrDefault(i => i.UserID == userID));
        }
}

事實上,WCF服務就是要把服務契約進行實現,我進行了一個簡單的測試,WCF也是走SOAP協議,所以 Register(Entity.UserBases entity)這種方法肯定是不行的,我們需要將它進

行序列化。

三 創建服務宿主,事實上,我們最習慣的宿主方式就是像WebService式的IIS宿主,原來WS里有ASMX,現在WCF里叫SVC,先看一個SVC文件內容如下:

1 <%@ ServiceHost Service="Artech.WcfServices.Services.UserService" %>

直接使用ServiceHost即可,注冊,在項目中,要引用System.ServiceModel程序集。

再看一下控制台程序的宿主代碼:

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             using (ServiceHost host = new ServiceHost(typeof(UserService)))
 6             {
 7 
 8                 host.AddServiceEndpoint(typeof(IUser), new WSHttpBinding(), "http://127.0.0.1:9999/UserService");
 9                 if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
10                 {
11                     ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
12                     behavior.HttpGetEnabled = true;
13                     behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/UserService/metadata");
14                     host.Description.Behaviors.Add(behavior);
15                 }
16                 host.Opened += delegate
17                {
18                    Console.WriteLine("Service已經啟動");
19 
20                };
21 
22                 host.Open();
23                 Console.Read();
24             }
25 
26         }
27 
28     }

而對於,客戶端來說,我們需要對Service項目進行服務引用

如果你的服務是一個獲取數據源的服務,WCF還會在你的項目中生成一個.datasource的數據源,client項目代碼如下:

1            using (UserServiceClient proxy = new UserServiceClient())
2             {
3                 UserBases_Ext entity = (UserBases_Ext)VCommons.SerializationHelper.ByteArrayToObject(proxy.GetUserByID(1));
4                 Console.Write(entity == null ? "沒有找到" : entity.Name);
5 
6                 UserBases entity2 = proxy.GetUserBaseByID(1);//soap不支持復雜類型
7                 Console.Write(entity2 == null ? "沒有找到" : entity2.Name);
8                 Console.ReadKey();
9             }

OK,到這里,我們的第一個WCF實例就完成了。

注意:linq to sql生成的實體,再進行序列化時,會報異常的,我們需要為它和它的派生類重新做序列化標記,然后返回數據時,使用派生類型和可以,代碼如下:

1     [Serializable]
2     public partial class UserBases
3     {
4         //linq實體擴展功能
5     }
6     [Serializable]
7     public class UserBases_Ext : UserBases //UserBases_Ext和UserBases需要同時被標記為可序列化
8     { }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM