wtorek, 29 kwietnia 2008

System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

Error:
Unable to connect to the remote server.
System.Net.Sockets.SocketException: Only one usage of each socket address
(protocol/network address/port) is normally permitted.

Scenario:
1. I had a web service proxy, generated automatically from the CrmServiceWsdl.wsdl file of the Microsoft Dynamics CRM 4.0.
2. I was loading a big amount of data to the CRM via the web services.
3. In the middle of the load I was gettingthe following error:
Unable to connect to the remote server
System.Net.Sockets.SocketException: Only one usage of each socket address
(protocol/network address/port) is normally permitted.

Reason:
Every time I was invoking a method of the web service, the proxy generated a new connection even tough I was using the default setting keep-alive. The new connection was being prepared because of the need to authenticate each request. The old connections were hanging on (240 seconds) before timing out. It can be seen with the netstat –n command. There were so many connections that it was not possible to a make a new connection.

Solution:
I simply turned off the keep-alive for the web service proxy. I had the Reference.cs file generated automatically and I did not want to change this file. I prepared a new file with the partial class implementation and the overridden GetWebRequest method.

Sample code of the partial class:
namespace Crm40
{
using System.Diagnostics;
using System.Web.Services;
using System.ComponentModel;
using System.Web.Services.Protocols;
using System;
using System.Xml.Serialization;

public partial class CrmService :
System.Web.Services.Protocols.SoapHttpClientProtocol
{
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest webRequest =
(System.Net.HttpWebRequest)base.GetWebRequest(uri);

webRequest.KeepAlive = false;

webRequest.ProtocolVersion = System.Net.HttpVersion.Version10;
return webRequest;
}
}
}


However I think I had already met the same issue a long time ago, I resolved it once again ;)

Brak komentarzy: