WCF REST Client Exception - c#

I have the following WCF client code:
string add = String.Format("http://localhost:{0}/ServiceRequestRest",accessPort);
var cf = new ChannelFactory<IServiceRequestRest>(new WebHttpBinding(), add);
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
busService = cf.CreateChannel();
busService.DoMyStuff("hello",null);
When I have "first chance exception break" activated, that last line of code throws a series of exceptions. The first exception is this:
System.InvalidOperationException occurred
Message="Envelope Version 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)' does not support adding Message Headers."
Source="System.ServiceModel"
StackTrace:
at System.ServiceModel.Channels.MessageHeaders.ValidateHeaderKind(HeaderKind headerKind)
I am not aware that I am trying to "add Message Headers", but I choose to continue execution and immediately get the next exception:
System.Net.WebException occurred
Message="The underlying connection was closed: The connection was closed unexpectedly."
Source="System"
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
With all that said, I'm having difficulty understanding what the problem is. Any clues?
Update: The return type of my REST call could not be serialized, as I learned from another site. That alone could cause this problem, but after I fixed it I still had the same problem. When I use a regular catch block for the exception, all I get is "the connection was closed unexpectedly." What else could I be missing?
Update2: I think I found the final problem. I was getting interference from Skype. The code is working now. :)

The main problem that I see is that you are trying to use a WCF Channel to communicate using WebHttpBinding.
IMHO the entire purpose of WebHttpBinding was so that the client did not have to use the WCF stack to communicate with it. You simply use any HTTP stack to make the requests.

Related

What would cause AmqpException in azure wcf relay?

I am logging the connection status events with the wcf relay, and I'm seeing something like this in the logs.
1/26 06:47:12 ERROR Service Bus ConnectionStatus: 'Reconnecting' Event. [(null)][42]
LastError: System.ServiceModel.CommunicationException: Exception of type 'System.ServiceModel.CommunicationException' was thrown. ---> Microsoft.ServiceBus.Messaging.Amqp.AmqpException: An AMQP error occurred (condition='amqp:unauthorized-access').
--- End of inner exception stack trace ---
This exception doesn't show up in the list on this microsoft page, and the only other post I can find anywhere related to this error message is here. However, that post does not have any recent comments or a resolution or workaround for the issue. Also, the exception doesn't have any stacktrace, so how am I supposed to troubleshoot this error?
I guess as a follow-up, I would ask whether this is anything to actually worry about if the wcf connection is never faulting.
Apparently, the token that the relay keeps refreshing to stay active requires the time on the server to match the azure service that it is connecting with, and if not, this type of error will show up. We were able to fix it by correcting the server time.

web services throwing errors intermittently

I have a web service running on server but once in a while it throws an error. This is happening when calling one particular method and all other method works fine but on calling this method I receive following error:-
An error occurred while receiving the HTTP response to
https://xxx/xxx/xxx.svc. This could be due to the service endpoint
binding not using the HTTP protocol. This could also be due to an HTTP
request context being aborted by the server (possibly due to the
service shutting down). See server logs for more details.
Currently it happened after running fine for 2 weeks. Strangely the method started working fine again after few hours. I am wondering where should I start looking to troubleshoot this issue? Any pointers to troubleshoot shall be helpful.
Just to add to this today I received below error:-
The underlying connection was closed: A connection that was expected
to be kept alive was closed by the server
Can both of the error be related to each other?
Thanks for the help!
I've also encountered that error.
I encountered that error when almost thousands of user simultaneously accessing the web service.
I put this piece of code..
ServiceClient = new ServiceClient ();
var customBinding = new CustomBinding(ServiceClient.Endpoint.Binding);
var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();
transportElement.KeepAliveEnabled = false;
ServiceClient.Endpoint.Binding = customBinding;
ServiceClient.Open();

suppressing WCF error

