-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
source.vb
93 lines (69 loc) · 2.54 KB
/
source.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'<snippet0>
Imports System.Collections.Generic
Imports System.ServiceModel
'<snippet0>
Imports System.ServiceModel.Description
Imports System.Security.Principal
Class Service
Shared Sub Main(ByVal args() As String)
Dim s As New Service()
s.UnsecuredHttp()
End Sub
Private Sub UnsecuredHttp()
'<snippet1>
Dim httpUri As New Uri("http://localhost/Calculator")
' Create the ServiceHost.
Dim myServiceHost As New ServiceHost(GetType(Calculator), httpUri)
' Create a binding that uses HTTP. By default,
' this binding has no security.
Dim b As New BasicHttpBinding()
' Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
AddMexEndpoint(myServiceHost)
myServiceHost.Open()
Console.Write("Listening....")
Console.ReadLine()
' Close the service when a key is pressed.
myServiceHost.Close()
'</snippet1>
End Sub
Private Sub UnsecuredTcp()
'<snippet2>
Dim tcpUri As New Uri("net.tcp://localhost:8008/Calculator")
' Create the ServiceHost.
Dim sh As New ServiceHost(GetType(Calculator), tcpUri)
' Create a binding that uses TCP and set the security mode to none.
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.None
' Add an endpoint to the service.
sh.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
sh.Open()
Dim listenUri As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening on: {0}", listenUri)
Console.Write("Press Enter to end the service")
Console.ReadLine()
' Close the service when a key is pressed.
'</snippet2>
End Sub
Private Sub AddMexEndpoint(ByRef sv As ServiceHost)
' Add an endpoint to return metadata.
Dim sb As New ServiceMetadataBehavior()
sb.HttpGetEnabled = True
sb.HttpGetUrl = New Uri("http://localhost:8008/metadata")
sv.Description.Behaviors.Add(sb)
End Sub
End Class
<ServiceContract()> _
Interface ICalculator
<OperationContract()> _
Function Add(ByVal a As Double, ByVal b As Double) As Double
End Interface
Public Class Calculator
Implements ICalculator
Public Function Add(ByVal a As Double, ByVal b As Double) As Double _
Implements ICalculator.Add
Return a + b
End Function
End Class