我有一个 Windows 服务,它使用 net.pipe 作为 WCF 服务器和一个 Windows 窗体客户端来连接它。有一个名为 ConfigSettings 的类具有我希望客户端查询的值。
我想让客户端读取服务使用的 serviceConfig 实例中的当前值。最终,我希望客户更改其中的值,但要先迈出第一步。
表单可以通过命名管道与服务器通信,但 'return serviceConfig 正在向客户端发送一个新的空实例。我想要服务正在积极使用的数据(即 serviceConfig.Setting1 = x; serviceConfig.Setting2 = "foo"; )
Windows 服务和 WCF 服务器代码是(更新到工作版本):
using System.IO;
using System.ServiceModel;
using System.ServiceProcess;
namespace WindowsServiceTest
{
public partial class Service1 : ServiceBase
{
internal static ServiceHost myServiceHost = null;
//this is the master config that the service uses
public static ConfigSettings serviceConfig = new ConfigSettings();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(WCFService1));
myServiceHost.Open();
//set active default settings
serviceConfig.Setting1 = 1;
serviceConfig.Setting2 = "initial.Setting2:" + serviceConfig.Setting1;
}
protected override void OnStop()
{
if (myServiceHost != null)
{
myServiceHost.Close();
myServiceHost = null;
}
}
}
public partial class WCFService1 : IService1
{
public ConfigSettings GetConfig()
{
return Service1.serviceConfig;
}
public void SetConfig(ConfigSettings sentConfig)
{
Service1.serviceConfig = sentConfig;
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
ConfigSettings GetConfig();
[OperationContract]
void SetConfig(ConfigSettings sentConfig);
}
public class ConfigSettings
{
public int Setting1 { get; set; }
public string Setting2 { get; set; }
public ConfigSettings() { }
}
}
客户端像这样检索配置(更新了一些更改):
using System;
using System.ServiceProcess;
using System.Windows.Forms;
using WindowsServiceTest;
namespace WindowsServiceTestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ConfigSettings config = new ConfigSettings();
//GetConfig()
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client myService = new ServiceReference1.Service1Client();
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "Service1");//this will grant permission to access the Service
//get the current config and display
config = myService.GetConfig();
MessageBox.Show(config.Setting1 + "\r\n" + config.Setting2, "config");
myService.Close();
}
//SetConfig(ConfigSettings)
private void button2_Click(object sender, EventArgs e)
{ //make changes to values
config.Setting1 += 1;
config.Setting2 = "new.Setting2:" + config.Setting1;
ServiceReference1.Service1Client myService = new ServiceReference1.Service1Client();
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "Service1");//this will grant permission to access the Service
//send the new config
myService.SetConfig(config);
myService.Close();
}
}
}
更新: 也许我认为需要做的是矫枉过正。看来我在 WCF 和 Windows 服务之间遇到了障碍。
你将如何解决这个问题?
- 需要表单进行配置的 Windows 服务。
- 当服务启动时,它会从磁盘加载一个 config.xml 文件。(一个序列化的类)
- 当 GUI 启动时,我想:
- 检索其当前配置,
- 对其进行一些修改,
- 将其推回服务,
- 触发服务重新读取并对新配置做出反应。
我试图避免静态/将配置文件写入磁盘并告诉服务再次重新读取它。它“似乎”像 WCF 是要走的路。
更新2 似乎只需将服务中的主配置更改为静态,WCF服务就可以直接访问它。我本可以发誓我在发帖之前就这样做了,但我想不是。
我还将 Service1 的命名与上面的 WCFService1 分开,但事实证明这并不重要,并且可以使用任何一种方式。
上面更新了新的完整代码。