Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of WCF Stream Operation restriction

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "case analysis of WCF Stream operation restrictions". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "case analysis of WCF Stream operation restrictions".

WCF supports operations on Stream objects, especially for messages whose size is too large. If you want to consider the efficiency of message delivery, WCF recommends operating through Stream. However, WCF imposes some restrictions on Stream operations, and we need to pay special attention to when we write relevant programs:

1. Restrictions on binding

If you need to use Stream operations, the only bindings you can use are BasicHttpBinding,NetTcpBinding and NetNamedPipeBinding. In addition, Reliable Messaging cannot be used when using WCF Stream operations. This approach is not advisable if message security is taken into account.

2. Restrictions on Stream objects

To be a message object passed by a service operation, such an object must be serializable. Unfortunately, the definition of the FileStream class does not support serialization, and we can use Stream objects, including Stream,MemoryStream, and so on. Using Stream class objects is the * of most WCF Stream operations. An interesting phenomenon is the conversion between FileStream and Stream types. For example, in the operation of a service contract, there is the following implementation:

Public Stream TransferDocument (Document document) {FileStream stream = new FileStream (document.LocalPath, FileMode.Open, FileAccess.Read); return stream;}

Note that the return type of the operation TransferDocument () is Stream, while in the implementation of the method, the returned object is of type FileStream. Because the Stream class is the parent of the FileStream class, there is no problem with such an implementation.

However, when the client invokes the operation, you cannot assign the return value of the operation to an object of type FileStream, as follows:

FileStream stream = m_service.TransferDocument (doc)

The Stream object obtained at this point is null. Therefore, we can only invoke the operation like this:

Stream stream = m_service.TransferDocument (doc)

However, another strange problem is that WCF does not support the serialization of the Length property of the Stream object, that is, we cannot use the Length property of the Stream object returned by the service operation on the client side. Calls such as stream.Length throw an exception.

Thank you for your reading, the above is the content of "case analysis of WCF Stream operation restrictions". After the study of this article, I believe you have a deeper understanding of the analysis of WCF Stream operation restrictions, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report