1 / 20

Tips & Tricks: Using System.Net To Write Better Connected Applications

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.

shandi
Download Presentation

Tips & Tricks: Using System.Net To Write Better Connected Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tips & Tricks: Using System.Net To Write Better Connected Applications Durgaprasad Gorti COML02 Test Lead Microsoft Corporation

  2. Agenda • System.Net Tracing • Port Exhaustion • Sending Email with embedded objects • Encryption over Sockets

  3. 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?

  4. 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

  5. Using System.Net Tracing

  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. Port exhaustion

  8. 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

  9. 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); } }

  10. Send/Receive – EMail • How do I use embedded objects in my email?

  11. SMTP Mail

  12. 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

  13. 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”

  14. 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

  15. 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);

  16. 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);

  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. 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. Questions? dgorti@microsoft.com

  20. © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

More Related