1 Tips & Tricks: Using Tips & Tricks: Using System.Net To Write Better System.Net To Write Better Connected Applications Connected Applications Durgaprasad Gorti Durgaprasad Gorti COML02 COML02 Test Lead Test Lead Microsoft Corporation Microsoft Corporation
20
Embed
1 Tips & Tricks: Using System.Net To Write Better Connected Applications Durgaprasad Gorti COML02 Test Lead Microsoft Corporation.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Tips & Tricks: Using System.Net To Tips & Tricks: Using System.Net To Write Better Connected ApplicationsWrite Better Connected Applications
Sending Email with embedded Sending Email with embedded objectsobjects
Encryption over SocketsEncryption over Sockets
3
TracingTracingHow can I debug my System.Net How can I debug my System.Net app?app?How can I see what’s going on the How can I see what’s going on the wire?wire?Before .NET Framework 2.0Before .NET Framework 2.0
Which process Which process issued request?issued request?
Which thread issued Which thread issued this request?this request?
What What about about SSL?SSL?
What What about about loop loop
back?back?
4
TracingTracingHow can I debug my System.Net How can I debug my System.Net app?app?How can I see what’s going on the How can I see what’s going on the wire?wire?
With System.Net With System.Net TracingTracing
App1App1 App 2App 2
<Configuration<Configuration>>
</</Configuration>Configuration>
……GET GET
http://...http://...……
Log fileLog file
<Configuration<Configuration>>
</</Configuration>Configuration>
……POST POST
http://...http://...……
Log fileLog file
Per processPer process
Shows thread IDsShows thread IDs
No recompile for No recompile for appapp
Works for loop Works for loop backback
Shows SSL trafficShows SSL traffic
5
Using System.Net TracingUsing System.Net Tracing
6
Port ExhaustionPort Exhaustion
I see SocketException: Only one I see SocketException: Only one usage of each socket address usage of each socket address (protocol/network address/port) is (protocol/network address/port) is normally permitted. How can I fix normally permitted. How can I fix this?this?
ScenariosScenariosRepeated authenticated web service Repeated authenticated web service calls to the same servercalls to the same server
Authenticated/Unauthenticated calls with Authenticated/Unauthenticated calls with KeepAlive=falseKeepAlive=false
{protocol, local IP, local port, remote IP, remote port}{protocol, local IP, local port, remote IP, remote port}enters TIME_WAIT state for 4 minutes by default enters TIME_WAIT state for 4 minutes by default ON ACTIVE CLOSEON ACTIVE CLOSE
Req.ServicePoint.BindIPEndPointDelegate Req.ServicePoint.BindIPEndPointDelegate = new= new B BindIPEndPoint(BindIPEndPointCallback);indIPEndPoint(BindIPEndPointCallback);
public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, IPEndPoint remoteEndPoint, int retryCount) int retryCount)
{{ int port = Interlocked.Increment(ref m_LastBindPortUsed);int port = Interlocked.Increment(ref m_LastBindPortUsed); //increment//increment IInterlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534);nterlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534); if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) {{
return new IPEndPoint(IPAddress.Any,port);return new IPEndPoint(IPAddress.Any,port); }} elseelse {{
return new IPEndPoint(IPAddress.IPv6Any,port);return new IPEndPoint(IPAddress.IPv6Any,port); }}}}
10
Send/Receive – EMailSend/Receive – EMail
How do I use embedded objects in How do I use embedded objects in my email?my email?
11
SMTP Mail SMTP Mail
12
Send/Receive – Encryption Over Send/Receive – Encryption Over SocketsSockets
I use sockets. How can I authenticate I use sockets. How can I authenticate and/or encrypt data over sockets? and/or encrypt data over sockets?
Use msdn forums for questions and Use msdn forums for questions and commentscomments
http://forums.microsoft.com/msdnhttp://forums.microsoft.com/msdnAll of my team hangs out on that forum so All of my team hangs out on that forum so that is your best bet for System.Net that is your best bet for System.Net questionsquestions