【发布时间】:2009-11-02 18:12:18
【问题描述】:
我在尝试在 Windows 服务中托管的远程对象上设置属性时遇到问题。我正在尝试更改对象的属性,但由于某种原因它没有保存。
这里是服务的相关代码:
private static List<Alert> _alerts = new List<Alert>(); // List of the Alerts
private TcpChannel _tcpChannel;
protected override void OnStart(string[] args)
{
loadAlerts(); // This sets up the List (code not req'd)
// Set up the remotelister so that other processes can access _alerts
// Create the TcpChannel
_tcpChannel = new TcpChannel(65000);
ChannelServices.RegisterChannel(_tcpChannel, false);
// Register the Proxy class for remoting.
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteLister),
"AlertList.soap",
WellKnownObjectMode.Singleton);
}
[Serializable]
public class RemoteLister : MarshalByRefObject
{
public List<Alert> TheList
{
get { return _alerts; }
set { _alerts = value; }
}
public bool save()
{
EventLog.WriteEntry("AlertService", "Calling saveAlerts...");
return saveAlerts();
}
}
这是 Alert 类的代码(还有很多其他内容):
private string _alertName; // Name of alert
public string AlertName
{
get { return _alertName; }
set { _alertName = value; }
}
现在在我的 ASP.NET Web 应用程序中,我是这样初始化所有内容的:
AlertService.RemoteLister remoteAlertList;
protected void Page_Load(object sender, EventArgs e)
{
// This is where we create a local object that accesses the remote object in the service
Type requiredType = typeof(AlertService.RemoteLister);
// remoteAlertList is our reference to the List<Alert> in the always running service
remoteAlertList = (AlertService.RemoteLister)Activator.GetObject(requiredType,
"tcp://localhost:65000/AlertList.soap");
}
所以现在下面的代码可以工作了:
private void fillFields()
{
AlertNameTextBox.Text = remoteAlertList.TheList[AlertDropDownList.SelectedIndex].AlertName;
}
但是当我如下更改该属性时,它不起作用。
protected void AlertSaveButton_Click(object sender, EventArgs e)
{
remoteAlertList.TheList[AlertDropDownList.SelectedIndex].AlertName = AlertNameTextBox.Text;
}
有人知道它为什么不保存该属性吗?
提前致谢!
【问题讨论】:
-
我对你的 cmets 添加了一些进一步的解释
标签: c# asp.net remoting .net-remoting