gpt4 book ai didi

wcf - 可以从 WCF 与 Windows 服务通信

转载 作者:行者123 更新时间:2023-12-04 02:52:30 25 4
gpt4 key购买 nike

我对 windows 服务 有点陌生。我在一台机器(机器 1)上运行 WCF 服务,在另一台机器(机器 2)上运行 Windows 服务。

我需要使用 WCF 服务在机器 2 上运行 powershell 脚本。我不知道从哪里开始以及如何完成。此外,我需要将消息从 WCF Web 服务传递到 Windows 服务

请给我建议或提供一些好的示例或教程。

编辑
我想在机器 2 上运行一个 powershell 脚本。这个 powershell 脚本只有 WCF 服务知道。
简单地想做的是,将那个 powershell 传递给机器 2。怎么做??

最佳答案

首先,我假设您正在接管一个现有项目,因为您的问题有点矛盾:您是 Windows 服务的新手,但您说您的系统中有一个。我还会考虑到您必须维护现有的软件模型,同时您必须控制两台机器。所以:

  • 机器 1 上的 WCF 服务提供 PS 脚本
  • 机器 2 上的 Windows 服务应在这些脚本可用并由机器 1 上的 WCF 服务传递时立即执行这些脚本

为了处理这个问题,您可以考虑机器 2 上托管另一个 WCF 服务: How to: Host a WCF Service in a Managed Windows Service

这将如何运作?每次有新的 PS 脚本可用时,您都可以从机器 1 上的 WCF 服务调用机器 2 上的第二个 WCF 服务。随后,机器 2 上的 WCF 服务可能会将脚本保存到某个存储库(文件、数据库、内存中)并调用 ServiceController.ExecuteCommand Method在 Windows 服务上。此时,Windows 服务将从它保存的位置获取脚本并执行它。虽然我觉得这是一个不必要的过于复杂的软件项目,但这里有一个针对您的情况的实现,只是为了回答您的问题:是的,这是可能的

机器 2 上,安装包含 WCF 服务的 Windows 服务:

using System;
using System.Linq;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration.Install;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Sample.Services
{
[ServiceContract(Namespace = "http://Sample.Services")]
public interface IScriptExecutor
{
[OperationContract]
void ExecuteScript(string script);
}

/// <summary>
/// The WCF service class which will pass the script to the Windows
/// service
/// </summary>
public class ScriptExecutorService : IScriptExecutor
{
const string PATH = @"C:\test\queue.txt";
public void ExecuteScript(string script)
{
File.WriteAllText(PATH, script);
ServiceController myService =
new ServiceController("WCFScriptWindowsService");
myService.ExecuteCommand((int)MyCustomCommands.ExecuteScript);

}
}

public class ScriptWindowsService : ServiceBase
{
const string PATH = @"C:\test\queue.txt";
const string PATH_OUT = @"C:\test\out.txt";

public ServiceHost serviceHost = null;
public ScriptWindowsService()
{
ServiceName = "WCFScriptWindowsService";
}

protected override void OnCustomCommand(int command)
{
switch (command)
{
case (int)MyCustomCommands.ExecuteScript:
// Execute the PS script
var ps = PowerShell.Create();
var runspace = RunspaceFactory.CreateRunspace();
ps.Runspace = runspace;
// read the command from a repository,
// could also be a database
ps.AddCommand(File.ReadAllText(PATH));
runspace.Open();
var results = ps.Invoke().ToList();
runspace.Close();
foreach (var result in results)
{
// writing the results to a file
File.AppendAllText(PATH_OUT,
String.Format("{0}\r\n",
result.BaseObject.GetType()));
}
break;
default:
break;
}
}

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

protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
serviceHost =
new ServiceHost(typeof(ScriptExecutorService));
serviceHost.Open();
}

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 = "WCFScriptWindowsService";
Installers.Add(process);
Installers.Add(service);
}
}

/// <summary>
/// Holds the custom commands (only one, in our case)
/// </summary>
public enum MyCustomCommands { ExecuteScript = 128 };
}

机器 1 上,修改 WCF 服务,使其调用机器 2 上的 WCF 服务:

using System.ServiceModel;

namespace Sample.Services
{
[ServiceContract(Namespace = "http://Sample.Services")]
public interface IScriptProvider
{
[OperationContract]
string GetScript();
}
public class ScriptProviderService : IScriptProvider
{
public string GetScript()
{
// do some processing ...
// let's say we end up with the "Get-Service" script
var script = "Get-Service";
// make sure you add a reference to the ExecutorServiceReference
// WCF service on machine 2
var client = new WcfService1.ExecutorServiceReference
.ScriptExecutorClient();
client.ExecuteScript(script);
return script;
}
}
}

我再次强调:您不必从 Windows 服务本身执行 PS 脚本。通过在 Windows 服务中托管新的 WCF 服务,您可以轻松地调用它的方法并将逻辑从 OnCustomCommand 方法的覆盖转移到 ScriptExecutorServiceExecuteScript 方法。

如果我的模型假设有误,我会更新我的答案。

关于wcf - 可以从 WCF 与 Windows 服务通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com