配置 WCF 使用会话时遇到问题
我在配置 WCF 服务以在会话模式下运行时遇到问题。作为测试,我编写了这个简单的服务:
[ServiceContract]
public interface IService1
{
[OperationContract]
string AddData(int value);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
internal class Service1 : IService1,IDisposable
{
private int acc;
public Service1()
{
acc = 0;
}
public string AddData(int value)
{
acc += value;
return string.Format("Accumulator value: {0}", acc);
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
我使用默认配置的 Net.TCP 绑定,并启用了可靠会话标志。 据我了解,此类服务在会话模式下运行应该没有问题。 但是,服务以每次调用模式运行 - 每次我调用 AddData 时,都会在执行 AddData 之前调用构造函数,并在调用之后调用 Dispose() 。 任何想法为什么会发生这种情况?也许我错过了什么?
注意:我不知道它是否相关,但我正在使用VS2008来运行它。
更新:我注意到此处 wcftestclient 不与客户——也许这是我的问题。确实这就是问题所在。从简单的控制台客户端连接到服务确认该服务正常工作。
I am having trouble in configuring WCF service to run in session mode. As a test I wrote this simple service :
[ServiceContract]
public interface IService1
{
[OperationContract]
string AddData(int value);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
internal class Service1 : IService1,IDisposable
{
private int acc;
public Service1()
{
acc = 0;
}
public string AddData(int value)
{
acc += value;
return string.Format("Accumulator value: {0}", acc);
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
I am using Net.TCP binding with default configuration with reliable session flag enabled.
As far as I understand , such service should run with no problems in session mode.
But , the service runs as in per call mode - each time I call AddData , constructor gets called before executing AddData and Dispose() is called after the call.
Any ideas why this might be happening? Perhaps I am missing something?
note : I do not know if it is related , but I am using VS2008 to run this.
Update: I've noticed here that wcftestclient does not maintain session with clients - maybe it was my problem. Indeed that was the problem. Connecting to the service from simple console client confirmed that the service works as it should.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在定义 ServiceContract 时要求 SessionMode:
Try requiring a SessionMode when defining the ServiceContract: