WCF RESTful服务无法获取/ xml / string uri - 404错误

时间:2015-11-19 01:07:36

标签: c# xml wcf rest

我在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时,我得到了我期望的页面。但是,当我转到localhost / 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>

1 个答案:

答案 0 :(得分:1)

我将与您分享我的配置,因为它背后的服务已连续运行两年没有问题,返回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>