gpt4 book ai didi

c# - 为什么在使用 .NET 远程处理时只能从 ASP.NET Web 应用程序读取属性而不能设置属性?

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:44 25 4
gpt4 key购买 nike

我在尝试为 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 网络应用程序中,这是我初始化所有内容的方式:

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;
}

有没有人知道为什么它不保存该属性?

提前致谢!

最佳答案

我会猜测因为List<T>不继承自 MarshalByRefObject ,当您调用属性时 remoteAlertList.TheList你得到一个断开连接的对象。也许改为向对象添加索引器:

public class RemoteLister : MarshalByRefObject
{
public Alert this[int index] {get {...} set {...}}
}

实际上,我主要会说放弃远程处理并使用 WCF/SOA。 RemoteLister 没有什么用处|正在[Serializable] .您可能还想考虑一下线程安全。


澄清; Alert实例是独立的,因此本地更新不会影响服务器;基本上,您有两种情况:

  • 如果类型是MarshalByRefObject ,那么它只存在于服务器上;所有客户端操作都远程到实际对象 - 但 MarshalByRefObjecf类型
  • 否则,对象被序列化并重建一个实际对象。

如果你有一个索引器(例如)

obj[index].Name = "abc";

那么这是:

var tmp = obj[index]; // tmp points to the deserialized disconnected Alert
tmp.Name = "abc"; // we update the local disconnected Alert

(如果 AlertMarshalByRefObject ,这更新服务器,但不要那样做)。但是,如果我们将值推回:

obj[index] = tmp;

然后我们更新了服务器。

正如您所发现的,以操作为中心的设计可能要简单得多(即 setAlertName )。但我真的认为远程处理在这里不是一个好主意。

关于c# - 为什么在使用 .NET 远程处理时只能从 ASP.NET Web 应用程序读取属性而不能设置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1662796/

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