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

时间:2010-10-21 17:22:41

标签: wcf object reference null weboperationcontext

当我尝试在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。以下是示例。有人可以对此有所了解吗?

我需要的是是一种透明的方式(或者对现有代码的改变方式),以使现有代码基于令牌执行指定的工作。这是使用的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

提前谢谢。

0 个答案:

没有答案