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。下面是示例。有人可以解释一下吗?
我需要是一种透明的方式(或者说对现有代码改动很小的方式)让现有的代码能够基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论