如何从WCF Web服务访问Application []集合

时间:2020-12-17 15:17:47

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.

我曾经使用过.ASMX网络服务,但我正在尝试转移到WCF,因为它是最新的东西,它应该会更好。

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.

无论如何,我想要做的事情真的很简单:创建一个通过调用Application.Clear()来清空Application集合的web服务。在ASMX Web服务中,这非常简单,因为.ASMX webservices可以完全访问Application []集合。但是,这在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 :

这是我的.svc文件:

<%@ 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.

一切都很好。但是,当我调用我的webservice时,FlushApplicationCache()会抛出NullReferenceException,因为Application []为null。

Is there any way to access the Application[] collection from a WCF webservice? Or do I need to go back to .ASMX?

有没有办法从WCF Web服务访问Application []集合?或者我需要回到.ASMX吗?

1 个解决方案

#1


0  

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.

即使WCF可以在与ASP.Net相同的AppDomain中运行,它也不会通过完整的ASP.Net管道。因此,HttpContext未设置,您无法访问该应用程序。 WCF服务已经与ASP.Net分离,期望被托管在Server 2008的WAS中。

#1


0  

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.

即使WCF可以在与ASP.Net相同的AppDomain中运行,它也不会通过完整的ASP.Net管道。因此,HttpContext未设置,您无法访问该应用程序。 WCF服务已经与ASP.Net分离,期望被托管在Server 2008的WAS中。