码迷,mamicode.com
首页 > Windows程序 > 详细

create windows service base on net.pipe ,windows 服务

时间:2016-05-07 10:00:50      阅读:498      评论:0      收藏:0      [点我收藏+]

标签:

1.创建一个windows服务

技术分享


2.添加安装程序

技术分享


3.修改

技术分享技术分享



4.删除自带的Proman.CS 的MAIN函数


5.修改Services,添加ServiceModel.dll

public class NotificationWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;


        public NotificationWindowsService()
        {
            ServiceName = "WCFNotificationService";
            //InitializeComponent();
        }




        public static void Main()
        {
            ServiceBase.Run(new NotificationWindowsService());
        }


        protected override void OnStart(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            if (serviceHost != null)
            {
                serviceHost.Close();
            }
            serviceHost = new ServiceHost(typeof(Matcher));


            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            try
            {
                serviceHost.Open();


            }
            catch (Exception ex)
            {
            }




        }


        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }


        private void InitializeComponent()
        {
            // 
            // NotificationWindowsService
            // 
            this.ServiceName = "WCFNotificationService";


        }
    }


6.创建Service Interface

namespace HandPickMatchServices
{
    [ServiceContract(Namespace = "HandPickMatchServices", ConfigurationName = "IMatcher")]
    interface IMatcher
    {
        [OperationContract]
        Task DoMatchIngredient(List<string> ingredients);
    }

}


7.创建Service Instance

namespace HandPickMatchServices
{
    public class Matcher : IMatcher
    {
        public async Task DoMatchIngredient(List<string> ingredients)
        {

        }
    }
}


8.添加Configuration <system.serviceModel>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>


  <system.serviceModel>
    <services>
      <!-- This section is optional with the new configuration model
           introduced in .NET Framework 4. -->
      <service name="HandPickMatchServices.Matcher" behaviorConfiguration="MatcherBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.pipe://localhost/notification/service" />
          </baseAddresses>
        </host>
        <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->
        <endpoint address="" binding="netNamedPipeBinding" contract="IMatch" bindingConfiguration="Custom.WSHTTPBinding.Configuration">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <netNamedPipeBinding>
        <binding name="Custom.WSHTTPBinding.Configuration" transferMode="Streamed" maxBufferPoolSize="4194304" maxReceivedMessageSize="9223372036854775807">
          <security mode="None" />
        </binding>
      </netNamedPipeBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MatcherBehavior">
          <serviceMetadata httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  
  




add reference

net.pipe://localhost/crawler/service/mex


install service\

1.

输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车

切换当前目录,此处需要注意的是,在C:\Windows\Microsoft.NET\Framework目录下有很多类似版本,具体去哪个目录要看项目的运行环境,例 如果是.net framework2.0则需要输入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727


2.

Install:

InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe 

uninstall:

 InstallUtil.exe /u E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe 


http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html










create windows service base on net.pipe ,windows 服务

标签:

原文地址:http://blog.csdn.net/lglgsy456/article/details/44153977

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!