1
1
using System ;
2
+ using System . Data . SqlClient ;
2
3
using System . IO ;
4
+ using System . Net . NetworkInformation ;
3
5
using System . Net . Security ;
4
6
using System . Net . Sockets ;
7
+ using System . Net . WebSockets ;
8
+ using System . Reflection ;
5
9
using System . Text ;
10
+ using System . Threading ;
6
11
7
12
namespace PublishRuntimeDependency
8
13
{
9
14
public class Program
10
15
{
11
16
public void Main ( string [ ] args )
12
17
{
13
- if ( args . Length < 1 )
14
- {
15
- args = new [ ] { "https://www.microsoft.com" } ;
16
- }
18
+ RunTest ( "System.Net.Security" , TestSslStream ) ;
19
+ RunTest ( "System.Net.NetworkInformation" , TestNetworkInformation ) ;
20
+ RunTest ( "System.Net.WebSockets.Client" , TestWebSockets ) ;
21
+ RunTest ( "System.Data.SqlClient" , TestSqlClient ) ;
22
+ RunTest ( "System.Reflection.DispatchProxy" , TestDispatchProxy ) ;
23
+ }
17
24
18
- Console . WriteLine ( "=== Testing with raw Stream ===" ) ;
19
- try
25
+ public interface ITestInterface
26
+ {
27
+ void DoTheThing ( ) ;
28
+ }
29
+ public class TestProxy : DispatchProxy
30
+ {
31
+ public bool WasCalled { get ; private set ; }
32
+
33
+ protected override object Invoke ( MethodInfo targetMethod , object [ ] args )
20
34
{
21
- TestStream ( args [ 0 ] ) ;
35
+ WasCalled = true ;
36
+ Console . WriteLine ( $ "Intercepted: { targetMethod . Name } with dispatch proxy") ;
37
+ return null ;
22
38
}
23
- catch ( Exception ex )
39
+ }
40
+
41
+ private void TestDispatchProxy ( )
42
+ {
43
+ var proxy = DispatchProxy . Create < ITestInterface , TestProxy > ( ) ;
44
+ proxy . DoTheThing ( ) ;
45
+
46
+ if ( ! ( ( TestProxy ) proxy ) . WasCalled )
24
47
{
25
- Console . WriteLine ( "FAILED:" ) ;
26
- Console . WriteLine ( ex . ToString ( ) ) ;
48
+ throw new Exception ( "The proxy was not called!" ) ;
27
49
}
50
+ Console . WriteLine ( "The proxy was called" ) ;
51
+ }
28
52
53
+ private void TestSqlClient ( )
54
+ {
55
+ // Don't want to take a dependency on Sql Server so we just force loading by constructing a type
56
+ var con = new SqlConnection ( ) ;
57
+ Console . WriteLine ( "Successfully loaded System.Data.SqlClient" ) ;
58
+ }
59
+
60
+ private void RunTest ( string name , Action act )
61
+ {
29
62
Console . WriteLine ( ) ;
30
- Console . WriteLine ( "=== Testing with StreamReader ===" ) ;
63
+ Console . WriteLine ( $ "=== Testing { name } ===") ;
31
64
try
32
65
{
33
- TestStreamReader ( args [ 0 ] ) ;
66
+ act ( ) ;
34
67
}
35
68
catch ( Exception ex )
36
69
{
@@ -39,33 +72,34 @@ public void Main(string[] args)
39
72
}
40
73
}
41
74
42
- public void TestStreamReader ( string urlStr )
75
+ private void TestWebSockets ( )
43
76
{
44
- // Make a simple HTTP request
45
- var url = new Uri ( urlStr ) ;
77
+ var socket = new ClientWebSocket ( ) ;
78
+ Console . WriteLine ( "Connecting" ) ;
79
+ socket . ConnectAsync ( new Uri ( "wss://echo.websocket.org" ) , CancellationToken . None ) . Wait ( ) ;
46
80
47
- var socket = new Socket ( SocketType . Stream , ProtocolType . Tcp ) ;
48
- socket . Connect ( url . Host , url . Port ) ;
81
+ Console . WriteLine ( "Sending" ) ;
82
+ socket . SendAsync ( new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( "Hello" ) ) , WebSocketMessageType . Text , true , CancellationToken . None ) . Wait ( ) ;
49
83
50
- using ( var stream = GetStream ( socket , url ) )
51
- {
52
- // Send the request
53
- var request = $ "GET { url . PathAndQuery } HTTP/1.1\r \n Host: { url . Host } \r \n Connection: close\r \n \r \n ";
54
- var buffer = Encoding . UTF8 . GetBytes ( request ) ;
55
- stream . Write ( buffer , 0 , buffer . Length ) ;
84
+ var buffer = new byte [ 1024 ] ;
85
+ Console . WriteLine ( "Receiving" ) ;
86
+ var result = socket . ReceiveAsync ( new ArraySegment < byte > ( buffer ) , CancellationToken . None ) . Result ;
56
87
57
- // Read the response
58
- using ( var reader = new StreamReader ( stream , Encoding . UTF8 ) )
59
- {
60
- Console . WriteLine ( reader . ReadToEnd ( ) ) ;
61
- }
88
+ Console . WriteLine ( $ "Recieved: { Encoding . UTF8 . GetString ( buffer , 0 , result . Count ) } ") ;
89
+ }
90
+
91
+ private void TestNetworkInformation ( )
92
+ {
93
+ foreach ( var iface in NetworkInterface . GetAllNetworkInterfaces ( ) )
94
+ {
95
+ Console . WriteLine ( $ "Network Interface: { iface . Id } { iface . Name } ({ iface . NetworkInterfaceType } )") ;
62
96
}
63
97
}
64
98
65
- public void TestStream ( string urlStr )
99
+ public void TestSslStream ( )
66
100
{
67
101
// Make a simple HTTP request
68
- var url = new Uri ( urlStr ) ;
102
+ var url = new Uri ( "https://www.microsoft.com" ) ;
69
103
70
104
var socket = new Socket ( SocketType . Stream , ProtocolType . Tcp ) ;
71
105
socket . Connect ( url . Host , url . Port ) ;
@@ -78,13 +112,9 @@ public void TestStream(string urlStr)
78
112
stream . Write ( buffer , 0 , buffer . Length ) ;
79
113
80
114
// Read the response
81
- byte [ ] readbuffer = new byte [ 1024 ] ;
82
- int read = 0 ;
83
- Console . WriteLine ( "Response:" ) ;
84
- while ( ( read = stream . Read ( readbuffer , 0 , readbuffer . Length ) ) != 0 )
115
+ using ( var reader = new StreamReader ( stream , Encoding . UTF8 ) )
85
116
{
86
- var converted = Encoding . UTF8 . GetString ( readbuffer , 0 , read ) ;
87
- Console . Write ( converted ) ;
117
+ Console . WriteLine ( reader . ReadToEnd ( ) ) ;
88
118
}
89
119
}
90
120
}
0 commit comments