Wcf多服务读取文件

发布于 2024-11-07 22:45:04 字数 1061 浏览 0 评论 0原文

我正在使用 wcf 来实现使用多个服务的分布式应用程序。

我的一项服务必须使用 MSMQ 绑定才能异步接收请求,因此在脱机时不会忽略请求。 MSMQ 绑定的问题在于它只是一种方式,因此我不能让方法在合约中返回。 所以我必须使用 wshttpbinding 创建另一个端点。

现在我有一个问题,它甚至可能与 wcf 服务无关。

具有 msmq 绑定的服务写入由其他服务读取的文件。问题是,当第一个服务对文件进行更改时,我必须重新启动这两个服务才能让第二个服务读取这些更改。

我正在使用以下命令写入文件中的信息:

string filename = "holder.txt";
if (!File.Exists(filename))
    File.Create(filename);
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, requests_list);
stream.Close();

我关闭了流,因此在读取该流时是否应该从其他服务中看到所做的更改?使用:

string filename = "holder.txt";
if (!File.Exists(filename))
    return null;
Requests requests_list;
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
try
{
    requests_list = (Requests)bFormatter.Deserialize(stream);
}
catch (Exception)
{
    stream.Close();
    return null;
}
stream.Close();
return requests_list;

顺便说一句,这两种方法都在两个服务共享的类中。

提前致谢

I'm using wcf to implement a distributed application using multiple services.

One of my services must use MSMQ binding in order to receive requests asynchronously, so the requests aren't ignored when it's offline. The problem with MSMQ binding is that it is one way only and therefore I can't have methods returning within the contract.
So I had to make another endpoint with a wshttpbinding.

Now I have a problem that it might even not bee related to wcf services.

The service with the msmq binding writes in a file which is read by the other service. The problem is that when the first service makes changes to the file I must restart both in order to the second one read those changes.

I'm writing the information from the file using the following:

string filename = "holder.txt";
if (!File.Exists(filename))
    File.Create(filename);
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, requests_list);
stream.Close();

I close the stream so shouldn't the changes made been seen from the other service when reading it? using:

string filename = "holder.txt";
if (!File.Exists(filename))
    return null;
Requests requests_list;
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
try
{
    requests_list = (Requests)bFormatter.Deserialize(stream);
}
catch (Exception)
{
    stream.Close();
    return null;
}
stream.Close();
return requests_list;

Btw both methods are in a class shared by both services.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文