Download presentation
Presentation is loading. Please wait.
Published byVictor Holt Modified over 9 years ago
1
1 Tips & Tricks: Using System.Net To Write Better Connected Applications Durgaprasad Gorti COML02 Test Lead Microsoft Corporation
2
2 Agenda System.Net Tracing Port Exhaustion Sending Email with embedded objects Encryption over Sockets
3
3 Tracing How can I debug my System.Net app? How can I see what’s going on the wire? Before.NET Framework 2.0 Which process issued request? Which thread issued this request? What about SSL? What about loop back?
4
4 Tracing How can I debug my System.Net app? How can I see what’s going on the wire? With System.Net Tracing App1 App 2 <Configuration></Configuration> … GET http://... … Log file <Configuration></Configuration> … POST http://... … Log file Per process Shows thread IDs No recompile for app Works for loop back Shows SSL traffic
5
5 Using System.Net Tracing
6
6 Port Exhaustion I see SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted. How can I fix this? Scenarios Repeated authenticated web service calls to the same server Authenticated/Unauthenticated calls with KeepAlive=false {protocol, local IP, local port, remote IP, remote port} enters TIME_WAIT state for 4 minutes by default ON ACTIVE CLOSE
7
7 Port exhaustion
8
8 Port Exhaustion Recommendations HKLM\System\CurrentControlSet\Services\Tc pip\Parameters MaxUserPort - Dynamic Port range Default 5000 Max Value 65534 Set the MaxUserPort to a higher value than 5000 TCPTimedWaitDelay - How long a connection remains in TIME_WAIT state Default 240 seconds Range: 30-240 Seconds You can set this to as low as 30 seconds
9
9 Port Exhaustion RecommendationsServicePoint.BindIPEndPointDelegate Req.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback); public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) { int port = Interlocked.Increment(ref m_LastBindPortUsed);//increment int port = Interlocked.Increment(ref m_LastBindPortUsed);//increment Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534); Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534); if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) { if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) { return new IPEndPoint(IPAddress.Any,port); } else { else { return new IPEndPoint(IPAddress.IPv6Any,port); }}
10
10 Send/Receive – EMail How do I use embedded objects in my email?
11
11 SMTP Mail
12
12 Send/Receive – Encryption Over Sockets I use sockets. How can I authenticate and/or encrypt data over sockets? RecommendationsNegotiateStream Uses windows auth SSLStream Uses Certificates
13
13 Send/Receive – Encryption Over Sockets I use sockets. How can I authenticate and/or encrypt data over sockets? socket socket Network stream client server “1234-5678-0000-1234”
14
14 Send/Receive – Encryption Over Sockets I use sockets. How can I authenticate and/or encrypt data over sockets? socket socket Network stream client server “&*@a1!” Negotiat e /SSL stream AuthenticateAsClient “1234-5678-0000-1234” Negotiat e /SSL stream AuthenticateAsServer
15
15 Send/Receive – Encryption Over Sockets I use sockets. How can I authenticate and/or encrypt data over sockets? Stream AppStream = null; TcpClient client = new TcpClient(, ); NetworkStream networkStream = client.GetStream(); string s = "Hello From Client"; byte[] bytes = Encoding.ASCII.GetBytes(s); networkStream.Write(bytes, 0, bytes.Length); Stream AppStream = null; TcpClient client = new TcpClient(, ); NetworkStream networkStream = client.GetStream(); NegotiateStream ns = new NegotiateStream(networkStream ); ns.AuthenticateAsClient(); string s = "Hello From Client"; byte[] bytes = Encoding.ASCII.GetBytes(s); ns.Write(bytes, 0, bytes.Length); Unauthenticated Authenticated! CLIENT
16
16 Send/Receive – Encryption Over Sockets I use sockets. How can I authenticate and/or encrypt data over sockets? TcpListener Server = new TcpListener(, ); Server.Start(); TcpClient client = Server.AcceptTcpClient(); NetworkStream networkStream = client.GetStream(); byte[] bytes = new byte[256]; int read = networkStream.Read(bytes, 0, bytes.Length); Stream AppStream = null; TcpClient client = new TcpClient(server,port); NetworkStream networkStream = client.GetStream(); NegotiateStream ns = new NegotiateStream(networkS tream); ns.AuthenticateAsServer(); string client = ns.RemoteIdentity.Name; byte[] bytes = new byte[256]; int read = ns.Read(bytes, 0, bytes.Length); Unauthenticated Authenticated! Server
17
17 Call to Action Use System.Net 2.0 and take advantage of the new features SMTP, FTP, Caching, SSL/Negotiate Stream Provide feedback dgorti@microsoft.com chadmu@microsoft.com mflasko@microsoft.com New feature asks nclasks@microsoft.com
18
18 Community Resources Use msdn forums for questions and comments http://forums.microsoft.com/msdn All of my team hangs out on that forum so that is your best bet for System.Net questions Blogs http://blogs.msdn.com/dgorti http://blogs.msdn.com/malarch http://blogs.msdn.com/mahjayar http://blogs.msdn.com/joncole http://blogs.msdn.com/mflasko
19
19 Questions? dgorti@microsoft.com
20
20 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.