【问题标题】:WCF Rest service post method gives endpoint not foundWCF Rest 服务发布方法给出未找到端点
【发布时间】:2012-12-14 09:04:26
【问题描述】:

我无法使用 post 方法调用休息服务并不断收到未找到端点的错误。代码如下:

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

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

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

这就是我在提琴手中的称呼

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

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

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

需要帮助才能将帖子发送到 REST 服务。

【问题讨论】:

    标签: .net wcf rest iis wcf-binding


    【解决方案1】:

    为什么通过浏览器调用它会失败:浏览器发出的是GET 请求,而不是POST 请求。

    为什么当您通过 Fiddler 调用它时它会失败:您的内容类型是“application/x-www-form-urlencoded”,但内容不是(它是一个 base64 blob)。即使您有格式良好的 form-urlencoded 数据(例如 a=foo&amp;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"
    

    【讨论】:

    • 实际上,为了发送 base64 输入,我只需将输入参数从字符串更改为 Stream,当我从 Fiddler 测试时它就起作用了。我将内容类型保留为“application/x-www-form-urlencoded”。你是正确的关于浏览器获取而不是发布。
    • 对,您也可以使用raw programming model 接收任意数据,例如您的情况下的表单编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2014-01-15
    相关资源
    最近更新 更多