Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original request url in WCF REST service

I've to retrieve the orginal request url in my WCF rest webservice. Now my code looks like this:

public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        base.CheckAccessCore(operationContext);

        var url = operationContext.IncomingMessageProperties.Via.OriginalString;
        ...

web.config

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <standardEndpoints> 
         <webHttpEndpoint>
             <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

if my original url is

http://192.168.1.100:8081/test

this code return

http://hostname:8081/test

is there a way to retrieve the exact original request url?

Note

I found posts talking about cutomize "baseAddress" tag in web.config but I've no specific endpoint fom my extensionles webservice and I don't want to add it. I don't know if there is a way to do it without endpoint.

I found this post https://stackoverflow.com/a/5915713/735864 plays with System.Net.HttpRequestHeader.Host but with port number it doesn't works! I know I can parse provided url and do a Replace but... I don't think this is the best practice to achieve this.

like image 300
danyolgiax Avatar asked Feb 02 '12 10:02

danyolgiax


People also ask

How to get URL of WCF service?

Short answer: var url = System. Web. HttpContext.

How do I enable rest and SOAP on the same WCF service?

So how to enable both SOAP and REST services in the same WCF service. STEP 1: Create a new WCF service application. Configure service for both basicHttpBinding and webHttpBinding, So in Web. Config file define two endpoints - one each for SOAP and REST.

What is RESTful WCF service?

RESTful service follows the REST (Representational State Transfer) architectural style. WCF service will allows to make calls and exchange the data using SOAP protocol over different protocols (HTTP, TCP, MSMQ etc..) and it uses the complex mechanism like SOAP for communication.


1 Answers

System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;

This gives the original URI.

like image 94
Karthik D V Avatar answered Sep 24 '22 13:09

Karthik D V