简体   繁体   中英

WCF Rest service post method gives endpoint not found

I am not able to call a rest service using the post method and keep getting an endpoint not found error. Code here below:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(
    Method = "POST",
    UriTemplate = "GetData",
    BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json
    )]
    string GetData(string value);
}


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : IService1
{
    public string GetData(string value)
    {
        return value;
    }
}

The web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxUrlLength="1048576" relaxedUrlToFileSystemMapping="true" />
  </system.web>
  <system.serviceModel>

    <protocolMapping>
      <add scheme="http" binding="webHttpBinding" />
    </protocolMapping>

    <services>
      <service name="RestPost.Service1" behaviorConfiguration="default">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"  contract="RestPost.IService1" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web"  >
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="default">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <!-- default binding configration used for all REST services -->
      <webHttpBinding>
        <!-- max allowed message size incresed to 500 000 Bytes -->
        <binding maxBufferSize="95000000" maxReceivedMessageSize="95000000" />
      </webHttpBinding>
    </bindings>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>


   <security>
     <requestFiltering>
       <requestLimits maxUrl="40960000" maxQueryString="20480000" maxAllowedContentLength="20480000" />
     </requestFiltering>
   </security>
  </system.webServer>

</configuration>

This is how I am calling the url in the browser

http://localhost:57395/Service1.svc/getdata/large base64encoded string here

This is how I call it in fiddler 在此处输入图片说明

I am trying to run this under Casini now. Eventually it would be deployed on IIS 7.5.

If you are wondering why I am passing a large base64 encoded string, I am doing it because I need to post a request in JSON format. Now since JSON has special charachters that IIS rejects right away, I tried using URLencode. The problem with this is that you cant go beyond 1000 chars or so. There is a finite limit to the length. Base64 encoding and using post was the only way to go, which is why I am going with this code.

The original goal of this Rest service is to be able to serve a javascript based client that will make a JSON post to this service and get a JSON response in return. A pure JSON response without the xml string padding.

Need help in getting the post to the rest service to work.

Why it fails when you call it via the browser: the browser is making a GET request, not a POST request.

Why it fails when you call it via Fiddler: your content-type is "application/x-www-form-urlencoded", but the content is not (it's a base64 blob). Even if you had a nicely-formatted form-urlencoded data (such as a=foo&b=bar ), that would still not work, since WCF doesn't support this format out-of-the-box (you can use some of the extensibility points to add the support, but it requires more work).

What you need to do: your operation expects a string parameter. In Fiddler, you can pass the base64-encoding of your data as a JSON string (ie, wrap it in " ). Also, set the correct content-type:

POST http://localhost:57395/Service1.svc/getdata
Content-Type: application/json
Host: localhost:57395
Content-Length: <the appropriate length; fiddler will set it for you>

"large base64 string here"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM