C# windows服务

来源:互联网 发布:银行网络应急预案 编辑:程序博客网 时间:2024/05/29 17:00

            最近学习c#,得到大哥指点,学习了C# windows服务。

      部署效果:

  

     运行结果:

           

      

 建立服务端:

      1)  新建一个“窗口应用程序”

        

    2)添加两个类(一个普通类,一个接口类)

       

              代码:      using System;
using System.Collections.Generic;
                                using System.Linq;
                                using System.Text;
                                using System.ServiceModel;


                               namespace WindowsFormsApplication2.TestService
                              {
                                   [ServiceContract]
                                   public interface ITestService
                                   {
                                   [OperationContract]
                                  string Test();
                                   }
                              }


                    using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Text;
                    using System.ComponentModel;
                    namespace WindowsFormsApplication2.TestService
                    {
                     class TestService :ITestService
                   {
                     public string Test()
                   {
                     return "test111111";
                      
                   }
                   }
               }

3)添加  windows服务  项

             

        4)添加方法(启动服务方法)

                 添加windows 服务 项 里的方法

                  protected override void OnStart(string[] args)
        {
            Console.WriteLine("打开测试第一个wcf");
            InitWcf();
            // TODO: 在此处添加代码以启动服务。
        }


        protected override void OnStop()
        {
            Console.WriteLine("关闭测试第一个wcf");
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            if(hostTaskService!=null){
                hostTaskService.Close();
            }
        }
        private void InitWcf()
        {
            hostTaskService = new ServiceHost(typeof(WindowsFormsApplication2.TestService.TestService));
            hostTaskService.Open();
        }

       5)添加安装程序 

       在windows 服务 界面  右击  添加“添加安装程序”  即可

         6)修改Program.cs  方法

          static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {


            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new WindowsFormsApplication3.Service1()
            };
            ServiceBase.Run(ServicesToRun);
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
    }


7) 修改 ProjectInstaller.cs 的InitializeComponent()   设置服务的服务名,和说明



8)修改app.config文件(如果发现没有app.config文件,可以添加   应用程序配置文件  项  即可)


<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
 
  <system.serviceModel>
    
    <bindings>
      <basicHttpBinding>
        <binding name="DefaultBasicHttpBinding" maxBufferSize="10485760"
          maxReceivedMessageSize="10485760">
          <readerQuotas maxStringContentLength="10485760" maxArrayLength="10485760" />
        </binding>
        <binding name="ServiceMainSoap" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WindowsFormsApplication2.TestService.TestService" behaviorConfiguration="DefalutServiceBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="WindowsFormsApplication2.TestService.ITestService"
                  bindingConfiguration="DefaultBasicHttpBinding">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:10054/TestService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefalutServiceBehavior">
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>


8)添加 注册文件

Install.bat(这里注意Framework的版本要和你程序版本一致)

cd %~dp0
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WindowsFormsApplication3.exe -i
pause

和 卸载文件

Uninstall.bat(这里注意Framework的版本要和你程序版本一致)

cd %~dp0
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WindowsFormsApplication2.exe -u
pause



9)  修改注册权限


9)





10)调用服务 

       新建一个控制台程序

添加引用服务  http://localhost:10054/TestService

 static void Main(string[] args)
        {
            ServiceReference1.TestServiceClient s = new ServiceReference1.TestServiceClient();
            Console.WriteLine(s.Test());
            Console.ReadLine();
        }


11) 调用结果





  

0 0
原创粉丝点击