1 ' Copyright (c) Microsoft Corporation. All Rights Reserved.
4 Imports System
.Collections
.Generic
8 Imports System
.ServiceModel
9 Imports System
.ServiceModel
.Channels
10 Imports System
.Runtime
.Serialization
11 Imports Microsoft
.VisualBasic
13 Namespace Microsoft
.ServiceModel
.Samples
15 <ServiceBehavior(InstanceContextMode
:=InstanceContextMode
.[Single], AddressFilterMode
:=AddressFilterMode
.Prefix
)> _
17 Implements IUniversalContract
19 'For the purposes of this sample, we're storing
20 'state inside of an instance variable on the service
21 'and using InstanceContextMode.Single.
22 Private customerList
As Dictionary(Of
String, Customer
)
23 Private nextId
As Integer
27 customerList
= New Dictionary(Of
String, Customer
)()
29 Dim customer
As New Customer()
31 customer
.Address
= "100 Main Street"
32 customerList
.Add("1", customer
)
38 #Region
"IUniversalContract Members"
40 Public Function ProcessMessage(ByVal request
As Message
) As Message Implements IUniversalContract
.ProcessMessage
42 Dim response
As Message
= Nothing
44 'The HTTP Method (e.g. GET) from the incoming HTTP request
45 'can be found on the HttpRequestMessageProperty. The MessageProperty
46 'is added by the HTTP Transport when the message is received.
47 Dim requestProperties
As HttpRequestMessageProperty
= DirectCast(request
.Properties(HttpRequestMessageProperty
.Name
), HttpRequestMessageProperty
)
49 'Here we dispatch to different internal implementation methods
50 'based on the incoming HTTP verb.
51 If requestProperties IsNot
Nothing Then
53 If [String].Equals("GET", requestProperties
.Method
, StringComparison
.OrdinalIgnoreCase
) Then
55 response
= GetCustomer(request
)
57 ElseIf [String].Equals("PUT", requestProperties
.Method
, StringComparison
.OrdinalIgnoreCase
) Then
59 response
= UpdateCustomer(request
)
61 ElseIf [String].Equals("POST", requestProperties
.Method
, StringComparison
.OrdinalIgnoreCase
) Then
63 response
= AddCustomer(request
)
65 ElseIf [String].Equals("DELETE", requestProperties
.Method
, StringComparison
.OrdinalIgnoreCase
) Then
67 response
= DeleteCustomer(request
)
71 'This service doesn't implement handlers for other HTTP verbs (such as HEAD), so we
72 'construct a response message and use the HttpResponseMessageProperty to
73 'set the HTTP status code to 405 (Method Not Allowed) which indicates the client
74 'used an HTTP verb not supported by the server.
75 response
= Message
.CreateMessage(MessageVersion
.None
, [String].Empty
, [String].Empty
)
77 Dim responseProperty
As New HttpResponseMessageProperty()
78 responseProperty
.StatusCode
= HttpStatusCode
.MethodNotAllowed
80 response
.Properties
.Add(HttpResponseMessageProperty
.Name
, responseProperty
)
86 Throw
New InvalidOperationException("This service requires the HTTP transport")
96 Private Function GetCustomer(ByVal msg
As Message
) As Message
98 Dim response
As Message
= Nothing
100 Dim requestProperties
As HttpRequestMessageProperty
= TryCast(msg
.Properties(HttpRequestMessageProperty
.Name
), HttpRequestMessageProperty
)
101 Dim responseProperties
As New HttpResponseMessageProperty()
103 Dim customer
As Customer
= Nothing
104 Console
.WriteLine("Received GET for " & msg
.Properties
.Via
.ToString())
106 Dim endpointUri
As Uri
= OperationContext
.Current
.EndpointDispatcher
.EndpointAddress
.Uri
107 Dim id
As String = CustomerIdFromRequestUri(msg
.Properties
.Via
, endpointUri
)
109 If id IsNot
Nothing Then
111 customerList
.TryGetValue(id
, customer
)
115 'No customer was specified, so return the contents of the collection as links
116 Dim links
As New List(Of Uri
)()
118 For Each customerId
As String In Me.customerList
.Keys
120 links
.Add(New Uri(msg
.Properties
.Via
.ToString() & customerId
))
124 responseProperties
.StatusCode
= HttpStatusCode
.OK
125 response
= Message
.CreateMessage(msg
.Version
, msg
.Headers
.Action
, links
)
126 response
.Properties(HttpResponseMessageProperty
.Name
) = responseProperties
131 If customer Is
Nothing Then
133 responseProperties
.StatusCode
= HttpStatusCode
.NotFound
134 response
= Message
.CreateMessage(msg
.Version
, msg
.Headers
.Action
, [String].Empty
)
138 responseProperties
.StatusCode
= HttpStatusCode
.OK
139 response
= Message
.CreateMessage(msg
.Version
, msg
.Headers
.Action
, customer
)
143 response
.Properties(HttpResponseMessageProperty
.Name
) = responseProperties
149 Private Function UpdateCustomer(ByVal msg
As Message
) As Message
151 Dim response
As Message
= Nothing
153 Dim requestProperties
As HttpRequestMessageProperty
= TryCast(msg
.Properties(HttpRequestMessageProperty
.Name
), HttpRequestMessageProperty
)
154 Dim responseProperties
As New HttpResponseMessageProperty()
156 Dim endpointUri
As Uri
= OperationContext
.Current
.EndpointDispatcher
.EndpointAddress
.Uri
157 Dim id
As String = CustomerIdFromRequestUri(msg
.Properties
.Via
, endpointUri
)
159 Console
.WriteLine("Received " + requestProperties
.Method
+ " for Customer.")
160 Dim customer
As Customer
= msg
.GetBody(Of Customer
)()
161 Console
.WriteLine(vbTab
& "Customer Data - Name: " & customer
.Name
& " Address: " & customer
.Address
)
163 If customer IsNot
Nothing Then
165 If Not customerList
.ContainsKey(id
) Then
167 responseProperties
.StatusCode
= HttpStatusCode
.NotFound
170 customerList(id
) = customer
171 responseProperties
.StatusCode
= HttpStatusCode
.OK
177 responseProperties
.StatusCode
= HttpStatusCode
.BadRequest
180 responseProperties
.SuppressEntityBody
= True
181 response
= Message
.CreateMessage(msg
.Version
, msg
.Headers
.Action
, [String].Empty
)
182 response
.Properties(HttpResponseMessageProperty
.Name
) = responseProperties
188 Private Function AddCustomer(ByVal msg
As Message
) As Message
190 Dim response
As Message
= Nothing
191 Dim requestProperties
As HttpRequestMessageProperty
= TryCast(msg
.Properties(HttpRequestMessageProperty
.Name
), HttpRequestMessageProperty
)
193 Dim responseProperties
As New HttpResponseMessageProperty()
195 Console
.WriteLine("Received " & requestProperties
.Method
& " for Customer.")
196 Dim customer
As Customer
= msg
.GetBody(Of Customer
)()
198 Console
.WriteLine(vbTab
& "Customer Data - Name: " & customer
.Name
& " Address: " & customer
.Address
)
199 If customer IsNot
Nothing Then
202 Me.customerList(nextId
.ToString()) = customer
204 Dim collectionUri
As Uri
= msg
.Properties
.Via
206 responseProperties
.StatusCode
= HttpStatusCode
.Created
207 responseProperties
.Headers(HttpResponseHeader
.Location
) = New Uri(collectionUri
, Me.nextId
.ToString()).ToString()
211 responseProperties
.StatusCode
= HttpStatusCode
.BadRequest
215 responseProperties
.SuppressEntityBody
= True
216 response
= Message
.CreateMessage(msg
.Version
, msg
.Headers
.Action
, [String].Empty
)
217 response
.Properties(HttpResponseMessageProperty
.Name
) = responseProperties
223 Private Function DeleteCustomer(ByVal msg
As Message
) As Message
225 Dim response
As Message
= Nothing
226 Dim requestProperties
As HttpRequestMessageProperty
= TryCast(msg
.Properties(HttpRequestMessageProperty
.Name
), HttpRequestMessageProperty
)
228 Dim responseProperties
As New HttpResponseMessageProperty()
230 Console
.WriteLine("Received DELETE for Customer")
232 Dim endpointUri
As Uri
= OperationContext
.Current
.EndpointDispatcher
.EndpointAddress
.Uri
233 Dim id
As String = CustomerIdFromRequestUri(msg
.Properties
.Via
, endpointUri
)
235 If customerList
.ContainsKey(id
) Then
237 customerList
.Remove(id
)
238 responseProperties
.StatusCode
= HttpStatusCode
.OK
242 responseProperties
.StatusCode
= HttpStatusCode
.NotFound
246 responseProperties
.SuppressEntityBody
= True
247 response
= Message
.CreateMessage(msg
.Version
, msg
.Headers
.Action
, [String].Empty
)
248 response
.Properties(HttpResponseMessageProperty
.Name
) = responseProperties
254 Private Function CustomerIdFromRequestUri(ByVal via
As Uri
, ByVal endpoint
As Uri
) As String
256 Dim customerNameSegmentIndex
As Integer = endpoint
.Segments
.Length
258 If customerNameSegmentIndex
< via
.Segments
.Length
Then
259 Return via
.Segments(customerNameSegmentIndex
)