Cross Domain JSON Enabled WCF

I have a WCF service decorated with WebInvoke attributes and a WebHttp binding to include JSON. The service can be accessed from JavaScript as long as we don't try to make it cross domain. Can you please recommend how to make this cross domain work?

We tried to create a proxy, but it gives a "Bad Request" every time the WebHttpRequest tries to access it.

0


source to share


3 answers


I needed to create a proxy. Cross domain request only works with GET verb, not POST. My whole request goes through a proxy and if it is a POST it acts like a typical proxy. If the request uses GET, I have to convert it to POST. (I specify POST as a verb in my service contracts).

On the client side, I am using JQuery josnp (json with padding) to add the correct information to the query string.



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());
        }
    }

      

0


source


Follow the instructions in Parts 1-4 of this great article series for a clean solution. I have been using it in production with no problem.

You have to make one tweak to make it work with all browsers. In CorsDispatchMessageInspector.BeforeSendReply

write the check:



if (state.Message! = null)

otherwise, Allow headers apply only to pre-flight requests and not to the actual request.

0


source


To solve the problem, do the following

Create Global.asax and add the following code to enable POST AJAX between domains

 public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST,OPTIONS");

            if ((HttpContext.Current.Request.HttpMethod == "OPTIONS"))
            {

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
    }

      

0


source







All Articles