【发布时间】:2011-01-17 18:24:50
【问题描述】:
我在这篇文章的开头有一个警告。虽然我是一个相当称职的程序员(如果刚从大学毕业 4 年的人就是这种情况),但我对 Windows 编程还是很陌生。我正在使用 .NET C#(第 4 版)进行编程。
我正在尝试创建可以通过“客户端”配置实用程序(Windows 窗体应用程序)远程配置的 Windows 服务。我为 IPC 选择 WCF 的原因是因为我的理解是 WCF 是 Windows Remoting 的替代品,并且 Remoting 正在逐步淘汰。
我的问题是我不知道如何从配置实用程序连接到 Windows 服务时创建的 WCF 实例访问已实例化的对象。
也许这个非常简化的代码块会让您对我正在尝试做的事情有更多的了解。 (边走边编。希望没有令人尴尬的逻辑错误。)
class Blah
{
/*
* Create configuration object that is read
* by myApp and written to by myServ.
*/
ConfigManager confMan = new ConfigManager();
MyService myApp = new MyService(ref confMan);
Thread myAppT = new Thread(new ThreadStart(myApp.init));
myAppt.Start();
myAppT.Join();
//The confServ will handle all of the WCF related stuff
ConfigServer confServ = new ConfigServer(confMan);
Thread confServT = new Thread(new ThreadStart(confServ.init));
confServT.Start();
confServT.Join();
}
class ConfigManager
{
public bool someAttribute;
public ConfigManager()
{
}
public init()
{
someAttribute = false;
}
}
class MyService
{
ConfigManager confMan;
public MyService(ref ConfigManager confMan)
{
this.confMan = confMan;
}
public int init()
{
while(1==1)
{
if(confMan.someAttribute == true)
return 0;
}
}
}
class ConfServ
{
ConfigManager confMan;
public ConfServ(ref ConfigManager confMan)
{
this.confMan = confMan;
}
public void init()
{
//Do all the ServiceHost WCF stuff
}
}
[ServiceContract]
public interface IModConfig
{
[OperationContract]
void changeFalse();
void changeTrue();
}
/*
* How can I get my instance of confMan
* into this instance of ModConfig
*/
class ModConfig: IModConfig
{
public void changeFalse()
{
/*
* I want to change confMan.someAttribute
* to false here.
*/
}
public void changeTrue()
{
/*
* I want to change confMan.someAttribute
* to true here.
*/
}
}
基本上我只是希望能够写入被 MyService 实例引用的 ConfigManager 实例。
我想我已经清楚地定义了我的问题。如果我需要进一步阐述或改写我的帖子,请告诉我。
【问题讨论】:
-
我想过打开一个套接字,但这似乎违背了使用 IPC 框架的目的。
-
线程是怎么回事?将
Thread myAppt...myAppT.Join();替换为myApp.init();。confServer.init也一样,更容易阅读,更容易调试。
标签: .net wcf windows-services remoting