2012-06-12 76 views
1

在WCF中管理状态的最可扩展的方式是什么?WCF管理会话状态信息

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

它出现SessionID是那个变量。

对于规模我使用每呼叫作为ctor是空的。我认为我不需要每个会话。

在我简单的EightBall测试中,我得到了表示会话的SessionID。但我只是在一个盒子上测试。

让我担心的是,我看到一些需要设置ReliableSessionBindingElement的文档,默认情况下关闭。

在以下配置中SessionID是会话的可靠指标吗?

<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; 
    } 

回答

2

WCF支持4种类型的会话,可靠会话只是其中之一。因此,您不需要将Set ReliableSessionBindingElement设置为启用会话。您可以使用其他会话类型。

System.ServiceModel.Channels.ReliableSessionBindingElement, 它实现了WS-ReliableMessaging规范,提供了哪些消息是为了 交付,正好一次可靠的会话 支持,使信心,即使邮件的会话期间在 多个节点行进。

实际上wsHttpBinding默认情况下使用安全会话加密和数字签名消息。

SessionID将在配置中成为 中会话的可靠指示符吗?

服务可以检查您的绑定配置为使用会话使用ServiceContractAttribute.SessionMode属性。

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

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

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

什么是一个WCF管理国家最scaleble方式。

InstanceContextMode.PerCall是正确的选择,如果您的代码不依赖于会话ID以外的任何会话数据。会话ID是WCF消息的一部分,因此无需保持InstanceContext比处理请求所需的时间更长。

另一个问题:

是SessionID的将是会议的 以下配置的可靠指标?

答案是YES。 以下是房地产System.ServiceModel.OperationContext.SessionId的逆向工程代码。正如你所看到的,SessionIdChannel.Session加载而不是空的SessionId仅当Session不为空时才返回值。

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; 
    } 
} 
+0

很多很好的信息,但请在以下问题上明确。 “在以下配置中SessionID是否是会话的可靠指标?” – Paparazzi