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

来源:互联网 发布:网络骰宝赌博 编辑:程序博客网 时间:2024/05/29 13:47

1.创建一个windows服务

image


2.添加安装程序

image


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










0 0
原创粉丝点击