Cómo utilizar un servicio WCF con HTTP GET (dentro de Visual Studio 2010)
-
28-09-2019 - |
Pregunta
Hemos tratado de utilizar un servicio WCF muy muy simple con un HTTP GET y no lo puede operar. Hemos seguido los "guía" pero no funciona
- http://msdn.microsoft.com/en-us/library/ bb412178.aspx
- http://www.dotnetfunda.com/articles/article779-simple-5-steps-to-expose-wcf-services-using-rest-style-.aspx
Cuando llamamos a nuestro servicio con el siguiente URL, se obtiene un error de página no encontrada:
La URL base (http: // localhost: 9999 / Service1.svc). Trabaja muy bien y devuelve la página de información de servicio WCF correctamente
Estos son los pasos y el código para reproducir nuestro ejemplo.
- En Visual Studio 2010, cree un nuevo "servicio WCF aplicación" Proyecto
-
Sustituir la interfaz IService con este código
[ServiceContract()] public interface IService1 { [OperationContract()] [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetData/{value}")] string GetData(string value); }
-
Sustituir la clase de servicio con el código
public class Service1 : IService1 { public string GetData(string value) { return string.Format("You entered: {0}", value); } }
-
La mirada web.config como esto
<system.web> <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="Service1"> <endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="WebBehavior1"> </endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name="WebBehavior1"> <webHttp helpEnabled="True"/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors>
- Pulse Ejecutar y tratar de llamar al método get
Si alguien consigue esto o algo similar de trabajo, sería muy amable si pudiera responder información sobre el ejemplo de trabajo.
Muchas gracias
Solución
He recreado su muestra -. Funciona como un encanto
Un punto: hacer su contrato de servicio (public interface IService1
) y la implementación del servicio (public class Service1 : IService1
) se realiza al interior de un espacio de nombres de .NET ??
Si es así, usted necesita cambiar su * .svc y su web.config
incluir:
<services>
<service name="Namespace.Service1">
<endpoint address="" binding="webHttpBinding"
contract="Namespace.IService1"
behaviorConfiguration="WebBehavior1">
</endpoint>
</service>
</services>
El atributo <service name="...">
y la <endpoint contract="...">
debe incluir el espacio de nombres de .NET para que esto funcione.