运行服务作为控制台应用

时间:2015-03-21 13:13:59

标签: c# wcf windows-services

我正在编写一个公开WCF接口的Windows服务。这一切都可以作为已安装的服务正常工作我正在整合更改,以便我可以将其作为控制台应用程序运行,以便按照http://makit.net/post/running-a-windows-service-as-a-console-application等多个网站的指南进行测试。

当它作为服务运行时它很好...当我作为控制台运行时它似乎运行正常但是当我使用wcftestclient测试时,我能够添加服务但是调用方法会导致错误。 ...

  

无法调用该服务。可能的原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。

     

对SSPI的调用失败 - 内部例外:   系统检测到可能危及安全性的尝试

我已将此作为管理员运行,但没有区别......

这是我的代码

using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using AgentCoreService;
using Interfaces;
using NLog;
using System;
using System.ComponentModel;
using System.Configuration.Install;

namespace Service
{
public class AgentWindowsService : ServiceBase
{
    public ServiceHost serviceHost = null;

    private static Logger logger = LogManager.GetCurrentClassLogger();


    public static void Main()
    {
        var serviceToRun = new AgentWindowsService();
        if (Environment.UserInteractive)
        {
            serviceToRun.OnStart(null);
            Console.WriteLine("Press any key to stop the service");
            Console.Read();
            serviceToRun.OnStop();
        }

        Run(serviceToRun);
    }


    public AgentWindowsService()
    {
        // Name the Windows Service
        ServiceName = "AgentWindowsServiceSample";
    }

    // Start the Windows service.
    protected override void OnStart(string[] args)
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }
        // Create a ServiceHost for the AgentService type and
        // provide the base address.
        Uri baseAddress = new Uri("net.pipe://localhost/test");

        serviceHost = new ServiceHost(typeof(AgentService), baseAddress);

        NetNamedPipeBinding pipebinding = new NetNamedPipeBinding();

        pipebinding.SendTimeout = TimeSpan.FromMinutes(15);
        pipebinding.ReceiveTimeout = TimeSpan.FromMinutes(15);

        var t = Type.GetType("Interfaces.IEchoString, Interfaces");
        serviceHost.AddServiceEndpoint(t, pipebinding, baseAddress);

        serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
        serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexNamedPipeBinding(), baseAddress + "/mex");


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

        logger.Info("Host open");
        logger.Info("OptimizerService is up and running with the following endpoints...");

        foreach (ServiceEndpoint se in serviceHost.Description.Endpoints)
        {
            logger.Info(se.Address.ToString()); ;
        }

    }

    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
}
// Provide the ProjectInstaller class which allows 
// the service to be installed by the Installutil.exe tool
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
    private ServiceProcessInstaller process;
    private ServiceInstaller service;

    public ProjectInstaller()
    {
        process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;
        service = new ServiceInstaller();
        service.ServiceName = "AgentWindowsServiceSample";
        Installers.Add(process);
        Installers.Add(service);
    }
}
}

0 个答案:

没有答案