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.
精彩评论