200 likes | 323 Views
Tips & Tricks: Using System.Net To Write Better Connected Applications. Durgaprasad Gorti COML02 Test Lead Microsoft Corporation. Agenda. System.Net Tracing Port Exhaustion Sending Email with embedded objects Encryption over Sockets. Before .NET Framework 2.0. Tracing.
E N D
Tips & Tricks: Using System.Net To Write Better Connected Applications Durgaprasad Gorti COML02 Test Lead Microsoft Corporation
Agenda • System.Net Tracing • Port Exhaustion • Sending Email with embedded objects • Encryption over Sockets
Before .NET Framework 2.0 Tracing • How can I debug my System.Net app?How can I see what’s going on the wire? What about SSL? Which process issued request? Which thread issued this request? What about loop back?
With System.Net Tracing Tracing • How can I debug my System.Net app?How can I see what’s going on the wire? Per process Shows thread IDs No recompile for app Works for loop back Shows SSL traffic App 2 App1 <Configuration> </Configuration> <Configuration> </Configuration> …POST http://...… …GET http://...… Log file Log file
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
Port Exhaustion • Recommendations • HKLM\System\CurrentControlSet\Services\Tcpip\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
Port Exhaustion • Recommendations • ServicePoint.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 Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534); if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork){ return new IPEndPoint(IPAddress.Any,port); } else{ return new IPEndPoint(IPAddress.IPv6Any,port); } }
Send/Receive – EMail • How do I use embedded objects in my email?
Send/Receive – Encryption Over Sockets • I use sockets. How can I authenticate and/or encrypt data over sockets? • Recommendations • NegotiateStream • Uses windows auth • SSLStream • Uses Certificates
Send/Receive – Encryption Over Sockets • I use sockets. How can I authenticate and/or encrypt data over sockets? server client “1234-5678-0000-1234” “1234-5678-0000-1234” Networkstream Networkstream socket socket “1234-5678-0000-1234”
Send/Receive – Encryption Over Sockets • I use sockets. How can I authenticate and/or encrypt data over sockets? client server “1234-5678-0000-1234” “1234-5678-0000-1234” AuthenticateAsClient Negotiate/SSL stream Negotiate/SSL stream AuthenticateAsServer Networkstream Networkstream “&*@a1!” socket socket
Send/Receive – Encryption Over Sockets • I use sockets. How can I authenticate and/or encrypt data over sockets? CLIENT Authenticated! Unauthenticated Stream AppStream = null; TcpClient client = new TcpClient(<server>, <port>); NetworkStream networkStream = client.GetStream(); NegotiateStream ns = newNegotiateStream(networkStream); ns.AuthenticateAsClient(); string s = "Hello From Client"; byte[] bytes = Encoding.ASCII.GetBytes(s); ns.Write(bytes, 0, bytes.Length); Stream AppStream = null; TcpClient client = new TcpClient(<server>, <port>); NetworkStream networkStream = client.GetStream(); string s = "Hello From Client"; byte[] bytes = Encoding.ASCII.GetBytes(s); networkStream.Write(bytes, 0, bytes.Length);
Send/Receive – Encryption Over Sockets • I use sockets. How can I authenticate and/or encrypt data over sockets? Server Unauthenticated Authenticated! Stream AppStream = null; TcpClient client = new TcpClient(server,port); NetworkStream networkStream = client.GetStream(); NegotiateStream ns = new NegotiateStream(networkStream); ns.AuthenticateAsServer();string client = ns.RemoteIdentity.Name; byte[] bytes = new byte[256]; int read = ns.Read(bytes, 0, bytes.Length); TcpListener Server = new TcpListener(<IP>, <Port>); Server.Start();TcpClient client = Server.AcceptTcpClient(); NetworkStream networkStream = client.GetStream();byte[] bytes = new byte[256]; int read = networkStream.Read(bytes, 0, bytes.Length);
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
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
Questions? dgorti@microsoft.com
© 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.