静观己心,厚积薄发

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

这段时间学习wcf,做了一个小例子,实现了一个产生随机数的wcf服务,寄存于windows服务中,然后通过一个form程序来显示其产生的随机数。

开发工具:VS2010

语言:C#

1.建立3个工程,分别为form(调用wcf服务),wcf服务,windowsservicehost(寄存wcf服务),如下图

2.在WcfService的服务实现中,编写产生随机数的代码

MyService.svc
1 public class MyService : IMyService
2 {
3 private static Timer mytimer = new Timer(100);
4
5 private static string msg = string.Empty;
6
7 private Random rand = new Random(DateTime.Now.Millisecond);
8
9 public void TimerInit()
10 {
11 mytimer.Elapsed += new ElapsedEventHandler(TimerTickEvent);
12
13 mytimer.AutoReset = true;
14
15 mytimer.Interval = 200;
16
17 mytimer.Enabled = false;
18 }
19
20 private void TimerTickEvent(object sender,ElapsedEventArgs e)
21 {
22 msg = string.Format("return code:{0}", rand.Next(1000));
23 }
24
25 public bool TimerStart()
26 {
27 if (!mytimer.Enabled)
28 {
29 mytimer.Enabled = true;
30 }
31
32 return mytimer.Enabled;
33 }
34
35 public bool TimerStop()
36 {
37 if (mytimer.Enabled)
38 {
39 mytimer.Enabled = false;
40 }
41
42 return mytimer.Enabled;
43 }
44
45 public string GetData()
46 {
47 return msg;
48 }
49 }

3.在WindowsServiceHost(这是一个Windows服务)工程中,打开ProjectInstaller.cs,查看serviceInstaller1的属性,将ServiceName修改为你所需要的服务名称(我这里是LR)。查看serviceProcessInstaller1的属性,修改Account为LocalSystem。

修改Service1.cs源代码如下:

public partial class LR : ServiceBase
    {
        private ServiceHost _host = new ServiceHost(typeof(MyService));

        public LR()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            _host.Open();
        }

        protected override void OnStop()
        {
            _host.Close();
        }
    }

4.编辑WindowsServiceHost的App.config文件如下内容:

App.config
1 <?xml version="1.0" encoding="utf-8"?>
2  <configuration>
3 <system.serviceModel>
4 <behaviors>
5 <serviceBehaviors>
6 <behavior name="NewBehavior">
7 <serviceMetadata httpGetEnabled="true"/>
8 </behavior>
9 </serviceBehaviors>
10 </behaviors>
11 <services>
12 <service behaviorConfiguration="NewBehavior" name="WcfService.MyService">
13 <endpoint address="basic" binding="basicHttpBinding"
14 bindingConfiguration="" contract="WcfService.IMyService" />
15 <host>
16 <baseAddresses>
17 <add baseAddress="http://localhost:3302/MyService.svc"/>
18 </baseAddresses>
19 </host>
20 </service>
21 </services>
22 </system.serviceModel>
23  </configuration>

5.生成WcfService和WindowsServiceHost工程,然后以管理员身份打开VS2010命令行工具,进入到生成后的WindowsServiceHost程序目录,开始创建windows服务。

    1)输入 installutil WindowsServiceHost.exe  回车(安装windows服务,我这里安装完成后的服务名为“LR”,如下图)

      

    2)输入 net start LR 回车(启动LR服务,当然,也可以通过服务管理器来启动)

6.进入FormTest工程,右键Service References,选择添加服务引用,填入之前app.config中的元地址(我这里是:http://localhost:3302/MyService.svc)。提醒:如果选择发现,默认会搜索到解决方案中的WcfService,这样会导致元地址错误,所以一定要手动输入。然后命名为“LRService”(自定义),如下图

7. 在form1.cs中编写form调用服务的代码:

form1.cs
1 LRService.MyServiceClient lrClient = new LRService.MyServiceClient();
2
3 private static int rownum = 1;
4
5 private void btnGetService_Click(object sender, EventArgs e)
6 {
7
8 lrClient.TimerInit();
9 lrClient.TimerStart();
10
11 MyTimer.Enabled = true;
12 }
13
14 private void MyTimer_Tick(object sender, EventArgs e)
15 {
16 if (listBoxMsg.Items.Count >= 200)
17 {
18 listBoxMsg.Items.Clear();
19 rownum = 1;
20 }
21
22 listBoxMsg.Items.Add(rownum.ToString()+":" + lrClient.GetData());
23 listBoxMsg.SelectedIndex = listBoxMsg.Items.Count - 1;
24 rownum++;
25 }
26
27 private void btnStop_Click(object sender, EventArgs e)
28 {
29 MyTimer.Enabled = false;
30 }

OK,编译运行即可看到form从寄存于windows服务中的wcf服务读取的随机数:

posted on 2010-12-14 12:13  猎人杰  阅读(5152)  评论(0编辑  收藏  举报