如何将会话变量从ASPX页面传递到WCF服务
问题描述:
我想将会话变量从我的asp.net网站传递给WCF服务,你能帮我吗,我已经从服务端进行了操作,但无法从aspx页面传递值.
你能帮我一下吗.真的很紧急.
WCF服务代码
Hi,
i want to pass session variable from my asp.net website to WCF service can you help me i have doen from service side but not able to pass value from aspx page.Can u help me plzzz. Its really very urgent.
Code of WCF Service
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
//bool boolValue = true;
//string stringValue = "Hello ";
public string Greet(string words)
{
return GreetInternal(words);
}
public string Greet2(string words)
{
return GreetInternal(words);
}
string GreetInternal(string words)
{
MySessionState state = (MySessionState)HttpContext.Current.Session["MySessionState"];
if (state == null) {
state = new MySessionState();
HttpContext.Current.Session["MySessionState"] = state;
}
int counter = state.Touch();
return string.Format("[Session ''{0}''] You said: {1} (counter: {2})", HttpContext.Current.Session.SessionID, words, counter);
}
}
class MySessionState
{
int counter;
public MySessionState() { }
public int Counter { get { return this.counter; } }
public int Touch() { return Interlocked.Increment(ref this.counter); }
}
给OP的注意事项:请不要使用文字说话.另外,这可能对您来说很紧急,但对我们而言并非如此.因此,尝试在发布时避免这种情况.
Note to OP: Please do not use text speak. Also, it may be urgent for you, but not for us. So try avoiding that while posting.
答
共享会话时要考虑三件事:
1. aspNetCompatibilityEnabled = true
2. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
3. allowCookies = true
我认为这足以让您继续操作.
顺便说一句,我个人不喜欢那种设计.我会使用以下方法:
您的会话对象应签订数据合同或消息合同,然后再传递给服务.恕我直言,这是一种更清洁,更分离的处理方式.
There are three things to consider while sharing sessions:
1. aspNetCompatibilityEnabled = true
2.[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
3. allowCookies = true
I think that''s enough hint for you to proceed.
BTW, I personally do not like kind of design. I would have used following approach:
Your session object should made a data contract or a message contract and then passed on to service. That IMHO is cleaner and decoupled way of doing things.