added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Basic / Binding / WS / MTOM / VB / client / client.vb
blobf93354e543e314fbe43393c32bdb87a592a8e48a
1 ' Copyright (c) Microsoft Corporation. All Rights Reserved.
3 Imports System
4 Imports System.ServiceModel
5 Imports System.ServiceModel.Channels
6 Imports System.IO
8 Namespace Microsoft.ServiceModel.Samples
10 'The service contract is defined in generatedClient.vb, generated from the service by the svcutil tool.
12 'Client implementation code.
13 Class Client
15 Public Shared Sub Main()
17 Dim binaryData() As Byte = New Byte(999) {}
18 Dim stream As New MemoryStream(binaryData)
20 ' Upload a stream of 1000 bytes
21 Dim client As New UploadClient()
22 Console.WriteLine(client.Upload(stream))
23 Console.WriteLine()
24 stream.Close()
26 ' Compare the wire representations of messages with different payloads
27 CompareMessageSize(100)
28 CompareMessageSize(1000)
29 CompareMessageSize(10000)
30 CompareMessageSize(100000)
31 CompareMessageSize(1000000)
33 Console.WriteLine()
34 Console.WriteLine("Press <ENTER> to terminate client.")
35 Console.ReadLine()
37 End Sub
39 Private Shared Sub CompareMessageSize(ByVal dataSize As Integer)
41 ' Create and buffer a message with a binary payload
42 Dim binaryData() As Byte = New Byte(dataSize - 1) {}
43 Dim msg As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "action", binaryData)
44 Dim buffer As MessageBuffer = msg.CreateBufferedCopy(Integer.MaxValue)
46 ' Print the size of a text encoded copy
47 Dim size As Integer = SizeOfTextMessage(buffer.CreateMessage())
48 Console.WriteLine("Text encoding with a {0} byte payload: {1}", binaryData.Length, size)
50 ' Print the size of an MTOM encoded copy
51 size = SizeOfMtomMessage(buffer.CreateMessage())
52 Console.WriteLine("MTOM encoding with a {0} byte payload: {1}", binaryData.Length, size)
53 Console.WriteLine()
54 msg.Close()
56 End Sub
58 Private Shared Function SizeOfTextMessage(ByVal msg As Message) As Integer
60 ' Create a text encoder
61 Dim element As MessageEncodingBindingElement = New TextMessageEncodingBindingElement()
62 Dim factory As MessageEncoderFactory = element.CreateMessageEncoderFactory()
63 Dim encoder As MessageEncoder = factory.Encoder
65 ' Write the message and return its length
66 Dim stream As New MemoryStream()
67 encoder.WriteMessage(msg, stream)
68 Dim size As Integer = CInt(stream.Length)
70 msg.Close()
71 stream.Close()
72 Return size
74 End Function
76 Private Shared Function SizeOfMtomMessage(ByVal msg As Message) As Integer
78 ' Create an MTOM encoder
79 Dim element As MessageEncodingBindingElement = New MtomMessageEncodingBindingElement()
80 Dim factory As MessageEncoderFactory = element.CreateMessageEncoderFactory()
81 Dim encoder As MessageEncoder = factory.Encoder
83 ' Write the message and return its length
84 Dim stream As New MemoryStream()
85 encoder.WriteMessage(msg, stream)
87 Dim size As Integer = CInt(stream.Length)
88 stream.Close()
89 msg.Close()
90 Return size
92 End Function
94 End Class
96 End Namespace