最近一直在學習WCF相關知識,下面將通過一個小實例對所學的知識進行簡單的回顧;本實例是一個簡單三層操作數據庫,並且也簡單實現的三種宿主(控制台宿主,IIS宿主以及Windows服務宿主)的練習;還包含一個雙工的功能,下圖為程序所創建分層結構圖;
首先了解為這個實例所創建的兩張簡單表;
![]() |
USE [TestDb] GO /****** 對象: Table [dbo].[T_Account] 腳本日期: 07/31/2013 23:09:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[T_Account]( [ID] [int] IDENTITY(1,1) NOT NULL, [Uid] [int] NULL, [Money] [int] NULL, CONSTRAINT [PK_T_Account] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO USE [TestDb] GO /****** 對象: Table [dbo].[T_User] 腳本日期: 07/31/2013 23:10:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[T_User]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, [PassWord] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY]
|
接下來簡單了解每一層的作用以及一些注意事項;源代碼后面直接提供下載;
1:Service.DAL 是針對數據庫操作的一些類;
2:Service.DbHelp 為數據庫操作提供助手
3:Service.HostTest 為服務提供自宿主的測試;一般在編碼時會用到;此層還用到批量寄存的知識;
4:Service.Interface 為契約層,定義的相關開放接口;
5:Service.Model為實體層也是數據契約層;
6:Service.ServerDemo 為實現契約的服務內容;若有邏輯都放在這一層;
7:WcfServiceForIIs 為宿主在IIS內容;
8:WebClient 為客戶端為Web形式
9:Windows_HostService為宿主要Windows服務里的內容;
10:WindowsClient、WindowsFormsForHostService、WindowsFormsForIIS分別是對三種寄存宿主進行測試的客戶端;
一:注意點批量寄存宿主;把重點的一些配置一及實現代碼貼出;
<configuration> <configSections> <section name="artech.batchingHosting" type="Service.HostTest.Configuration.BatchingHostingSettings, Service.HostTest"/> </configSections> <appSettings> <add key="ConnectionString" value="server=.;database=TestDb;uid=sa;pwd=admin"/> </appSettings> <system.serviceModel> <bindings> <netTcpBinding> <binding name="portSharingBinding" portSharingEnabled="true"></binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="UsermetadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3721/UserService/metadata"/> </behavior> <behavior name="AccountmetadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3721/AccountService/metadata"/> </behavior> <behavior name="ExcptDivedeBehavior"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.ServerDeom.UserService" behaviorConfiguration="UsermetadataBehavior"> <endpoint address="http://127.0.0.1:3721/UserService" binding="wsHttpBinding" contract="Service.Interface.IUser"/> </service> <service name="Service.ServerDeom.AccountService" behaviorConfiguration="AccountmetadataBehavior"> <endpoint address="http://127.0.0.1:3721/AccountService" binding="wsHttpBinding" contract="Service.Interface.IAccount"/> </service> <service name="Service.ServerDeom.DuplexTestService"> <endpoint address="net.tcp://127.0.0.1:3722/DuplexTestService" binding="netTcpBinding" contract="Service.Interface.IDuplexTest" bindingConfiguration="portSharingBinding"/> </service> <service name="Service.ServerDeom.ExcptDivideService" behaviorConfiguration="ExcptDivedeBehavior"> <endpoint address="http://127.0.0.1:3723/ExcptDivideService" binding="wsHttpBinding" contract="Service.Interface.IExcptDivide"/> </service> </services> </system.serviceModel> <artech.batchingHosting> <add type="Service.ServerDeom.UserService, Service.ServerDeom"/> <add type="Service.ServerDeom.AccountService, Service.ServerDeom"/> <add type="Service.ServerDeom.DuplexTestService, Service.ServerDeom"/> <add type="Service.ServerDeom.ExcptDivideService, Service.ServerDeom"/> </artech.batchingHosting> </configuration>
using System.ServiceModel; using Service.ServerDeom; using Service.Interface; namespace Service.HostTest { class Program { static void Main(string[] args) { using (ServiceHostCollection hosts = new ServiceHostCollection()) { foreach (ServiceHost host in hosts) { host.Opened += (sender, arg) => Console.WriteLine("服務{0}開始監聽", (sender as ServiceHost).Description.ServiceType); } hosts.Open(); Console.Read(); } } } }
二:宿主要IIS里的注意內容;
新建一個文本文件然后把它的后綴修改成.svc;並在其頭部增加一行調用服務的代碼;
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServerDeom.UserService" %>
其配置如下:
<configuration> <appSettings> <add key="ConnectionString" value="server=.;database=TestDb;uid=sa;pwd=admin"/> </appSettings> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 為避免泄漏元數據信息,請在部署前將以下值設置為 false 並刪除上面的元數據終結點 --> <serviceMetadata httpGetEnabled="true"/> <!-- 要接收故障異常詳細信息以進行調試,請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.ServerDeom.UserService"> <endpoint binding="ws2007HttpBinding" contract="Service.Interface.IUser"/> </service> </services> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
把I宿主部署到IIS里;
三:宿主在Windows服務;創建一個Windows服務類庫,增加相應的配置以及代碼;
<configuration> <appSettings> <add key="ConnectionString" value="server=.;database=TestDb;uid=sa;pwd=admin"/> </appSettings> <system.serviceModel> <bindings> <netTcpBinding> <binding name="portSharingBinding" portSharingEnabled="true"></binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="UsermetadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3718/UserService/metadata"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.ServerDeom.UserService" behaviorConfiguration="UsermetadataBehavior"> <endpoint address="http://127.0.0.1:3718/UserService" binding="wsHttpBinding" contract="Service.Interface.IUser"/> </service> </services> </system.serviceModel> </configuration>
using System.ServiceModel; using Service.ServerDeom; using Service.Interface; namespace Windows_HostService { public partial class Service1 : ServiceBase { private ServiceHost serviceHost = null; //寄宿服務對象 public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { try { serviceHost = new ServiceHost(typeof(UserService)); if (serviceHost.State != CommunicationState.Opened) { serviceHost.Open(); } } catch (Exception ex) { } } protected override void OnStop() { } } }
然后安裝服務文件的一些屬性進行設置,比如服務自動重啟,服務名稱等;另一些圖是對服務進行安裝;
![]() |
![]() |
若服務安裝成功后會在服務里可以看到如下:
客戶端調用服務的代碼大家就直接看源代碼,由於本人也是剛開始學習WCF,實例內容為本人學習所做,若有不足或錯誤歡迎指正;[源代碼下載]
最近有個妹子弄的一個關於擴大眼界跟內含的訂閱號,每天都會更新一些深度內容,在這里如果你感興趣也可以關注一下(嘿對美女跟知識感興趣),當然可以關注后輸入:github 會有我的微信號,如果有問題你也可以在那找到我;當然不感興趣無視此信息;