none
Error 405 for WCF service RRS feed

  • Question

  • Hi
    I have a wcf service hosted by IIS. Here is my service contract.
      [ServiceContract]
        public interface IBlackhawk
        {

            [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "/TransactionManagement/v2/Transaction/{transactionUMRequest}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            string Transaction(string transactionUMRequest);

    ...   

    }


    I can access my methods using webservice referense. See detais:

     ***Request***
     POST https://myhost.com/bhntgcc/Blackhawk.svc HTTP/1.1
     Content-Type: text/xml; charset=utf-8
     SOAPAction: "http://tempuri.org/IBlackhawk/Transaction"
     Host: myhost.com
     Content-Length: 1036
     Expect: 100-continue
     Accept-Encoding: gzip, deflate
     Connection: Keep-Alive

     <s:Envelope xmlns:s="<Transaction">http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Transaction xmlns="<transactionUMRequest>{"request":{"header">http://tempuri.org/"><transactionUMRequest>{"request":{"header":{"...
     
     ***Response***
     HTTP/1.1 200 OK
     Cache-Control: private
     Content-Length: 1094
     Content-Type: text/xml; charset=utf-8
     Server: Microsoft-IIS/7.5
     X-AspNet-Version: 4.0.30319
     X-Powered-By: ASP.NET
     Access-Control-Allow-Origin: *
     Access-Control-Allow-Headers: Content-Type
     Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
     Date: Fri, 21 Jun 2019 20:54:25 GMT

    But when I try to post my json with postman I get this error:
    405 - HTTP verb used to access this page is not allowed.
    The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access.

     ***Request***
     POST https://myhost.com/bhntgcc/TransactionManagement/v2/Transaction/ HTTP/1.1
     cache-control: no-cache
     Postman-Token: 9f9dd63e-d9a9-431d-8abe-c52e8cf732bb
     Content-Type: text/plain
     User-Agent: PostmanRuntime/6.1.6
     Accept: */*
     Host: myhost.com
     accept-encoding: gzip, deflate
     content-length: 842
     Connection: close

     {"request":{"header":{"...

     ***Response***
     HTTP/1.1 405 Method Not Allowed
     Allow: GET, HEAD, OPTIONS, TRACE
     Content-Type: text/html
     Server: Microsoft-IIS/7.5
     X-Powered-By: ASP.NET
     Access-Control-Allow-Origin: *
     Access-Control-Allow-Headers: Content-Type
     Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
     Date: Fri, 21 Jun 2019 21:04:13 GMT
     Connection: close
     Content-Length: 1293
     
    If someone can direct me to the cause of this problem it would be greatly appreciated.
    Thank you

    Friday, June 21, 2019 9:21 PM

Answers

  • Finally got it working. Had to allow longer url

    <httpRuntime  maxUrlLength="6144" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters=""

    and modify registry. This link helped: https://stackoverflow.com/questions/15004232/wcf-get-url-length-limit-issue-bad-request-invalid-url

    Thank you Abraham for being with me on this painful journey.

    • Marked as answer by lenab Tuesday, July 2, 2019 5:30 PM
    Tuesday, July 2, 2019 5:29 PM
  • Hi Lenab,

    the Services section should be removed from the web.config.

    Beside, Have you added the transactionUMRequest parameter to the reqeust?

    Feel free to let me know if there is anything I can help with.

    Best Regards

    Abraham

    • Marked as answer by lenab Tuesday, July 2, 2019 5:29 PM
    Friday, June 28, 2019 1:40 AM
    Moderator

All replies

  • The 405 not allowed error message usually  means that  a Post was used on the method when a Get was expected or vice versa, which applies to other HTTP verbs too. You're sending one verb when another verb is expected.
    Saturday, June 22, 2019 8:56 AM
  • Thank you for answering but how can it be? request shows as post and method is defined as post in operationcontract.

    • Edited by lenab Sunday, June 23, 2019 9:32 PM
    Sunday, June 23, 2019 9:29 PM
  • Thank you for answering but how can it be? request shows as post and method is defined as post in operationcontract.


    Well, what's the contract method and its logic supposed to be doing?
    Monday, June 24, 2019 5:27 AM
  • Hi,
    First, if the service is hosted with WebHttpBinding , the HTTP request headers should not contain SOAPAction field.  I suspect there some erroneous setup in the service binding.
    Moreover, if the method is defined by using the specified route.
    UriTemplate = "/TransactionManagement/v2/Transaction/{transactionUMRequest}"

    The practical route should contain the SVC path. Like below.
    https://myhost.com/bhntgcc/Blackhawk.svc/TransactionManagement/v2/Transaction/abc

    While yours is,
    https://myhost.com/bhntgcc/TransactionManagement/v2/Transaction/ 

    Therefore, I think there are something wrong with binding configuration.
    If you want to publish the service with Rest style, please refer to the below configuration (support both HTTP and HTTPS).
      <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="httpsbinding">
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
          <!--http and https are all supported.-->
          <add binding="webHttpBinding" scheme="http" />
          <add binding="webHttpBinding" scheme="https" bindingConfiguration="httpsbinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    I would like that you post more details about your configuration so that I could give you an effective reply.
    Feel free to let me know if there is anything I can help with.
    Best Regard
    Abraham
    Monday, June 24, 2019 6:50 AM
    Moderator
  • Hi Abraham
    Thank you for trying to help.
    I created whole new test environment , http , modified web.config based on your example:

     <system.serviceModel>
      <services>
          <service name="R1.WCF.Blackhawk.Service.Blackhawk">
            <endpoint address=""
                      binding="webHttpBinding"
                      bindingConfiguration="BasicHttpBinding_IBlackhawk"
                      contract="R1.WCF.Blackhawk.Service.IBlackhawk"/>

          </service>
        </services>
       <bindings>
          <webHttpBinding>
            <binding name="httpsbinding">
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
     <binding name="BasicHttpBinding_IBlackhawk">
    </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
          <!--http and https are all supported.-->
          <add binding="webHttpBinding" scheme="http" />
          <add binding="webHttpBinding" scheme="https" bindingConfiguration="httpsbinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
     
      </system.serviceModel>

    Now I am getting this

    ***Request**
    POST http://myhost.com/mytest/TransactionManagement/v2/Transaction/ HTTP/1.1
    cache-control: no-cache
    Postman-Token: c6ba2f3b-1956-4bee-a412-ba00ec4e1ef0
    Content-Type: application/json
    User-Agent: PostmanRuntime/6.1.6
    Accept: */*
    Host: myhost.com
    accept-encoding: gzip, deflate
    content-length: 842
    Connection: keep-alive

    {"request":{"header":{..."

    ***Reply***
    HTTP/1.1 404 Not Found

      <h2>404 - File or directory not found.</h2>
     
     
    And If I add .svc
    ***Request**
    POST http://myhost.com/mytest/Blackhawk.svc/TransactionManagement/v2/Transaction/ HTTP/1.1
    cache-control: no-cache
    Postman-Token: 163838e5-7c3e-4dd0-995f-12ae619e254b
    Content-Type: application/json
    User-Agent: PostmanRuntime/6.1.6
    Accept: */*
    Host: myhost.com
    accept-encoding: gzip, deflate
    content-length: 842
    Connection: keep-alive

    {"request":{"header":{"signature...

    ***Reply***
    HTTP/1.1 404 Not Found
     <p>Endpoint not found.</p>
    Tuesday, June 25, 2019 7:29 PM
  • Hi,

    Please try to use the below configuration. It still supports both Http and Https, as long as we configure http and https base address in the IIS site binding module.

      <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="httpsbinding">
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
          <!--http and https are all supported.-->
          <add binding="webHttpBinding" scheme="http" />
          <add binding="webHttpBinding" scheme="https" bindingConfiguration="httpsbinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Besides, since the route we defined in the method signature contains the variable transactionUMRequest, we should add the parameter when we invoke it.

    https://myhost.com/bhntgcc/Blackhawk.svc/TransactionManagement/v2/Transaction/abc


    The Protocol Mapping feature is provided by the WCF4.5, it could simplify our configuration. Please refer to the following document.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/simplified-configuration
    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham

    Wednesday, June 26, 2019 9:21 AM
    Moderator
  • web.config looks the same I still have the problem
    Thursday, June 27, 2019 1:24 PM
  • Hi Lenab,

    the Services section should be removed from the web.config.

    Beside, Have you added the transactionUMRequest parameter to the reqeust?

    Feel free to let me know if there is anything I can help with.

    Best Regards

    Abraham

    • Marked as answer by lenab Tuesday, July 2, 2019 5:29 PM
    Friday, June 28, 2019 1:40 AM
    Moderator
  • Hi Abraham

    I did all that

    now my request:

    POST /testservice/Blackhawk.svc/TransactionManagement/v2/Transaction/%7B%22request%22:%7B%.........

    Reply:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
    <HTML><HEAD><TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
    <BODY><h2>Bad Request - Invalid URL</h2>
    <hr><p>HTTP Error 400. The request URL is invalid.</p>
    </BODY></HTML>


    • Edited by lenab Tuesday, July 2, 2019 2:48 PM
    Tuesday, July 2, 2019 2:48 PM
  • Finally got it working. Had to allow longer url

    <httpRuntime  maxUrlLength="6144" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters=""

    and modify registry. This link helped: https://stackoverflow.com/questions/15004232/wcf-get-url-length-limit-issue-bad-request-invalid-url

    Thank you Abraham for being with me on this painful journey.

    • Marked as answer by lenab Tuesday, July 2, 2019 5:30 PM
    Tuesday, July 2, 2019 5:29 PM