配置 WCF 使用会话时遇到问题

发布于 2024-09-05 11:38:09 字数 995 浏览 1 评论 0原文

我在配置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-09-12 11:38:09

尝试在定义 ServiceContract 时要求 SessionMode:

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

Try requiring a SessionMode when defining the ServiceContract:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IService1
{
  [OperationContract]
  string AddData(int value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文