WebOperationContext 对象引用未设置为对象的实例

发布于 2024-09-28 08:50:55 字数 1887 浏览 1 评论 0原文

当我尝试在 WCF 项目中使用 WebOperationContext.Current 时,Current 为 null。下面是示例。有人可以解释一下吗?

WebForm - default.aspx:

    ServiceClient sc = new ServiceClient();

    Response.Write(sc.DoWork(1) + "<br />");

    WebOperationContext c = WebOperationContext.Current;  --Current is null

//WCF接口

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet]
    int DoWork(int num);
}

//WCF实现

public class Service : IService
{
    public int DoWork(int num)
    {           
        return num;
    }
}

系统设置: ASP.NET 3.5

预先感谢您。


我的问题的描述更改为以下内容:

当我尝试在 WCF 项目中使用 WebOperationContext.Current 时,Current 为 null。下面是示例。有人可以解释一下吗?

我需要是一种透明的方式(或者说对现有代码改动很小的方式)让现有的代码能够基于 token 来完成指定的工作。这就是这里使用 HttpModule 的原因,如下所示:

//HttpModule: 插入一个令牌,pipneline 中的工作可以基于该令牌。如前所述,HttpModule 用于对现有代码进行最少的更改。

public class ImageIntercept : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {

        WebOperationContext.Current.IncomingRequest.Headers.Add("image", "true");  //a token on which works will be based

    }

    public void Dispose()
    { }

}

//WCF Interface

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet]
    int DoWork(int num);
}

//WCF Implementation

public class Service : IService
{
    public int DoWork(int num)
    {
        string isImage = WebOperationContext.Current.IncomingRequest.Headers["image"];
        if(isImage == "true")
        {
            //this is what I need to do something here
        }

        return num;
    }
}

系统设置:ASP.NET 3.5

预先感谢您。

When I try to use WebOperationContext.Current in a WCF project, the Current is null. Below is the example. Could anyone please shed a light on it?

WebForm - default.aspx:

    ServiceClient sc = new ServiceClient();

    Response.Write(sc.DoWork(1) + "<br />");

    WebOperationContext c = WebOperationContext.Current;  --Current is null

//WCF Interface

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet]
    int DoWork(int num);
}

//WCF Implementation

public class Service : IService
{
    public int DoWork(int num)
    {           
        return num;
    }
}

System Settings:
ASP.NET 3.5

Thank you in advance.


The description of my question is changed to below:

When I try to use WebOperationContext.Current in a WCF project, the Current is null. Below is the example. Could anyone please shed a light on it?

What I need is a transparent way (or a way that changes as little to the existing code) to make the existing code to do a specified work based on a token. This is whay HttpModule is used here, show below:

//HttpModule: Insert a token on which works in the pipneline can be based on. As mentioned, HttpModule is used to make minimum changes to existing code.

public class ImageIntercept : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {

        WebOperationContext.Current.IncomingRequest.Headers.Add("image", "true");  //a token on which works will be based

    }

    public void Dispose()
    { }

}

//WCF Interface

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet]
    int DoWork(int num);
}

//WCF Implementation

public class Service : IService
{
    public int DoWork(int num)
    {
        string isImage = WebOperationContext.Current.IncomingRequest.Headers["image"];
        if(isImage == "true")
        {
            //this is what I need to do something here
        }

        return num;
    }
}

System Settings: ASP.NET 3.5

Thank you in advance.

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

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

发布评论

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