Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I have the following error creating and simple service with WCF REST (just for Simplifying the code, I don't create an interface)

C#
[ServiceContract]
  public class Regiones
  {
      RegionesServiceEntities db = new RegionesServiceEntities();

      [OperationContract]
      [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Ciudad/{NombreCiudad}")]
      public string Ciudades(int id)
      {
          var query = from t in db.Ciudad
                      where t.CiudadID == id
                      select new { t.NombreCiudad };
          var mObjeCiudad = query.SingleOrDefault();
          return mObjeCiudad.ToString();
      }

      [OperationContract]
      [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Departamento/{NombreDepartamento}")]
      public string Departamento(int id)
      {
          var query = from t in db.Departamento
                      where t.DepartamentoID == id
                      select new { t.NombreDepartamento };
          var mObjDepto = query.SingleOrDefault();
          return mObjDepto.ToString();
      }

      [OperationContract]
      [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Pais/{NombrePais}")]
      public string Paises(int id)
      {
          var query = from t in db.Pais
                      where t.PaisID == id
                      select new { t.NombrePais};
          var mObjPais = query.SingleOrDefault();
          return mObjPais.ToString();
      }
  }


and the web.config was organized like this...

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.serviceModel>
    
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="coRegionesService.Regiones">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
          bindingConfiguration="webHttpBindingWithJsonP" contract="coRegionesService.IRegiones" />
      </service>
    </services>
    
    <behaviors>
      
      <serviceBehaviors>

        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        
      </serviceBehaviors>
      
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    
    </behaviors>
    
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    
    <bindings>
      
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    
    </bindings>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="RegionesServiceEntities" connectionString="metadata=res://*/RegionesModel.csdl|res://*/RegionesModel.ssdl|res://*/RegionesModel.msl;provider=System.Data.SqlClient;provider connection string="data source=Express/database;initial catalog=RegionesService;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>


and the error that shows me is this

The operation 'Cities' contract 'Regions' has a UriTemplate that expects a parameter called 'NameCity', but there is no parameter entry with that name in the operation.

any help??

What I have tried:

[WebInvoke(Method = "GET", UriTemplate = "ciudades/{NombreCiudad}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Ciudades(int id)
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "departamento/{NombreDepartamento}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Departamento(int id)
{
Posted
Comments
David_Wimbley 8-Aug-16 11:32am    
Have you tried changing "/Ciudad/{NombreCiudad}" to "/Ciudad/{id}" in your URI templates. I believe the URI template paramter ( the text inside the curly brackets) needs to be the same as what is in the signature of your method.

So if you have a parameter of string id, your URI template should reference {id}. Either that or you need to change your parameter name in your methods to match what is in the curly brackets. ex: ciudades/{NombreCiudad} template needs to have a parameter being passed into the method public string Ciudades(string NombreCiudad)
GREG_DORIANcod 8-Aug-16 11:49am    
yes!! I did that!! a new error shows The ends using 'UriTemplate' can not be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.
David_Wimbley 8-Aug-16 14:11pm    
I think this link may be of help for the new error.

http://kaushikghosh12.blogspot.com/2012/10/a-known-error-in-wcf-restful-service.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900