20
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

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

Embed Size (px)

Citation preview

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

1

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

Durgaprasad GortiDurgaprasad GortiCOML02COML02Test LeadTest LeadMicrosoft CorporationMicrosoft Corporation

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

2

AgendaAgenda

System.Net TracingSystem.Net Tracing

Port ExhaustionPort Exhaustion

Sending Email with embedded Sending Email with embedded objectsobjects

Encryption over SocketsEncryption over Sockets

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

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?

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

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

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

5

Using System.Net TracingUsing System.Net Tracing

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

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

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

7

Port exhaustionPort exhaustion

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

8

Port ExhaustionPort Exhaustion

RecommendationsRecommendationsHKLM\System\CurrentControlSet\HKLM\System\CurrentControlSet\Services\Tcpip\ParametersServices\Tcpip\Parameters

MaxUserPort - Dynamic Port range MaxUserPort - Dynamic Port range Default 5000Default 5000

Max Value 65534Max Value 65534

Set the MaxUserPort to a higher value than 5000Set the MaxUserPort to a higher value than 5000

TCPTimedWaitDelay - How long a connection TCPTimedWaitDelay - How long a connection remains in TIME_WAIT stateremains in TIME_WAIT state

Default 240 secondsDefault 240 seconds

Range: 30-240 SecondsRange: 30-240 Seconds

You can set this to as low as 30 secondsYou can set this to as low as 30 seconds

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

9

Port ExhaustionPort Exhaustion

RecommendationsRecommendationsServicePoint.BindIPEndPointDelegateServicePoint.BindIPEndPointDelegate

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

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

10

Send/Receive – EMailSend/Receive – EMail

How do I use embedded objects in How do I use embedded objects in my email?my email?

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

11

SMTP Mail SMTP Mail

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

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?

RecommendationsRecommendationsNegotiateStreamNegotiateStream

Uses windows auth Uses windows auth

SSLStreamSSLStreamUses Certificates Uses Certificates

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

13

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?

socketsocket socketsocket

NetworkNetworkstreamstream

NetworkNetworkstreamstream

clientclient serverserver

““1234-5678-0000-1234-5678-0000-1234”1234”

““1234-5678-0000-1234-5678-0000-1234”1234”

““1234-5678-0000-1234-5678-0000-1234”1234”

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

14

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?

socketsocket socketsocket

NetworkNetworkstreamstream

NetworkNetworkstreamstream

clientclient serverserver

““&*@a1&*@a1!”!”

NegotiaNegotiatete

/SSL /SSL streamstream

AuthenticateAsClient AuthenticateAsClient

““1234-5678-0000-1234-5678-0000-1234”1234”

““1234-5678-0000-1234-5678-0000-1234”1234”NegotiaNegotia

tete/SSL /SSL

streamstream AuthenticateAsServerAuthenticateAsServer

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

15

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?

Stream AppStream = null;Stream AppStream = null;TcpClient client = new TcpClient(TcpClient client = new TcpClient(<server><server>, ,

<port><port>););NetworkStream networkStream = NetworkStream networkStream = client.GetStream();client.GetStream();sstring s = "Hello From Client";tring s = "Hello From Client";byte[] bytes = Encoding.ASCII.GetBytes(s);byte[] bytes = Encoding.ASCII.GetBytes(s);networkStream.Write(bytes, 0, networkStream.Write(bytes, 0, bytes.Length);bytes.Length);

Stream AppStream = null;Stream AppStream = null;TcpClient client = new TcpClient(TcpClient client = new TcpClient(<server><server>, ,

<port><port>););NetworkStream networkStream = NetworkStream networkStream =

client.GetStream();client.GetStream();

NegotiateStream ns = newNegotiateStream ns = new NegotiateStream(networkStream)NegotiateStream(networkStream);;ns.AuthenticateAsClient();ns.AuthenticateAsClient();

sstring s = "Hello From Client";tring s = "Hello From Client";byte[] bytes = Encoding.ASCII.GetBytes(s);byte[] bytes = Encoding.ASCII.GetBytes(s);

nsns.Write(bytes, 0, bytes.Length);.Write(bytes, 0, bytes.Length);

UnauthenticatedUnauthenticated Authenticated!Authenticated!

CLIENTCLIENT

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

16

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?

TcpListener Server = new TcpListener(TcpListener Server = new TcpListener(<IP>, <IP>, <Port>)<Port>);;

Server.Start();Server.Start();TcpClient client = TcpClient client = Server.AcceptTcpClient();Server.AcceptTcpClient();NetworkStream networkStream = NetworkStream networkStream =

client.GetStream();client.GetStream();bybyte[] bytes = new byte[256];te[] bytes = new byte[256];int read = networkStream.Read(bytes, 0, int read = networkStream.Read(bytes, 0,

bytes.Length);bytes.Length);

Stream AppStream = null;Stream AppStream = null;TcpClient client = new TcpClient client = new TcpClient(TcpClient(serverserver,,portport););NetworkStream networkStream = NetworkStream networkStream =

client.GetStream();client.GetStream();

NegotiateStream ns = new NegotiateStream ns = new NegotiateStream(networkStream);NegotiateStream(networkStream);ns.AuthenticateAsServer();ns.AuthenticateAsServer();string client = string client = ns.RemoteIdentity.Name;ns.RemoteIdentity.Name;byte[] bytes = new byte[256];byte[] bytes = new byte[256];

int read = int read = nsns.Read(bytes, 0, bytes.Length);.Read(bytes, 0, bytes.Length);

UnauthenticatedUnauthenticated Authenticated!Authenticated!

ServerServer

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

17

Call to ActionCall to Action

Use System.Net 2.0 and take Use System.Net 2.0 and take advantage of the new featuresadvantage of the new features

SMTP, FTP, Caching, SSL/Negotiate SMTP, FTP, Caching, SSL/Negotiate StreamStream

Provide feedbackProvide [email protected]@microsoft.com

[email protected]@microsoft.com

[email protected]@microsoft.com

New feature asksNew feature asks

[email protected]@microsoft.com

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

18

Community ResourcesCommunity Resources

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

BlogsBlogshttp://blogs.msdn.com/dgortihttp://blogs.msdn.com/dgorti

http://blogs.msdn.com/malarchhttp://blogs.msdn.com/malarch

http://blogs.msdn.com/mahjayarhttp://blogs.msdn.com/mahjayar

http://blogs.msdn.com/joncolehttp://blogs.msdn.com/joncole

http://blogs.msdn.com/mflaskohttp://blogs.msdn.com/mflasko

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

19

Questions?Questions?

[email protected]@microsoft.com

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

20

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