简体   繁体   English

WebService中的ViewState?这可能吗?

[英]ViewState in WebService? Is this possible?

according to my research, it isn't possible... unless i write one big hack (erk) to get around it... im wondering if it is possible or not as some of you may have more information on this. 根据我的研究,这是不可能的...除非我写一个大黑客(erk)来解决它...我想知道是否可能,因为你们中的一些人可能有更多的信息。 i understand this would go against the page paradigm, however it really should be accessible... any ideas anyone? 我明白这会违背页面范例,但它真的应该是可访问的...任何人的想法?

hate using cookies as the information is updated and reflects the real values in the collection after a full roundtrip (so always a 1 round trip lag)... im just storing an array really... session can be used in webservices but really dont want to load server up too much, although its probably only half a kb... maybe im just too paranoid? 讨厌使用cookies作为信息更新并反映完整往返后的集合中的真实价值(所以总是1往返延迟)...我只是真正存储一个数组...会话可以在webservices中使用,但真的不想加载服务器太多,虽然它可能只有半个kb ...也许我只是太偏执了?

any advice will be appreciated on if its worth the trouble of not using session state, im currently using cookies, would prefer to use viewstate, thanks. 任何建议将受到赞赏,如果它值得不使用会话状态的麻烦,我目前使用cookie,宁愿使用viewstate,谢谢。

Why not use the application cache instead? 为什么不使用应用程序缓存呢? It works great for this purpose. 它非常适用于此目的。

public static void AddToCache(string key, Object value, int slidingMinutesToExpire)
{
        if (slidingMinutesToExpire == 0)
        {
            HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }
        else
        {
            HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(slidingMinutesToExpire), System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }
    }

No, you can't use ViewState with web services. 不,您不能将ViewState与Web服务一起使用。 ViewState requires a hidden <input> field and an HTTP postback. ViewState需要隐藏的<input>字段和HTTP回发。 Input fields aren't supported with web services. Web服务不支持输入字段。

Since ViewState requires encoding data and sending it to the client and back again, it's no more efficient than cookies, which are supported by web services since they're implemented at the protocol level, rather than as part of the HTML like ViewState. 由于ViewState需要对数据进行编码并将其发送到客户端并再次返回,因此它不会比由Web服务支持的cookie更有效,因为它们是在协议级别实现的,而不是像ViewState那样的HTML的一部分。

Alternatively, you can use Session state, and keep the info on the server. 或者,您可以使用会话状态,并在服务器上保留信息。

只是在会话对象中创建一个缓存对象数组:)缓存的.dispose也应该从内存中释放会话:)

No, Viewstate is coupled tightly with the use of the browser as an HTTP client. 不,Viewstate与使用浏览器作为HTTP客户端紧密相关。

For webservices, you have two choices: let the client track the state of the conversation, or let the server track it. 对于webservices,您有两种选择:让客户端跟踪对话状态,或让服务器跟踪它。

  • Using the server session state, and passing a cookie (either HTTP cookie or some cookie-like parameter inside the SOAP envelope) 使用服务器会话状态,并传递cookie(HTTP cookie或SOAP信封内的一些类似cookie的参数)

  • requiring the client to track, retain, and possibly transmit to the server, the conversation state. 要求客户端跟踪,保留并可能向服务器发送会话状态。


About Viewstate - it is state for the page as presented to the user, and as an implementation, it is tightly coupled to the browser. 关于Viewstate - 它是呈现给用户的页面状态,作为实现,它与浏览器紧密耦合。 When the page is displayed, viewstate info is used to fill the page. 显示页面时,使用视图状态信息填充页面。 Later, when a form on the page is posted, the relevant form data, some of which may have been preset with the viewstate magic, is then transmitted to the server. 稍后,当页面上的表单被发布时,相关的表单数据(其中一些可能已经使用viewstate magic预设)然后被发送到服务器。 The server needs to validate the input from the client, despite the use of viewstate on the client side. 尽管在客户端使用了viewstate,服务器仍需要验证来自客户端的输入。 You can see that viewstate coupled with some lightweight browser-side population logic is a way for the client to manage the state of the page the user is seeing, but the server cannot forgo validation for conversation state. 您可以看到viewstate与一些轻量级浏览器端填充逻辑相结合,是客户端管理用户所看到的页面状态的一种方式,但服务器无法放弃对话状态的验证。

This approach can be taken in a web services application, but because there is no dependence on the browser or on a particular presentation (or any presentation at all), it is a do-it-yourself thing. 这种方法可以在Web服务应用程序中使用,但由于不依赖于浏览器或特定的表示(或任何演示文稿),因此它是一种自己动手的事情。 The client application maintains and uses any conversational state in a way that is appropriate for the client. 客户端应用程序以适合客户端的方式维护和使用任何会话状态。

On the other hand, Server-managed state means there is state information retained at the server for each "conversation" or "session". 另一方面,服务器管理状态意味着在服务器上为每个“会话”或“会话”保留状态信息。 The client doesn't necessarily need to track the information, if the server is doing it. 如果服务器正在执行此操作,则客户端不一定需要跟踪信息。 The client just presents a token (or cookie if you like) to the server and the server uses it as a lookup key into a state table. 客户端只是向服务器提供一个令牌(或cookie,如果你愿意),服务器将它用作状态表的查找键。 The server is primarily responsible for validating all state retained on behalf of the client. 服务器主要负责验证代表客户端保留的所有状态。

Since you're using .NET, you might be interested to learn that Workflow can be used server-side to track the state of a webservices (WCF) based conversation . 由于您使用的是.NET,因此您可能有兴趣了解Workflow可以在服务器端用于跟踪基于Web服务(WCF)的对话的状态 This approach maintains the WS network protocols - it does not stipulate any particular client technology or platform. 这种方法维护着WS网络协议 - 它没有规定任何特定的客户端技术或平台。

You can't use ViewState (or may be can after lot of unnecessary hard work:) in web service, but as alternative you can use session state. 您不能在Web服务中使用ViewState(或者可以在经过大量不必要的努力工作后使用:),但作为替代方案,您可以使用会话状态。 Session state is enabled for each WebMethod using EnableSession value: 使用EnableSession值为每个WebMethod启用会话状态:

[WebMethod(EnableSession=true)]
public int SessionHitCounter()
    {
    ...
    }

More info here at MSDN. 更多信息,请访问MSDN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM