如何从 WCF Web 服务访问 Application[] 集合
我曾经使用 .ASMX Web 服务,但我正在尝试转向 WCF,因为它是最新的东西,而且应该会更好。
不管怎样,我想要做的事情真的很简单:创建一个 Web 服务,通过调用 Application.Clear() 清空 Application 集合。 在 ASMX Web 服务中,这真的非常简单,因为 .ASMX Web 服务可以完全访问 Application[] 集合。 然而,这在 WCF 中并不起作用。
所以,这是我的服务合同:
[ServiceContract]
public interface IFlusherServicePage
{
[OperationContract]
void FlushApplicationCache();
}
这是我的实现类:
public class FlusherServicePage : Page, IFlusherServicePage
{
public void FlushApplicationCache()
{
Application.Clear();
}
}
这是我的 .svc 文件:
<%@ ServiceHost Language="C#" Debug="true" Service="FlusherServicePage" CodeBehind="~/App_Code/FlusherServicePage.cs" %>
一切都编译良好。 但是,当我调用 Web 服务时,FlushApplicationCache() 会抛出 NullReferenceException,因为 Application[] 为 null。
有没有办法从 WCF Web 服务访问 Application[] 集合? 或者我需要返回到 .ASMX 吗?
I used to use .ASMX web services, but I'm trying to move to WCF because it's the latest newest thing, and it's supposed to be better.
Anyway, what I'm wanting to do is really really simple : create a webservice that empties the Application collection by calling Application.Clear(). In an ASMX web service, this is really really simple, because .ASMX webservices have full access to the Application[] collection. However, this doesn't really work in WCF.
So, here's my service contract :
[ServiceContract]
public interface IFlusherServicePage
{
[OperationContract]
void FlushApplicationCache();
}
Here's my implementing class :
public class FlusherServicePage : Page, IFlusherServicePage
{
public void FlushApplicationCache()
{
Application.Clear();
}
}
And here's my .svc file :
<%@ ServiceHost Language="C#" Debug="true" Service="FlusherServicePage" CodeBehind="~/App_Code/FlusherServicePage.cs" %>
Everything compiles fine. However, when I call my webservice, FlushApplicationCache() throws a NullReferenceException, because Application[] is null.
Is there any way to access the Application[] collection from a WCF webservice? Or do I need to go back to .ASMX?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管 WCF 可以在与 ASP.Net 相同的 AppDomain 中运行,但它不会经过完整的 ASP.Net 管道。 因此,HttpContext 未设置,您无法访问应用程序。 WCF 服务已从 ASP.Net 中分离出来,预计托管在 Server 2008 的 WAS 中。
Even though WCF can run in the same AppDomain as ASP.Net, it does not go through the full ASP.Net pipeline. As such, the HttpContext is not set and you cannot get to the application. WCF Services have been separated from ASP.Net in anticipation of being hosted in the WAS of Server 2008.