How to send and retrieve custom header information for REST WCF Service The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceBest Practices for securing a REST API / web serviceREST / SOAP endpoints for a WCF serviceHow to access a property in a class used to implement IDispatchMessageInspector on a WCF service (server side)?Authenticating wcf serviceHow do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?WCF REST JSON with ISO Date FormatJSON, REST, SOAP, WSDL, and SOA: How do they all link togetherWCF rest client: unable to send HTTP message when triggered by another WCF service in the solutionSending Image Files from Android to WCF REST ServiceDifferent Security for different Service operation in WCF

Keeping a retro style to sci-fi spaceships?

Separating matrix elements by lines

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Can the Right Ascension and Argument of Perigee of a spacecraft's orbit keep varying by themselves with time?

Could an empire control the whole planet with today's comunication methods?

"... to apply for a visa" or "... and applied for a visa"?

What's the point in a preamp?

Was credit for the black hole image misappropriated?

What force causes entropy to increase?

Can we generate random numbers using irrational numbers like π and e?

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Make it rain characters

My body leaves; my core can stay

Why doesn't a hydraulic lever violate conservation of energy?

How to read αἱμύλιος or when to aspirate

Single author papers against my advisor's will?

How to support a colleague who finds meetings extremely tiring?

Is this wall load bearing? Blueprints and photos attached

Would an alien lifeform be able to achieve space travel if lacking in vision?

How did the audience guess the pentatonic scale in Bobby McFerrin's presentation?

What is the role of 'For' here?

Did the new image of black hole confirm the general theory of relativity?

Homework question about an engine pulling a train

How did passengers keep warm on sail ships?



How to send and retrieve custom header information for REST WCF Service



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceBest Practices for securing a REST API / web serviceREST / SOAP endpoints for a WCF serviceHow to access a property in a class used to implement IDispatchMessageInspector on a WCF service (server side)?Authenticating wcf serviceHow do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?WCF REST JSON with ISO Date FormatJSON, REST, SOAP, WSDL, and SOA: How do they all link togetherWCF rest client: unable to send HTTP message when triggered by another WCF service in the solutionSending Image Files from Android to WCF REST ServiceDifferent Security for different Service operation in WCF



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am struggling to set-up infrastructure in my solution to send and retrieve the custom header for REST WCF Service. Basically, we need this to send UserID, password, token value from client to service and if provided values are valid then operation will continue to execute otherwise throw exception.



We already have few classes inherited from interfaces like IDispatchMessageInspector, IClientMessageInspector, IEndPointBehaviour, MessageHeader, etc., This is working fine for WCF with soap request. I tried to use these classes for my new REST WCF Service, but was not working as MessageHeader derived class supports only Soap.



