0

I am trying to write a WCF service (A) that is in turn calling another service (B). This is not a problem. The problem is that B returns json, and this I want to return from A. Here is the code I have:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        WebRequest wr = WebRequest.Create("//url_to_B//");
        String username = "user";
        String password = "password";
        String encoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
        wr.Headers.Add("Authorization", "Basic " + encoded);
        Stream resStream = wr.GetResponse().GetResponseStream();
        StreamReader resReader = new StreamReader(resStream);
        String response = resReader.ReadToEnd();
        resReader.Close();
        resStream.Close();
        return response;
    }
}

and:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
    string GetData(int value);
}

Outputs as found in Fiddler:
B:

[{"id":"10103","key":"CAMMOT"}]

A:

"[{\"id\":\"10103\",\"key\":\"CAMMOT\"}]"

The returned value from A if called is a string with data that can be parsed to json. How would I go about returning json instead? Any help appreciated. Thanks.

Daniel Gruszczyk
  • 5,379
  • 8
  • 47
  • 86
  • Please show us the format of the string returned from `A` – Yuval Itzchakov May 14 '14 at 09:38
  • added example return values I can see in Fiddler :) – Daniel Gruszczyk May 14 '14 at 09:41
  • 1
    See http://stackoverflow.com/questions/2086666/how-do-i-return-clean-json-from-a-wcf-service maybe. – Jon Lindeheim May 14 '14 at 09:43
  • I was looking at that, but my problem is different. He has objects and can return a list of them. According to this I would have to parse the json to C# classes and then return these classes. Seems like too much work for a simple thing, provided the results I have shown you are like 1% of the returned object, they are huge, nested and complicated... – Daniel Gruszczyk May 14 '14 at 09:44
  • Wait, `A` returns a valid json. now, you want to return that same json from your call in `B`, but without the escaping inside the json? – Yuval Itzchakov May 14 '14 at 09:45
  • A returns a string that contains data that can be parsed to json. I want to return from A data exactly as it is returned from B, no extra escapes, quotes around it etc – Daniel Gruszczyk May 14 '14 at 09:47

2 Answers2

1

By returning a Stream, you can return a raw string:

public class Service1 : IService1
{
    public System.IO.Stream GetData(int value)
    {
        WebRequest wr = WebRequest.Create("//url_to_B//");
        String username = "user";
        String password = "password";
        String encoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
        wr.Headers.Add("Authorization", "Basic " + encoded);
        return wr.GetResponse().GetResponseStream();
    }
}
sroes
  • 14,663
  • 1
  • 53
  • 72
0

WCF binding will use jsonSerializer as MessageEncoder if you specify

 ResponseFormat = WebMessageFormat.Json

Then it will process string as part of Json object (a property) and encode the " as \" to avoid conflict with the json syntax.

Just remove the ResponseFormat = WebMessageFormat.Json and it should work as you want.