views:

1518

answers:

1

Hello,

I have a WCF Service decorated with WebInvoke attributes and WebHttp binding for JSON enabling. The service can be accessed from JavaScript until we try to make it working cross domain. Can you please recommend how to get this to work cross domain?

We've tried to create proxy web handler but it gives "Bad Request" everytime WebHttpRequest try to access it.

Thanks

Teera

A: 

What I had to do was to create a proxy. Cross domain request only work with the GET verb, and not POST. All my request go through the proxy, and if its a POST then it acts as a typical proxy. If the request is using a GET then I have to convert it to a POST. (I specify POST as the verb in my service contracts).

On the client side i use JQuery's josnp (json with padding) functionality to append the correct information to the querystring.

private static readonly Properties.Settings settings = new Properties.Settings();

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            string wcfAddress = context.Request.QueryString["WcfAddress"];                       

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(settings.WCFAddress + wcfAddress);

            request.ContentType = "application/json";

            request.Method = "POST";

            if (context.Request.RequestType == "GET")
            {
                string callback = context.Request.QueryString["callback"];
                string qs = context.Request.QueryString[null];
                byte[] body = body = Encoding.UTF8.GetBytes(qs);

                request.ContentLength = body.Length;

                request.GetRequestStream().Write(body, 0, body.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {

                    string contents = reader.ReadToEnd();

                    contents = callback + "(" + contents + ");";

                    context.Response.ContentType = "application/json";

                    context.Response.Write(contents);

                    response.Close();

                    reader.Close();
                }
            }
            else if (context.Request.RequestType == "POST")
            {
                byte[] body = new byte[context.Request.ContentLength];

                context.Request.InputStream.Read(body, 0, body.Length);

                request.ContentLength = body.Length;

                request.GetRequestStream().Write(body, 0, body.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    string contents = reader.ReadToEnd();

                    context.Response.ContentType = "application/json";

                    context.Response.Write(contents);

                    response.Close();

                    reader.Close();
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Trace.WriteLine(ex.ToString());
        }
    }
Mike_G