0

I have created a web service using .Net 4.5 and RestSharp. The service will initially have two clients that will be consuming it: one that is also using .Net 4.5 and RestSharp and the other is stuck on .Net 2.0 and WebClient (since RestSharp doesn't support .Net 2.0) for the time-being.

I can successfully respond to calls from the .Net 4.5 client with the following action:

[HttpPost]
public IRestResponse<CustomMessageResponseType> Send([FromBody]string message)
{
    //action code here;
}

However, this results in message having a null value when called by the .Net 2.0 client. I was able to successfully respond to calls from the .Net 2.0 client by changing the action signature to (notice the change in type of the message parameter):

[HttpPost]
public IRestResponse<CustomMessageResponseType> Send([FromBody]CustomMessageType message)
{
    //action code here;
}

But this causes a null value for message when called by the .Net 4.5 client.

I have also tried having both actions included in the controller and relying on the signature to differentiate between them automatically but this resulted in neither being called.

In the .Net 2.0 client, I am calling the service like this:

private string Send(CustomMessageType message)
{
    var request = new WebClient { BaseAddress = API.ToString(), Encoding = Encoding.UTF8 };
    request.Headers[HttpRequestHeader.ContentType] = "application/json";
    request.Headers[HttpRequestHeader.Accept] = "application/json";
    var content = JsonConvert.SerializeObject(message);
    var response = request.UploadString("Message", content);
    return response;
}

and message contains something like the following:

{
    "Subject":"Test Message",
    "Body":"This is a test message.",
    "Recipients":
    [{
        "FirstName":"John",
        "LastName":"Doe",
        "Name":"John Doe",
        "RecipientID":"1",
        "ContactPoints":
        {
            "Email":"John.Doe@example.com",
            "Phone":null,
            "Text":null
        }
    }],
    "Sender":
    {
        "FirstName":"Test Account",
        "LastName":null,
        "Name":"Test Account",
        "RecipientID":null,
        "ContactPoints":
        {
            "Email":null,
            "Phone":"5555551234",
            "Text":null
        }
    }
}

Can anyone help me figure out how to serve both clients?

Jaquez
  • 920
  • 1
  • 13
  • 21
  • 1
    Can you give an example of the message body you are sending through? Also example code of how you are making the request in .Net2.0 – dbarnes Mar 24 '15 at 16:22
  • I just found [RestSharp-.NET-2.0-Fork](https://github.com/mcintyre321/RestSharp-.NET-2.0-Fork) which allows me to use the RestSharp in the .Net 2.0 client and it works with the string typed signature so the difference seems to come down to how each client library (WebClient vs. RestSharp) sends the data over the wire but I still can't pin down the difference. Should I mark this as the answer and then create a separate question to find the difference between the library network traffic? – Jaquez Mar 24 '15 at 18:52
  • No I think it's still relevant, have you looked at the payload going to the server, using something like fiddler? I'm betting in one case they are not setting the correct Content-Type(or other) headers which may be causing your problem. – dbarnes Mar 24 '15 at 23:12
  • That's exactly what I have been attempting. I have thus far been able to capture the traffic of the .Net 2.0 client in fiddler but not the .Net 4.5 client for some reason. – Jaquez Mar 25 '15 at 14:37
  • hmm that usually arrises when you point the server to localhost vs the actual IP/Name. Can you verify that is/is not the case? – dbarnes Mar 25 '15 at 18:57
  • While I can confirm that I am indeed sending the data to the remote test server (I can set breakpoints that are being hit during execution) I still have not been able to see the traffic from the .Net 4.5 client. Despite my best efforts to do so according to @EricLaw [here](http://stackoverflow.com/a/21193146/4708097), I now suspect this to be a problem with my `machine.config` setup. On the plus side, I was able to find the solution to the original problem which I will detail as an answer just in case anyone else runs into it. Thanks for all of your help and suggestions @dbarnes! – Jaquez Mar 26 '15 at 20:16

1 Answers1

0

The problem ended up being in my original code (isn't it always?). The JSON data being sent by the .Net 4.5 client was actually being double-escaped. I was serializing the object as JSON and then passing it to RestSharp's AddBody method which expects a deserialized object and so it was serializing it again, which amounted to escaping all of the escape characters. The upside is that the .Net 2.0 client no longer needs to make use of RestSharp-.NET-2.0-Fork and I can use the strongly-typed action parameter as I would greatly prefer.

Thanks again for all of your help in working through this @dbarnes. It made for a great first experience for a long-time reader, first-time inquisitor.

Jaquez
  • 920
  • 1
  • 13
  • 21