这只是个简单的学习练习程序,代码内容并木有良好的注释,只作为学习记录,仅供参考。
解决方案目录说明:
源代码:
1.BugQiang.WcfClient
class="code_img_closed" src="/Upload/Images/2013071103/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('645cc1c5-204f-481f-85a0-ce6523363dae',event)" src="/Upload/Images/2013071103/2B1B950FA3DF188F.gif" alt="" />1 using System; 2 using System.Diagnostics; 3 using System.ServiceModel; 4 using BugQiang.WcfWinServices.WcfServices; 5 6 namespace BugQiang.WcfClient { 7 class Program { 8 9 static string WCF_URI = @"http://localhost:9045/WCFServices"; 10 11 static void Main(string[] args) { 12 Stopwatch stopwatch = new Stopwatch(); 13 stopwatch.Start(); 14 using (ChannelFactory<ICalculatorService> bugChannel = new ChannelFactory<ICalculatorService>(new BasicHttpBinding(), WCF_URI)) { 15 ICalculatorService calculatorService = bugChannel.CreateChannel(new EndpointAddress(WCF_URI)); 16 double z = calculatorService.Add(1, 1); 17 Console.WriteLine("1 + 1 = {0}", z); 18 Console.WriteLine("用时{0}ms", stopwatch.ElapsedMilliseconds); 19 stopwatch.Stop(); 20 Console.Read(); 21 } 22 } 23 } 24 }Program.cs
2.BugQiang.WcfWinService
WASService
1 using System.Configuration.Install; 2 using System.ComponentModel; 3 using System.ServiceProcess; 4 5 namespace BugQiang.WcfWinServices.WASService { 6 [RunInstaller(true)] 7 public class WASProjectInstaller : Installer { 8 9 public WASProjectInstaller() { 10 Initialize(); 11 } 12 13 private void Initialize() { 14 ServiceProcessInstaller wasServiceProcessInstaller; 15 wasServiceProcessInstaller = new ServiceProcessInstaller(); 16 wasServiceProcessInstaller.Account = ServiceAccount.LocalSystem; 17 18 ServiceInstaller wasServiceInstaller; 19 wasServiceInstaller = new ServiceInstaller(); 20 wasServiceInstaller.ServiceName = "WcfWinService"; 21 wasServiceInstaller.Description = "WCF服务ICalculatorServices的WAS宿主服务"; 22 wasServiceInstaller.DisplayName = "WCF服务ICalculatorServices的WAS宿主服务"; 23 wasServiceInstaller.StartType = ServiceStartMode.Manual; 24 25 this.Installers.AddRange(new Installer[]{ 26 wasServiceProcessInstaller, 27 wasServiceInstaller}); 28 } 29 } 30 }WASProjectInstaller.cs
1 using System; 2 using System.ServiceProcess; 3 using System.ServiceModel; 4 using BugQiang.WcfWinServices.WcfServices; 5 using System.IO; 6 using System.Diagnostics; 7 using System.ServiceModel.Channels; 8 9 namespace BugQiang.WcfWinServices.WASService { 10 class WcfWinService : ServiceBase { 11 12 const string _logPath = @"C:\WcfWinService.log"; 13 const string _endpointPath = @"C:\endpoint.log"; 14 const string _logFormat = "Time:{0}\r\nEvent:{1}"; 15 16 ServiceHost _wcfHost; 17 string _address; 18 Type _contract; 19 Binding _binding; 20 Type _serviceType; 21 22 public WcfWinService() { 23 Initialize(); 24 } 25 26 private void Initialize() { 27 this.ServiceName = "WcfWinService"; 28 29 _address = "http://127.0.0.1:9045/WCFServices"; 30 _binding = new BasicHttpBinding(); 31 _contract = typeof(ICalculatorService); 32 _serviceType = typeof(CalculatorService); 33 _wcfHost = new ServiceHost(_serviceType); 34 _wcfHost.AddServiceEndpoint(_contract, _binding, _address); 35 } 36 37 protected override void OnStart(string[] args) { 38 try { 39 _wcfHost.Open(); 40 File.AppendAllText(_logPath, string.Format(_logFormat, DateTime.Now, "CalculatorWinService已成功启动!")); 41 File.WriteAllText(_endpointPath, string.Format("Address:{0}\r\nBinding:{1}\r\nContract:{2}", _address, _binding, _contract)); 42 } catch (Exception ex) { 43 File.AppendAllText(_logPath, string.Format(_logFormat, DateTime.Now, ex)); 44 Process.Start(_logPath); 45 } 46 } 47 48 protected override void OnStop() { 49 try { 50 _wcfHost.Close(); 51 File.AppendAllText(_logPath, string.Format(_logFormat, DateTime.Now, "CalculatorWinService已成功关闭!")); 52 } catch (Exception ex) { 53 File.AppendAllText(_logPath, string.Format(_logFormat, DateTime.Now, ex)); 54 Process.Start(_logPath); 55 } 56 } 57 } 58 }WcfWinService.cs
WcfServices
1 using System.ServiceModel; 2 3 namespace BugQiang.WcfWinServices.WcfServices { 4 [ServiceContract] 5 public interface ICalculatorService { 6 [OperationContract] 7 double Add(double x, double y); 8 9 [OperationContract] 10 double Sub(double x, double y); 11 12 [OperationContract] 13 double Multi(double x, double y); 14 15 [OperationContract] 16 double Div(double x, double y); 17 } 18 19 public class CalculatorService : ICalculatorService { 20 #region ICalculatorServices Members 21 22 public double Add(double x, double y) { 23 return x + y; 24 } 25 26 public double Sub(double x, double y) { 27 return x - y; 28 } 29 30 public double Multi(double x, double y) { 31 return x * y; 32 } 33 34 public double Div(double x, double y) { 35 return x / y; 36 } 37 38 #endregion 39 } 40 }ICalculatorService.cs
1 using System.ServiceProcess; 2 using BugQiang.WcfWinServices.WASService; 3 4 namespace BugQiang.WcfWinServices { 5 class Program { 6 static void Main() { 7 ServiceBase.Run(new WcfWinService()); 8 } 9 } 10 }Program.cs
3.安装和测试记录
Setting environment for using Microsoft Visual Studio 2008 x86 tools. C:\Windows\system32>cd C:\Users\de\Documents\Visual Studio 2008\Projects\Export C:\Users\de\Documents\Visual Studio 2008\Projects\Export>installutil wcfwinservice.exe Microsoft (R) .NET Framework 安装实用工具版本 2.0.50727.6387 版权所有(C) Microsoft Corporation。保留所有权利。 正在运行事务处理安装。 正在开始安装的“安装”阶段。 查看日志文件的内容以获得 C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.exe 程序集的进度。 该文件位于 C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.InstallLog。 正在安装程序集“C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.exe”。 受影响的参数是: logtoconsole = assemblypath = C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.exe logfile = C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.InstallLog 正在安装服务 WcfWinService... 已成功安装服务 WcfWinService。 正在日志 Application 中创建 EventLog 源 WcfWinService... “安装”阶段已成功完成,正在开始“提交”阶段。 查看日志文件的内容以获得 C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.exe 程序集的进度。 该文件位于 C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.InstallLog。 正在提交程序集“C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.exe”。 受影响的参数是: logtoconsole = assemblypath = C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.exe logfile = C:\Users\de\Documents\Visual Studio 2008\Projects\Export\wcfwinservice.InstallLog “提交”阶段已成功完成。 已完成事务处理安装。 C:\Users\de\Documents\Visual Studio 2008\Projects\Export>sc start wcfwinservice SERVICE_NAME: wcfwinservice TYPE : 10 WIN32_OWN_PROCESS STATE : 2 START_PENDING (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN) WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x7d0 PID : 5780 FLAGS : C:\Users\de\Documents\Visual Studio 2008\Projects\Export>wcfclient 1 + 1 = 2 用时1662ms C:\Users\de\Documents\Visual Studio 2008\Projects\Export>wcfclient 1 + 1 = 2 用时1131ms安装和测试记录
4.服务的调试以及实用命令工具
1.服务的调试可以通过vs studio中附加进程的方式进行debug调试
2.sc.exe 服务控制程序
3.installutil.exe 安装程序