简体   繁体   English

无法将WCF配置为使用会话

[英]trouble configuring WCF to use session

I am having trouble in configuring WCF service to run in session mode. 我在配置WCF服务以在会话模式下运行时遇到麻烦。 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. 我正在将Net.TCP绑定与启用可靠会话标志的默认配置一起使用。 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. 但是,该服务按每次调用模式运行-每次我调用AddData时,都会在执行AddData之前调用构造函数,并在调用之后调用Dispose()。 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. 注意:我不知道它是否相关,但是我正在使用VS2008来运行它。

Update: I've noticed here that wcftestclient does not maintain session with clients - maybe it was my problem. 更新:我在这里注意到wcftestclient不能与客户保持会话-也许这是我的问题。 Indeed that was the problem. 确实,这就是问题所在。 Connecting to the service from simple console client confirmed that the service works as it should. 从简单控制台客户端连接到该服务,确认该服务可以正常工作。

Try requiring a SessionMode when defining the ServiceContract: 定义ServiceContract时,尝试要求使用SessionMode:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IService1
{
  [OperationContract]
  string AddData(int value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM