none
wcf 开启 persession RRS feed

  • 问题

  •     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
        public class Service1 : IService1
        {
             int i;
            public string GetData()
            {
                return i.ToString();
            }
            public void Add()
            {
                i++;
            }
        }
    
        [ServiceContract(SessionMode = SessionMode.Allowed)]
        public interface IService1
        {
            [OperationContract]
            void Add();
    
            [OperationContract]
            string GetData();
        }
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      
      <system.web>
        <compilation debug="true" targetFramework="4.6.2" />
        <httpRuntime targetFramework="4.6.2"/>
      </system.web>
      
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    
      </system.serviceModel>
    
    
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
            在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>

    以上是wcf 服务端,以下是 mvc 调用 wcf

            ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
            public ActionResult MvcAdd()
            {
                sc.Add();
                sc.Add();
                sc.Add();
    
                return View();
            }
    
            public ActionResult MvcGetData()
            {
                ViewBag.Message =  sc.GetData ();
                return View();
            }

    请问:在MVC 客户端,执行 MvcAdd 后,在 wcf 的session 的生存期内 ,再 执行 MvcGetDat ,得数要等于3,要怎么设置?

    2018年4月30日 1:46

答案

  • Hi,

    对于你的问题,在WCF MVC里面都是有问题的,首先WCF BasicHttpBinding是不支持Session,你需要使用wsHttpBinding.

        <services>
          <service name="WCFSession.Service1">
            <endpoint address="" binding="wsHttpBinding" contract="WCFSession.IService1" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
    

    然后在WCF里面,sc在请求“MvcAdd 和“MvcGetData”,都会被实例化,也就不是WCF的同一个Session了,你可以在Add之后将值存在MVCSession里面。

            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
                sc.Add();
                sc.Add();
                sc.Add();
                Session["sc"] = sc.GetData();
                return View();
            }
    
            public ActionResult Contact()
            {
                //ViewBag.Message = "Your contact page.";
                ViewBag.Message = Session["sc"];
                return View();
            }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年4月30日 5:52
    版主

全部回复

  • Hi,

    对于你的问题,在WCF MVC里面都是有问题的,首先WCF BasicHttpBinding是不支持Session,你需要使用wsHttpBinding.

        <services>
          <service name="WCFSession.Service1">
            <endpoint address="" binding="wsHttpBinding" contract="WCFSession.IService1" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
    

    然后在WCF里面,sc在请求“MvcAdd 和“MvcGetData”,都会被实例化,也就不是WCF的同一个Session了,你可以在Add之后将值存在MVCSession里面。

            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
                sc.Add();
                sc.Add();
                sc.Add();
                Session["sc"] = sc.GetData();
                return View();
            }
    
            public ActionResult Contact()
            {
                //ViewBag.Message = "Your contact page.";
                ViewBag.Message = Session["sc"];
                return View();
            }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年4月30日 5:52
    版主
  • 感恩有您的指导!

    WCF里面,sc在请求“MvcAdd 和“MvcGetData”,都会被实例化,也就不是WCF的同一个Session了。

    用 Single 模式,好像是所有客户端的请求,wcf都保留在 同一个 session。

    那如果,同一个客户端 sc在请求“MvcAdd 和“MvcGetData,能不能 保留在 wcf 的 同一个 session?


    • 已编辑 码魂 2018年4月30日 23:14
    2018年4月30日 23:13
  • 你想怎么实现同一个客户端的?

    正如你说的,用单例模式,所有客户端请求用的是同一个sc,共享同一个WCF Session.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月1日 2:35
    版主
  • ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
            public ActionResult MvcAdd()
            {
                sc.Add();
                sc.Add();
                sc.Add();
    
                return View();
            }
    
            public ActionResult MvcGetData()
            {
                ViewBag.Message =  sc.GetData ();
                return View();
            }
    我的意思,在mvc端, 执行 MvcAdd() ,不存储 session,再执行 sc.GetData() ,能否获得数据3.
    2018年5月1日 8:01
  • 你怎么执行sc.GetData()?是通过调用MvcGetData()还是在sc.Add()之后加sc.DetData(),如果是前者,是得不到3的,如果是后者,可以得到3.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月1日 9:09
    版主