none
I think I lose my header. what happend? RRS feed

  • Question

  • Hi. I  developing basic WCF.

    When I clicked the button on my client, he send data by "OPTION" to my service.

    I used ajax and send by json

    -----------------------------------------------------------------------

    $.ajax({
                    url: 'http://localhost:50870/Service.svc/Get_DoWork',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(rtn),
                    dataType: "json",
                    success: function (data)

    -----------------------------------------------------------------------

    and first , my service get a data like this by option

    ----------------------------------------------------------------------

    [OperationContract]
        [WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
        public void PostOptions()
        {
            var origin = WebOperationContext.Current.IncomingRequest.Headers["Origin"];

            var ser_Domains = ConfigurationManager.AppSettings["service_Domain"].Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var item in ser_Domains)
            {
                if (item == origin)
                {
                    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
                    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
                    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Origin, Accept, Content-Type");
                }
            }
        }

    -----------------------------------------------------------------------------------------

    and it will go to post method

    -----------------------------------------------------------------------------------------

     [OperationContract]
        //[WebInvoke(Method = "POST", UriTemplate = "/Get_DoWork/{value}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        [WebInvoke(Method = "POST", UriTemplate = "/Get_DoWork", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        public Request_RTN DoWork(Request_RTN value)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            return new Request_RTN() { RTN = " " + value.RTN }; //new Request_RTN() { RTN = " " + value.RTN };

        }

    -----------------------------------------------------------------------------------------

    problem is, in option method I use

    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

    and in post method I use

    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

    too, I want write this code only one time.

    and I don't want to use global.asmx

    I think when data go to post method from option method, may he lose his Header. So he need new

    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

    when he go to client. am I right?


    I want some solution please help me

    I want write this code only one time. 

    (WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");)

    and I don't want to use global.asmx

    Thanks.

    Thursday, March 29, 2018 2:13 AM

All replies

  • Hi tcl,

    Could you share us the reason you did not want to use Global.asmx which is standard way to resovle CORS issue?

    I suggest you check whether constructure will work for you.

        public class Service1 : IService1
        {
            public Service1()
            {
                //add code here
            }

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 29, 2018 6:42 AM
  • Hi, I tried your suggest. but unfortunately It didn't work.

    That's why I didn't want to use Global.asmx is dll.

    I want to use at least dll. I think Global.asmx is not included dll. Am l right?

    I want to maintain my program to easy.

    Thank for you help.

    Thursday, March 29, 2018 11:42 PM
  • How did you host WCF Service? Did you host it by self-host? I am not sure what you mean by use dll. Do you use WCF library instead of WCF application?

    I suggest you try WCF Extensions.

    #WCF: CORS support for self-hosted WCF REST service

    https://blogs.msdn.microsoft.com/dsnotes/2016/08/31/wcf-cors-support-for-self-hosted-wcf-rest-service/

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 30, 2018 2:55 AM