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

What is the restriction operation of WCF Stream object

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

Share

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

This article focuses on "how to restrict the operation of WCF Stream objects", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to restrict the operation of WCF Stream objects.

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

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 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 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 a NotSupportedException exception.

3. Restrictions on TransferMode

To use the Stream operation, you must modify the TransferMode property of the binding. The default value for this property is Buffered. We should determine whether the value of TransferMode is Streamed, StreamedRequest, or StreamedResponse, respectively, based on the parameter type of the WCF Stream object in the operation.

4. Restrictions on MaxReceivedMessageSize

The default value of the MaxReceivedMessageSize property is 64kb, and once the passed Stream object exceeds the set value of the MaxReceivedMessageSize property, a CommunicationException exception occurs when the client manipulates the object. Therefore, we should set the value of MaxReceivedMessageSize according to our actual needs. The value of the MaxReceivedMessageSize attribute ranges from 1-9223372036854775807 (Int32.MaxValue). If the setting value is not within this range, it cannot be compiled. The programming method is set to:

5. Limitation of operation parameters

WCF Stream operation parameters are strictly limited, it only allows such operations to contain only one Stream object, here the so-called WCF Stream object, contains return objects, out and ref objects. In other words, the following operation definitions are incorrect:

Void Transfer (Stream S1, Stream S2); void Transfer (Stream S1, out Stream S2); void Transfer (Stream S1, ref Stream S2); Stream Transfer (Stream stream)

If such an operation is defined, a run-time error occurs.

In addition, because the transferred WCF Stream object is large and may take too long, it is recommended that you increase the value of the bound SendTimeout property. For example, set it to 10 minutes. The programming method is set to:

Binding.SendTimeout = TimeSpan.FromMinutes (10)

The configuration file is set as follows:

Note that the relevant settings for the binding must require the configuration of the server to be consistent with that of the client. * in practice, all settings are set through configuration files. For example, in my application, it is set like this:

At this point, I believe you have a deeper understanding of "WCF Stream object restriction operation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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