在Windows服务中安装和运行WCF服务

dru*_*bit 2 .net c# wcf windows-services

我不是C#或.NET专家.但是,我必须使用它......

我正在运行InstallUtil.exe MyService.exe以安装基本上运行WCF服务的Windows服务.我已经定义了WFC接口并实现了它.以下是界面.

[ServiceContract(Namespace = "http://WCFService", Name = "WCFService")]
public interface IWCFService
{
    [OperationContract]
    User Login(string userName, string password);

    [OperationContract]
    List<Project> GetProjects(Guid userGuid);

    [OperationContract]
    List<Stylesheet> GetStylesheets(Guid projectGuid);

}
Run Code Online (Sandbox Code Playgroud)

我还定义了一个Windows服务,如下所示:

public partial class Service: ServiceBase
{
    public FlatWsdlServiceHost m_fwsh = null; // extends ServiceHost

    public DesignerService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {

        this.EventLog.WriteEntry("OnStart Successfull", EventLogEntryType.Information);

        if (m_fwsh != null)
        {
            m_fwsh.Close();
        }

        // Create a ServiceHost for the EventWebService type and 
        // provide the base address.
        Uri localUri= new Uri("http://localhost:7777/");
        m_fwsh = new FlatWsdlServiceHost(typeof(WCFService), localUri);

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

    protected override void OnStop()
    {
        //base.OnStop();

        if (m_fwsh != null)
        {
            m_fwsh.Close();
            m_fwsh = null;
        }
    }

    protected override void OnPause()
    {
        base.OnPause();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行InstallUtil.exe MyService.exe日志时说:

Installing assembly 'MyService.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = MyService.InstallLog
   assemblypath = MyService.exe
No public installers with the RunInstallerAttribute.Yes attribute could be found in the MyService.exe assembly.
Committing assembly 'MyService.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = MyService.InstallLog
   assemblypath = MyService.exe
No public installers with the RunInstallerAttribute.Yes attribute could be found in the MyService.exe assembly.
Remove InstallState file because there are no installers.
Run Code Online (Sandbox Code Playgroud)

另外,我有一个ProjectInstaller.cs,它初始化serviceInstaller1和serviceProcessInstaller1.在添加WCF服务之前,Windows服务正在安装正常并将"OnStart Successfull"消息写入Windows日志.

任何帮助或建议都非常感谢.

Pio*_*app 5

你需要这样的课程:

// 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 = "WCFWindowsServiceSample";
        Installers.Add(process);
        Installers.Add(service);
    }
}
Run Code Online (Sandbox Code Playgroud)

完整的教程在MSDN上