WCF Rest服务post方法未找到端点

时间:2012-12-29 10:15:56

标签: .net wcf rest iis wcf-binding

我无法使用post方法调用rest服务并继续获取端点未找到错误。代码如下:

[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;
    }
}

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>

这就是我在浏览器中调用网址的方式

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

这就是我在小提琴手中的称呼方式 enter image description here

我正在尝试在Casini下运行此功能。最终它将部署在IIS 7.5上。

如果您想知道为什么我传递一个大的base64编码字符串,我这样做是因为我需要以JSON格式发布请求。现在,由于JSON具有IIS立即拒绝的特殊字符,我尝试使用URLencode。这个问题是你不能超过1000个字符左右。长度有一个有限的限制。 Base64编码和使用post是唯一的方法,这就是为什么我要使用这段代码。

此Rest服务的最初目标是能够为基于javascript的客户端提供服务,该客户端将向此服务发送JSON帖子并获得JSON响应。没有xml字符串填充的纯JSON响应。

需要帮助才能让帖子到其余服务中去工作。

1 个答案:

答案 0 :(得分:1)

为什么在您通过浏览器调用时失败:浏览器正在发出GET请求,而不是POST请求。

当你通过Fiddler调用它时失败的原因:你的内容类型是“application / x-www-form-urlencoded”,但内容不是(它是base64 blob)。即使你有一个格式良好的form-urlencoded数据(例如a=foo&b=bar),这仍然无效,因为WCF不支持这种开箱即用的格式(你可以使用一些可扩展性指向添加支持,但需要更多工作)。

您需要做什么:您的操作需要string参数。在Fiddler中,您可以将数据的base64编码作为JSON字符串传递(即将其包装在"中)。另外,设置正确的内容类型:

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"
相关问题