WCF REST Client Exception

2019-07-27 11:06发布

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. :)

1条回答
The star\"
2楼-- · 2019-07-27 11:48

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.

查看更多
登录 后发表回答