System.ServiceModel.CommunicationException: The un
Blog Date: 4/23/2008
| Recent Blogs |
<< Back |
|
|
| More... |
| |
IT Jobs Hiring |
|
|
Cisco Voice Operations Engineer
|
New York, NY
|
|
|
Sr. Software Engineer -- DSP Algorithms, Security Encryption, C/C++ (San Diego, CA )
|
Los Angeles Area, CA
|
|
|
Software Developers- PHP, Java, MySql, Perl (Marina Del Rey, CA)
|
Los Angeles Area, CA
|
|
|
|
|
System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. ---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
In my scenario, it had to do with the way the proxies were being generated.
FIRST OF ALL: Make sure all your Data Contracts are marked with attribute of [DataContract]. It must either have a parameterless public constructor defined, or none at all. All used members must have public getters / setters and marked with [DataMember].
SOLUTION 1: When inheriting from another data contract, you must mark the parent with a "knownType" of the child that is inheriting from it.
namespace SomeLayer
{
[DataContract]
[KnownType(typeof(SomeLayer.Child))]
public class Parent
{
... Prop_1 ... Prop_2 ...
}
[DataContract]
public class Child : Parent
{
... Prop_3 ... Prop_4 ...
}
}
DON'T FORGET to do this opn enumeration types also. And that includes every single enum value.
[DataContract]
public enum CategoryType
{
[EnumMember]
None = 0,
[EnumMember]
Some = 8,
[EnumMember]
All = 12,
}
SOLUTION 3: In some cases the first call to the webservice works just fine, but if in the following few minutes no new call to the webservice is made, the next call would throw the exception shown above. This problem could be solved by altering the generated proxy class; in the GetWebRequest function the KeepAlive property must be set to false. This can be accomplished by following these steps:
- Add a Web Reference using the normal way (if you haven't already added one ofcourse).
- Make sure Show All Files menu item is enable in the Project menu.
- In the Solution Explorer window, navigate to:
- Web References
- <Name of your webservice>
- Open the Reference.cs file and add following code in the webservice proxy class:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest webRequest =
(System.Net.HttpWebRequest) base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}
12/3/2008 7:38:06 AM