I have written a WCF service. The client is built up in Silverlight.
It works fine, but as soon as the internet connection is lost even for a second, my application throws an error in a message box "the remote server returned an error not found"
Also, it sometimes thorws WCF Request Timed Out exception. All these exceptions are displayed in a message box. I wanted to know if there is a way to suppress these exceptions, as-in I don't want a message box to be popped up everytime with these messages.
Please give me some leads.
Thanks
Not sure about your first issue.
The second issue can be solved by two ways: 1.set up client side binding timeout to longer time in web.config 2.write code to set up binding timeout. The following example sets up BasicHttpBinding at code behind.
BasicHttpBinding binding = new BasicHttpBinding();
binding.ReceiveTimeout = System.TimeSpan.Parse("00:10:00");
binding.SendTimeout = System.TimeSpan.Parse("00:10:00");
you have to catch the Exception in the Catch block and there you can keep the process for the ideal state for some time.
After some time again try to access the remote services. This will how you can handle this.

Reproduce "A connection that was expected to be kept alive was closed by the server."

We're using WebClient, .NET 3.5sp1 in a winforms application. For some users this results
in an Exception with the message:
"The underlying connection was closed: A connection that was expected to be kept alive was closed by the server."
Searching a bit around the web suggests a "fix" to just disable http keepalive, which we're not really interested in doing, some suggests it might be a bug in the .NET libraries, etc.
The error message suggests it's a keepaliv'ed http connection that somehow got closed by the server(or a proxy) without the underlyings of WebClient detecting it properly.
We're thinking of catching this specific case, and simply try the request again. However we cannot reproduce this exception. So.
How can we properly catch the case that yields the above error message.
catch(WebException ex) {
if (ex.Message == "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server") { ... }
smells.. bad.
Any tips on how we can reproduce the above exception ?
WebClient detects this just fine. Thus the exception. You need to find the server that's misbehaving. Not quite sure what to do if you find that server, maybe you can send the admin a nice email message.
Log the server URL.
I suggest you have a look at this blog by Misrosoft: Http Client Protocol Issues

Silent exception caught by IntelliTrace

Good afternoon,
I am running into a curious problem with WCF and IntelliTrace. I have an application that I'm testing using a locally-hosted WCF endpoint (the development server built into VS2010) using the basicHttpBinding. The application has been running normally: no exceptions are are making their way to the app and all of the WCF calls are returning data.
On a lark, I decided to take a look at the IntelliTrace output and noticed that my first call to WCF throws two exceptiosn:
Exception:Thrown: "No connection could be made because the target machine actively refused it" (System.Net.Sockets.SocketException)
A System.Net.Sockets.SocketException was thrown: "No connection could be made because the target machine actively refused it"
Exception:Caught: "No connection could be made because the target machine actively refused it" (System.Net.Sockets.SocketException)
A System.Net.Sockets.SocketException was caught: "No connection could be made because the target machine actively refused it"
I've reduced the application to a trivial use case:
ServiceClient client = new ServiceClient();
string[] output = client.LegacyCheck("username");
Console.WriteLine(output[0]);
Console.WriteLine(client.GetData(65));
And I get the same behavior. The second call has no exception associated with it.
I'm very puzzled. If the connection is being refused, then why does the exception not make it up to the application? And why would it success after 2 failed tries?
Any help is appreciated!
For what it's worth, I've noticed this behavior too with my IronPython/WPF applications. I've eventually realized that Intellitrace is simply showing you ALL of the exceptions that are raised and caught during normal operation, even if its part of a BCL or other library.
Of course, you only need to worry about unhandled exceptions (after they break your execution, you'll usually see those in IntelliTrace as a long chain of Thrown: Caught: Thrown: Caught: .... all the way down to Thrown: which will be the last line as the exception was not caught.
What I'm willing to bet is that the WCF code try's a couple of things first, catches the SocketExceptions, and then continues on its merry way. You wouldn't ever see this, but for IntelliTrace :)

Categories