WCF RESTful Visual Studio 2013 年的调用问题

WCF RESTful Calling Issue in Visual Studio 2013

我有一个 ASP.NET 应用程序,它自 ASMX 时代以来就存在了。当我过去升级这个项目时,我能够通过扩展 System.Web.Services.WebService class 来利用 WCF 服务,然后利用 WebInvoke 属性允许 RESTful 调用我的各种方法。这是一个例子:

[ServiceContract(Namespace = "http://10.23.41.189/", Name = "SEO")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SEO : System.Web.Services.WebService
{
    [WebMethod(Description = "Use this Web Method to log into The SEO Panel")]
    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "Login?User={User}&Pass={Pass}")]
    public string Login(string User, string Pass)
    {

然后通过转到 http://10.23.41.189/seo.svc/login?User=USER&Pass=PASS

可以毫无问题地调用该方法

在旧项目中,我使用的是网站项目而不是 Web 应用程序,现在我已经升级到 Visual Studio 2013,我使用的是 Web 应用程序 - 但该调用不再有效。当我 运行 WSDL 时,我确实使用复制和粘贴移植了所有内容,我看到了这些方法,但所有调用都返回了 400 Bad Request 错误。这是我的 web.config 信息:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="SEO">
    <endpoint address="http://10.23.41.189/SEO.svc" behaviorConfiguration="json" binding="webHttpBinding" name="MainHttpPoint" contract="SEOPlatform.Intranet.SEO"/>
    <endpoint address="mex" binding="mexHttpBinding" name="MexEP" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="http://10.23.41.189/SEO.svc"/>
      </baseAddresses>
    </host>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding openTimeout="00:10:00" sendTimeout="00:10:00" useDefaultWebProxy="false">
      <readerQuotas maxDepth="32" maxStringContentLength="2048000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="json">
      <webHttp helpEnabled="false" automaticFormatSelectionEnabled="false" defaultBodyStyle="Bare"/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" httpGetBindingConfiguration=""/>
      <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" includeExceptionDetailInFaults="true"/>
      <useRequestHeadersForMetadataAddress>
        <defaultPorts>
          <add scheme="http" port="80"/>
        </defaultPorts>
      </useRequestHeadersForMetadataAddress>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel> 

我知道我遗漏了一些东西,但我想不通 - 有人有什么想法吗?

谢谢!

我发誓我应该等待 - 我经常在这里写问题然后相关的问题总是有我需要的答案。这次没有发生,但是当我输入 web.config 数据时,我发现了问题。与其删除这个问题,我想我会 post 一个希望能帮助其他人的答案。

问题出在服务名称上:

<services>
  <service name="SEO">

对于网站项目,规则很宽松,但 Web 应用程序需要正确的符号。我所做的只是将其更改为:

<services>
  <service name="SEOPlatform.Intranet.SEO">

这就解决了问题。我还将 multipleSiteBindingsEnabled 更改为 false,但如果您需要它是真的,那么已经在本网站的其他地方 post 编辑了很好的答案。