WCF管理会话状态信息状态、信息、WCF

2023-09-04 02:31:25 作者:得不到也忘不掉

什么是最scaleble方式在WCF管理状态。

我只需要一个变量来表示一个会议,我将在MSSQL管理相关信息的会话。我并不需要知道何时会结束。每天一次,我将只清除任何旧的会话。

看起来的SessionID是可变的。

有关我使用的每呼叫的构造函数是空的规模。我不认为我需要每个会话。

在我的简单EightBall的测试我得到了重新presents会话的会话ID。但我只是测试在一个机箱中。

我担心的是,我看到一些资料,我需要设置ReliableSessionBindingElement上,它是默认关闭。

时的SessionID将成为会议在以下配置的可靠指标?

 < system.serviceModel>
    <服务>
      <服务名称=MajicEightBallServiceLib.MagicEightBallService
               behaviorConfiguration =EightBallServiceMEXBehavior>
        <端点地址=
                  绑定=的wsHttpBinding
                  合同=MajicEightBallServiceLib.IEightBall/>
        <端点地址=MEX
                  绑定=mexHttpBinding
                  合同=IMetadataExchange接口/>
        <主机>
          < baseAddresses>
            <新增baseAddress =HTTP://本地主机:8000 / MagicEightBallService/>
          < / baseAddresses>
        < /主机>
      < /服务>
    < /服务>
    <行为>
      < serviceBehaviors>
        <行为NAME =EightBallServiceMEXBehavior>
          < serviceMetadata httpGetEnabled =真/>
        < /行为>
      < / serviceBehaviors>
    < /行为>
  < /system.serviceModel>

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)
公共类MagicEightBallService:IEightBall
{
    公共MagicEightBallService()
    {
        Console.WriteLine(Eightball等待你的问题......);
    }
    公共字符串ObtainAnswerToQuestion(字符串userQuestion)
    {
        返回也许+ OperationContext.Current.SessionId.ToString();
    }

    公共SDOC GetSdoc(INT SID)
    {
        名单< sDocProp>道具=新的名单,其中,sDocProp>();
        sDocProp为prop1 =新sDocProp {n = 1,名称=为prop1,ArrivalStatus = ArrivalStatus.OnTime};
        props.Add(为prop1);
        sDocPropStringSV prop2 =新sDocPropStringSV {n = 1,名称=为prop1,ArrivalStatus = ArrivalStatus.OnTime,值=StrValue1};
        props.Add(prop2);
        SDOC SDOC =新SDOC {SID = SID,sParID = 1,道具=道具,会话ID = OperationContext.Current.SessionId.ToString()};
        返回SDOC;
    }
 
在ASP.NET中使用web服务的会话状态的教程

解决方案

WCF支持4种类型的会话和可靠的会议只是其中之一。这样你就没有必要需要把设置ReliableSessionBindingElement在启用会话。您可以使用其他会话类型。

  

System.ServiceModel.Channels.ReliableSessionBindingElement ,   它实现了WS-ReliableMessaging规范,提供   可靠的会话的支持,其中邮件传递,以   而正是一次,使信心的消息在整个旅行即使   在通话过程中的多个节点。

实际上的wsHttpBinding 默认使用一个安全的会话进行加密和数字签名信息。

  

时的SessionID将是会议中的一个可靠的指标   下面的配置?

Service可以检查你的绑定配置为使用 ServiceContractAttribute.SessionMode 属性来使用会话。

下面的服务合同要求配置的绑定使用会话。

  [的ServiceContract(SessionMode = SessionMode.Required)
公共接口IEightBall
 

http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.sessionmode.aspx

  

什么是最scaleble方式在WCF管理状态。

InstanceContextMode.PerCall 是正确的选择,如果你的code不依赖于不同的会话ID的会话数据。会话ID是WCF消息的一部分,所以没有必要保持的InstanceContext 长于有必要处理请求。

另外一个问题:

  

时的SessionID将是会议中的一个可靠的指标   下面的配置?

答案是肯定的。 属性下面是逆向工程code System.ServiceModel.OperationContext.SessionId 。正如你可以看到的SessionID Channel.Session 加载,并且不为空的SessionID 返回值只有当会话不为空。

 公共字符串的SessionID
{
    得到
    {
        如果(this.channel!= NULL)
        {
            IChannel innerChannel = this.channel.InnerChannel;
            如果(innerChannel!= NULL)
            {
                ISessionChannel< IDuplexSession> sessionChannel = innerChannel为ISessionChannel< IDuplexSession取代;
                如果(sessionChannel = NULL和放大器;!&安培;!sessionChannel.Session = NULL)
                {
                    返回sessionChannel.Session.Id;
                }
                ISessionChannel< IInputSession> sessionChannel2 = innerChannel为ISessionChannel< IInputSession取代;
                如果(sessionChannel2 = NULL和放大器;!&安培;!sessionChannel2.Session = NULL)
                {
                    返回sessionChannel2.Session.Id;
                }
                ISessionChannel< IOutputSession> sessionChannel3 = innerChannel为ISessionChannel< IOutputSession取代;
                如果(sessionChannel3 = NULL和放大器;!&安培;!sessionChannel3.Session = NULL)
                {
                    返回sessionChannel3.Session.Id;
                }
            }
        }
        返回null;
    }
}
 

What is the most scaleble way to manage state in a WCF.

I just need a single variable to indicate a session and I will manage information relative to the session in a MSSQL. I don't need to know when a session ends. Once a day I will just clear out any old sessions.

It appears SessionID is that variable.

For scale I am using Per Call as the ctor is empty. I don't think I need per session.

In my simple EightBall test I am getting a SessionID that represents a session. But I am just testing on a single box.

What worries me is that I see some documentation that I need to set ReliableSessionBindingElement On and it is Off by default.

Is SessionID going to be a reliable indicator of session in the following configuration?

<system.serviceModel>
    <services>
      <service name="MajicEightBallServiceLib.MagicEightBallService"
               behaviorConfiguration="EightBallServiceMEXBehavior" >
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="MajicEightBallServiceLib.IEightBall" />
        <endpoint address="mex"
                  binding ="mexHttpBinding"
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/MagicEightBallService"/>
          </baseAddresses>
        </host>             
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="EightBallServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

[ServiceBehavior (InstanceContextMode=InstanceContextMode.PerCall)]
public class MagicEightBallService : IEightBall
{
    public MagicEightBallService()
    {
        Console.WriteLine("Eightball awaits your question ...");
    }
    public string ObtainAnswerToQuestion(string userQuestion)
    {
        return "maybe " + OperationContext.Current.SessionId.ToString();
    }

    public sDoc GetSdoc(int sID)
    {
        List<sDocProp> props = new List<sDocProp>();
        sDocProp prop1 = new sDocProp { ID = 1, Name = "Prop1", ArrivalStatus = ArrivalStatus.OnTime };
        props.Add(prop1);
        sDocPropStringSV prop2 = new sDocPropStringSV { ID = 1, Name = "Prop1", ArrivalStatus = ArrivalStatus.OnTime, Value = "StrValue1" };
        props.Add(prop2);
        sDoc sDoc = new sDoc { sID = sID, sParID = 1, Props = props, SessionID = OperationContext.Current.SessionId.ToString() };
        return sDoc;
    }

解决方案

WCF supports 4 types of sessions and reliable session is only one of them. So you do not necessary need to turn set ReliableSessionBindingElement On to enable session. You can use other session types.

The System.ServiceModel.Channels.ReliableSessionBindingElement, which implements the WS-ReliableMessaging specification, provides support for reliable sessions in which messages are delivered in order and exactly once, enabling confidence even when messages travel across multiple nodes during the conversation.

Actually wsHttpBinding by default uses a secure session which encrypts and digitally signs messages.

Is SessionID going to be a reliable indicator of session in the following configuration?

Service can check if your binding configured to use session using ServiceContractAttribute.SessionMode attribute.

The following service contract requires that configured bindings use sessions.

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IEightBall

http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.sessionmode.aspx

What is the most scaleble way to manage state in a WCF.

InstanceContextMode.PerCall is the right choice if your code does not depend on any session data except Session ID. Session ID is part of WCF message so there is no need to keep InstanceContext longer than it is necessary to process request.

Another question:

Is SessionID going to be a reliable indicator of session in the following configuration?

Answer is YES. Below is reverse-engineered code of property System.ServiceModel.OperationContext.SessionId. As you can see SessionId is loaded from Channel.Session and not empty SessionId value is returned only if Session is not null.

public string SessionId
{
    get
    {
        if (this.channel != null)
        {
            IChannel innerChannel = this.channel.InnerChannel;
            if (innerChannel != null)
            {
                ISessionChannel<IDuplexSession> sessionChannel = innerChannel as ISessionChannel<IDuplexSession>;
                if (sessionChannel != null && sessionChannel.Session != null)
                {
                    return sessionChannel.Session.Id;
                }
                ISessionChannel<IInputSession> sessionChannel2 = innerChannel as ISessionChannel<IInputSession>;
                if (sessionChannel2 != null && sessionChannel2.Session != null)
                {
                    return sessionChannel2.Session.Id;
                }
                ISessionChannel<IOutputSession> sessionChannel3 = innerChannel as ISessionChannel<IOutputSession>;
                if (sessionChannel3 != null && sessionChannel3.Session != null)
                {
                    return sessionChannel3.Session.Id;
                }
            }
        }
        return null;
    }
}