WCF RESTful服务未接收/xml/string uri-404错误

本文关键字:xml string uri-404 错误 RESTful 服务 WCF | 更新日期: 2023-09-27 18:22:46

我在MVC应用程序中有一个WCF服务,但它并没有像我预期的那样工作。

我的IService.cs代码如下:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        // Set output to XML and the text display to wrap
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "xml/{name}")]
    string XMLData(string name);
    [OperationContract]
    [WebInvoke(Method = "GET",
        // Set output to JSON and the text display to wrap
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/{name}")]
    string JSONData(string name);
}

我的Service.svc代码是:

    public class Service : IService
{
    #region IService Members
    public string XMLData(string name)
    {
        return "Hello " + name;
    }
    public string JSONData(string name)
    {
        return "Hello " + name;
    }
    #endregion
}

当我转到localhost/Service.svc时,我得到了我想要的页面。然而,当我转到本地主机/Service.svc/xml/Tim(或/json/)时,我会得到一个404-找不到资源的错误。

我很肯定我的Web.config文件也配置正确,但如果有人认为这是问题所在,我可以发布它(它只是有点膨胀了所有MVC的东西)。

你知道是什么原因造成的吗?谢谢

编辑:这是我完整的Web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
   </configSections>
   <connectionStrings>
   <add name="DefaultConnection" connectionString="Data Source= (LocalDb)'v11.0;Initial Catalog=aspnet-FinalProjectGuitar-20151110211739;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|'aspnet-FinalProjectGuitar-20151110211739.mdf" providerName="System.Data.SqlClient" />
   </connectionStrings>
<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
  <authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" />
  </authentication>
  <pages>
    <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>
</system.web>
<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
<handlers>
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%'Microsoft.NET'Framework'v4.0.30319'aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%'Microsoft.NET'Framework64'v4.0.30319'aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
      <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="v11.0" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>
<system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding name="WebxmlHttp">
        <security mode="None"/>
      </binding>
      <binding name="WebjsonHttp">
        <security mode="None"/>
      </binding>
    </webHttpBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="ServiceBehavior" name="FinalProjectGuitar.Service">
      <endpoint address="xml" behaviorConfiguration="xmlBehavior" binding="webHttpBinding"
      bindingConfiguration="WebxmlHttp" name="webXMLHttpBinding" contract="FinalProjectGuitar.IService" />
      <endpoint address="json" behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
      bindingConfiguration="WebjsonHttp" name="webJSONHttpBinding" contract="FinalProjectGuitar.IService" />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
    <behavior name="jsonBehavior">
      <webHttp defaultOutgoingResponseFormat="Json"/>
      </behavior>
      <behavior name="xmlBehavior">
        <webHttp defaultOutgoingResponseFormat="Xml"/>
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehavior">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>

WCF RESTful服务未接收/xml/string uri-404错误

我将与您分享我的配置,因为它背后的服务已经连续运行了两年,没有出现问题,返回了pox、json和soap。我敢打赌它是服务端点配置中的某个东西。

BTW:我认为您不应该在uri模板(json/xml)中指定端点行为。这是更多的参数匹配类型的东西,imo.

[OperationContract()]
[WebGet(UriTemplate = "GetData/{aParam1}/{aParam2}")]
MyResponse GetData(string aParam1, string aParam2);

web.config

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="WebxmlHttp">
          <security mode="None"/>
        </binding>
        <binding name="WebjsonHttp">
          <security mode="None"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="SecureServiceBehavior" name="XXXX.XXXX.Services.Reporting.ServiceImplementation.XXXX">
        <clear />
        <endpoint address="xml" behaviorConfiguration="xmlBehavior" binding="webHttpBinding"
          bindingConfiguration="WebxmlHttp" name="webXMLHttpBinding" contract="XXXX.XXXX.Services.Reporting.ServiceContracts.XXXX" />
        <endpoint address="json" behaviorConfiguration="jsonBehavior"
          binding="webHttpBinding" bindingConfiguration="WebjsonHttp"
          name="webJSONHttpBinding" contract="XXXX.XXXX.Services.Reporting.ServiceContracts.IXXXXX" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
          <webHttp defaultOutgoingResponseFormat="Json"/>
        </behavior>
        <behavior name="xmlBehavior">
          <webHttp defaultOutgoingResponseFormat="Xml"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SecureServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647"/>
          <serviceTimeouts transactionTimeout="00:03:00"/>
          <serviceThrottling maxConcurrentCalls="10" maxConcurrentSessions="10" maxConcurrentInstances="10"/>
        </behavior>
      </serviceBehaviors>
    </behaviors> 
  </system.serviceModel>