I also tried using WebOperationContext, but no luck :(



Please provide a solution along with sample project to solve this problem.



Thank you so much!










share|improve this question




























    0















    I am struggling to set-up infrastructure in my solution to send and retrieve the custom header for REST WCF Service. Basically, we need this to send UserID, password, token value from client to service and if provided values are valid then operation will continue to execute otherwise throw exception.



    We already have few classes inherited from interfaces like IDispatchMessageInspector, IClientMessageInspector, IEndPointBehaviour, MessageHeader, etc., This is working fine for WCF with soap request. I tried to use these classes for my new REST WCF Service, but was not working as MessageHeader derived class supports only Soap.



    I also tried using WebOperationContext, but no luck :(



    Please provide a solution along with sample project to solve this problem.



    Thank you so much!










    share|improve this question
























      0












      0








      0








      I am struggling to set-up infrastructure in my solution to send and retrieve the custom header for REST WCF Service. Basically, we need this to send UserID, password, token value from client to service and if provided values are valid then operation will continue to execute otherwise throw exception.



      We already have few classes inherited from interfaces like IDispatchMessageInspector, IClientMessageInspector, IEndPointBehaviour, MessageHeader, etc., This is working fine for WCF with soap request. I tried to use these classes for my new REST WCF Service, but was not working as MessageHeader derived class supports only Soap.



      I also tried using WebOperationContext, but no luck :(



      Please provide a solution along with sample project to solve this problem.



      Thank you so much!










      share|improve this question














      I am struggling to set-up infrastructure in my solution to send and retrieve the custom header for REST WCF Service. Basically, we need this to send UserID, password, token value from client to service and if provided values are valid then operation will continue to execute otherwise throw exception.



      We already have few classes inherited from interfaces like IDispatchMessageInspector, IClientMessageInspector, IEndPointBehaviour, MessageHeader, etc., This is working fine for WCF with soap request. I tried to use these classes for my new REST WCF Service, but was not working as MessageHeader derived class supports only Soap.



      I also tried using WebOperationContext, but no luck :(



      Please provide a solution along with sample project to solve this problem.



      Thank you so much!







      rest wcf c#-4.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 12:32









      SriSri

      11




      11






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Seems in your case it might be easier to interogate the ASPNET pipeline



          if you add the following to your WCF service to allow it to hookup into the ASPNET pipeline




          [AspNetCompatibilityRequirements(RequirementsMode =
          AspNetCompatibilityRequirementsMode.Allowed)]




          Then you can simply now use the HttpContext object and just get the headers as you would from a normal aspnet application, e.g



          System.Web.HttpContext.Current.Request.Headers["CustomHeader"]





          share|improve this answer






























            0














            If you want to add http header in wcf rest service , you should use HttpRequestMessageProperty, it has a Headers property , you could set http Header through its Headers property



             using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))

            HttpRequestMessageProperty property;
            // if OutgoingMessageProperties already has HttpRequestMessageProperty, use the existing one , or initialize a new one and
            set OutgoingMessageProperties's HttpRequestMessageProperty.Name key's value to the initialized HttpRequestMessageProperty so that the HttpRequestMessageProperty will work
            if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name))
            property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

            else

            property = new HttpRequestMessageProperty();
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;

            // add headers to HttpRequestMessageProperty, it will become the http header of the reuqest
            property.Headers.Add(System.Net.HttpRequestHeader.Authorization, "myAuthorization");
            string re = client.HelloWorld();



            About getting the Header , just use WebOperationContext.Current.Headers.



            WebOperationContext.Current.IncomingRequest.Headers["MyCustomHttpHeader"]


            Please refer to http://kenneththorman.blogspot.com/2011/02/wcf-rest-client-using-custom-http.html






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063335%2fhow-to-send-and-retrieve-custom-header-information-for-rest-wcf-service%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Seems in your case it might be easier to interogate the ASPNET pipeline



              if you add the following to your WCF service to allow it to hookup into the ASPNET pipeline




              [AspNetCompatibilityRequirements(RequirementsMode =
              AspNetCompatibilityRequirementsMode.Allowed)]




              Then you can simply now use the HttpContext object and just get the headers as you would from a normal aspnet application, e.g



              System.Web.HttpContext.Current.Request.Headers["CustomHeader"]





              share|improve this answer



























                0














                Seems in your case it might be easier to interogate the ASPNET pipeline



                if you add the following to your WCF service to allow it to hookup into the ASPNET pipeline




                [AspNetCompatibilityRequirements(RequirementsMode =
                AspNetCompatibilityRequirementsMode.Allowed)]




                Then you can simply now use the HttpContext object and just get the headers as you would from a normal aspnet application, e.g



                System.Web.HttpContext.Current.Request.Headers["CustomHeader"]





                share|improve this answer

























                  0












                  0








                  0







                  Seems in your case it might be easier to interogate the ASPNET pipeline



                  if you add the following to your WCF service to allow it to hookup into the ASPNET pipeline




                  [AspNetCompatibilityRequirements(RequirementsMode =
                  AspNetCompatibilityRequirementsMode.Allowed)]




                  Then you can simply now use the HttpContext object and just get the headers as you would from a normal aspnet application, e.g



                  System.Web.HttpContext.Current.Request.Headers["CustomHeader"]





                  share|improve this answer













                  Seems in your case it might be easier to interogate the ASPNET pipeline



                  if you add the following to your WCF service to allow it to hookup into the ASPNET pipeline




                  [AspNetCompatibilityRequirements(RequirementsMode =
                  AspNetCompatibilityRequirementsMode.Allowed)]




                  Then you can simply now use the HttpContext object and just get the headers as you would from a normal aspnet application, e.g



                  System.Web.HttpContext.Current.Request.Headers["CustomHeader"]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 15:54









                  mahlatsemahlatse

                  1,012519




                  1,012519























                      0














                      If you want to add http header in wcf rest service , you should use HttpRequestMessageProperty, it has a Headers property , you could set http Header through its Headers property



                       using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))

                      HttpRequestMessageProperty property;
                      // if OutgoingMessageProperties already has HttpRequestMessageProperty, use the existing one , or initialize a new one and
                      set OutgoingMessageProperties's HttpRequestMessageProperty.Name key's value to the initialized HttpRequestMessageProperty so that the HttpRequestMessageProperty will work
                      if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name))
                      property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

                      else

                      property = new HttpRequestMessageProperty();
                      OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;

                      // add headers to HttpRequestMessageProperty, it will become the http header of the reuqest
                      property.Headers.Add(System.Net.HttpRequestHeader.Authorization, "myAuthorization");
                      string re = client.HelloWorld();



                      About getting the Header , just use WebOperationContext.Current.Headers.



                      WebOperationContext.Current.IncomingRequest.Headers["MyCustomHttpHeader"]


                      Please refer to http://kenneththorman.blogspot.com/2011/02/wcf-rest-client-using-custom-http.html






                      share|improve this answer





























                        0














                        If you want to add http header in wcf rest service , you should use HttpRequestMessageProperty, it has a Headers property , you could set http Header through its Headers property



                         using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))

                        HttpRequestMessageProperty property;
                        // if OutgoingMessageProperties already has HttpRequestMessageProperty, use the existing one , or initialize a new one and
                        set OutgoingMessageProperties's HttpRequestMessageProperty.Name key's value to the initialized HttpRequestMessageProperty so that the HttpRequestMessageProperty will work
                        if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name))
                        property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

                        else

                        property = new HttpRequestMessageProperty();
                        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;

                        // add headers to HttpRequestMessageProperty, it will become the http header of the reuqest
                        property.Headers.Add(System.Net.HttpRequestHeader.Authorization, "myAuthorization");
                        string re = client.HelloWorld();



                        About getting the Header , just use WebOperationContext.Current.Headers.



                        WebOperationContext.Current.IncomingRequest.Headers["MyCustomHttpHeader"]


                        Please refer to http://kenneththorman.blogspot.com/2011/02/wcf-rest-client-using-custom-http.html






                        share|improve this answer



























                          0












                          0








                          0







                          If you want to add http header in wcf rest service , you should use HttpRequestMessageProperty, it has a Headers property , you could set http Header through its Headers property



                           using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))

                          HttpRequestMessageProperty property;
                          // if OutgoingMessageProperties already has HttpRequestMessageProperty, use the existing one , or initialize a new one and
                          set OutgoingMessageProperties's HttpRequestMessageProperty.Name key's value to the initialized HttpRequestMessageProperty so that the HttpRequestMessageProperty will work
                          if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name))
                          property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

                          else

                          property = new HttpRequestMessageProperty();
                          OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;

                          // add headers to HttpRequestMessageProperty, it will become the http header of the reuqest
                          property.Headers.Add(System.Net.HttpRequestHeader.Authorization, "myAuthorization");
                          string re = client.HelloWorld();



                          About getting the Header , just use WebOperationContext.Current.Headers.



                          WebOperationContext.Current.IncomingRequest.Headers["MyCustomHttpHeader"]


                          Please refer to http://kenneththorman.blogspot.com/2011/02/wcf-rest-client-using-custom-http.html






                          share|improve this answer















                          If you want to add http header in wcf rest service , you should use HttpRequestMessageProperty, it has a Headers property , you could set http Header through its Headers property



                           using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))

                          HttpRequestMessageProperty property;
                          // if OutgoingMessageProperties already has HttpRequestMessageProperty, use the existing one , or initialize a new one and
                          set OutgoingMessageProperties's HttpRequestMessageProperty.Name key's value to the initialized HttpRequestMessageProperty so that the HttpRequestMessageProperty will work
                          if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name))
                          property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

                          else

                          property = new HttpRequestMessageProperty();
                          OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;

                          // add headers to HttpRequestMessageProperty, it will become the http header of the reuqest
                          property.Headers.Add(System.Net.HttpRequestHeader.Authorization, "myAuthorization");
                          string re = client.HelloWorld();



                          About getting the Header , just use WebOperationContext.Current.Headers.



                          WebOperationContext.Current.IncomingRequest.Headers["MyCustomHttpHeader"]


                          Please refer to http://kenneththorman.blogspot.com/2011/02/wcf-rest-client-using-custom-http.html







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 11 at 8:45

























                          answered Mar 11 at 8:32









                          Ackelry XuAckelry Xu

                          6066




                          6066



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063335%2fhow-to-send-and-retrieve-custom-header-information-for-rest-wcf-service%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                              Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                              Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved