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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值