2015-11-19 8 views
1

J'ai un service WCF dans une application MVC qui ne fonctionne pas comme prévu.Le service RESTful de WCF ne capte pas sur/xml/string uri - erreur 404

Mon code pour mon IService.cs se présente comme suit:

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

Et mon code pour le Service.svc est:

public class Service : IService 
{ 
    #region IService Members 

    public string XMLData(string name) 
    { 
     return "Hello " + name; 
    } 

    public string JSONData(string name) 
    { 
     return "Hello " + name; 
    } 
    #endregion 
} 

Quand je vais à localhost/Service.svc, je obtenir la page que je m'attends à. Cependant, quand je vais à localhost/Service.svc/xml/Tim (ou/json /), j'obtiens une erreur 404 - ressource ne peut pas être trouvé. Je suis assez positif J'ai aussi configuré mon fichier Web.config correctement, mais je peux le poster si quelqu'un pense que c'est le problème (c'est juste un peu gonflé avec tout le contenu MVC).

Des idées sur ce qui cause cela? Merci!

EDIT: Voici mon fichier complet 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> 
+0

Pouvez-vous aller à 'localhost/Service.svc? Wsdl'? –

+0

Oui, je peux. Et je peux voir les opérations XMLData et JSONData là-dedans –

Répondre

1

Je vais partager avec vous ma configuration, car le service (s) derrière exploitons depuis sans interruption pendant deux ans sans problème, la variole retour, Jones, et du savon. Je parie que c'est quelque chose dans la configuration du point de terminaison de service. BTW: Je ne pense pas que vous devriez spécifier le comportement du point de terminaison dans votre modèle d'uri (json/xml). C'est plus pour les trucs de type matching de paramètres, 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> 
+0

Hmm, j'ai ajouté dans certains de votre code. Je n'ai pas eu les comportements séparés pour xml et json, mais cela ne l'a pas résolu. Encore obtenir la même erreur. –

+0

S'il s'agit toujours de 404, il s'agit d'un problème de point de terminaison de service ou d'une signature de méthode de service Web introuvable en raison du routage. Utilisez-vous DataContracts pour vos réponses? En outre, ajoutez-vous le paramètre attendu à votre URL? - localhost/Service.svc/xml/Tim/valueforNameParam –

+0

Je n'utilise pas DataContracts. Je ne suis pas vraiment sûr de ce que c'est, mais je les ai vus en train de faire des recherches sur Google. C'est ma première interaction avec les services Web, c'est donc un peu nouveau pour moi. La chose étrange est, j'ai utilisé exactement ce même code dans une application de service Web WCF et cela a bien fonctionné. Ensuite, j'ai essayé d'utiliser le même code et d'ajouter un service Web WCF à mon projet MVC et cela ne fonctionnera pas. –