首页 新闻 会员 周边 捐助

WCF的实例模式PerSession怎么使用呢

0
悬赏园豆:5 [待解决问题]

我有两个服务,一个是单实例一个是PerSession,

问题:是IService2实例模式PerSession每次执行GetData的GetHashCode都不一样,说明每次都实例化一次了

我的目的是:想让用户请求IService2服务时,如果2分钟一直不作任何操作(请求)了才注销实例,否则不注销实例,使用相同实例。如果用户使用IService1服务都是相同的实例,整个服务只实例一次,这步已经实现了

IService1是单实例模式

 [ServiceContract(Namespace = "http://www.artech.com/")]
    public interface IService1
    {
        [WebInvoke(Method = "*")]
        [OperationContract]
        string GetData();
}
   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
    public class Service1 : IService1
    {
      public string GetData()
        {
            return string.Format("You GetHashCode: {0}", this.GetHashCode());
        }
} 

 

IService2是PerSession

 [ServiceContract(SessionMode = SessionMode.Allowed)]  
    public interface IService2
    {
        [WebGet]
        [OperationContract]
        string GetData();
}
 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
    public class Service2 : IService2
    {
         public Service2()
         { 
         
         }
        public string GetData()
        {
            return string.Format("You GetHashCode: {0}", this.GetHashCode());
        }
}

 

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="WebRemoteMXWCF.Service2">
                <endpoint binding="webHttpBinding"  behaviorConfiguration="RestBehavior" contract="WebRemoteMXWCF.IService2"></endpoint>
            </service>
            <service name="WebRemoteMXWCF.Service1">
                <endpoint binding="webHttpBinding"  behaviorConfiguration="RestBehavior" contract="WebRemoteMXWCF.IService1"></endpoint>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
                    <serviceMetadata httpGetEnabled="true" />
                    <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="RestBehavior">
                    <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json"  />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <webHttpBinding>
                <binding  closeTimeout="00:01:00" maxReceivedMessageSize="2147483647"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                       writeEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                </binding>
            </webHttpBinding>
        </bindings>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
    </system.webServer>
    <system.web>
        <sessionState timeout="2"></sessionState>
        <httpRuntime maxRequestLength="2097151"/>
    </system.web>
</configuration>

以上是配置代码 ,帮我看一下是

ATATA的主页 ATATA | 初学一级 | 园豆:5
提问于:2016-03-23 17:09
< >
分享
所有回答(1)
0

你这样的需求是怎么来的?

原生wcf好像没法做到你的要求吧。

你要么在需要hashcode一致的对象外再包层,然后由那层对你这个对象做滑动过期的缓存。

获取hashcode的时候由缓存对象提供

Daniel Cai | 园豆:10424 (专家六级) | 2016-03-23 18:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册