JornWildt / Ramone

A C# client framework for consuming HTTP/REST services
Other
61 stars 11 forks source link

Using to retrieve name and email from a webservice #9

Closed Ridermansb closed 10 years ago

Ridermansb commented 10 years ago

Supports WebService? How to read a WebService?

Need to retrieve the name and email of the following webservice: http://{{url}}.asmx?op=consultCustomer (I just url)

Must pass registration id: eg 12345

JornWildt commented 10 years ago

Take a look at this example: https://github.com/JornWildt/Ramone/wiki/GET-a-resource

You should do the same: "Cat" is your customer class, CatUrlTemplate is "http://{{url}}.asmx?op=consultCustomer&id={id}", replace "name" with "id" in the example (and so on ...).

But you have to tell what format your webservice is returning data in. JSON? HTML? XML? Other?

Ridermansb commented 10 years ago

How can I say that the format is XML?

My WebService

public class DadosRmService : System.Web.Services.WebService
{
    public class UsuarioRM
    {
        public string Nome { get; set; }
        public string Email { get; set; }
    }

    [WebMethod]
    public UsuarioRM ConsultaFuncionarioRm(int matricula)
    {
        return new UsuarioRM{ Nome="Usuário RM", Email="usuariorm@rm.com.br" };
    }
}

Consume

public IDictionary<string, object> GetRMUser(int matricula)
{
var endPointRM = "http://localhost:58592/WSDadosAeC/DadosRmService.asmx?op=ConsultaFuncionarioRm"
    ISession Session = RamoneConfiguration.NewSession(new Uri(endPointRM));
    Request req = Session.Bind(endPointRM, new { matricula = matricula });
    using (var resp = req.AcceptXml().Get<UsuarioRM>())
    {
        UsuarioRM c = resp.Body;
        return new Dictionary<string, object> { { "Nome", c.Nome.Trim() }, { "Email", c.Email.Trim() } };
    }
}

Error

debug

Full image: https://f.cloud.github.com/assets/725337/2064141/f48e0d74-8cc5-11e3-93da-b82001d6e7e6.png

JornWildt commented 10 years ago

This is wrong: Request req = Session.Bind(endPointRM, new { matricula = matricula }); You are trying yo bind the variable "matricula" in a URI without template variables. Where do you want the parameter "matricula" to be expanded into the URL?

JornWildt commented 10 years ago

Try:

public IDictionary<string, object> GetRMUser(int matricula)
{
    var endPoint = "http://localhost:58592/WSDadosAeC/"
    var path= "DadosRmService.asmx?op=ConsultaFuncionarioRm&matricula ={matricula }"
    ISession Session = RamoneConfiguration.NewSession(new Uri(endPoint));
    Request req = Session.Bind(path, new { matricula = matricula });
    using (var resp = req.AcceptXml().Get<UsuarioRM>())
    {
        UsuarioRM c = resp.Body;
        return new Dictionary<string, object> { { "Nome", c.Nome.Trim() }, { "Email", c.Email.Trim() } };
    }
}

If it still fails then please supply a stacktrace.

Ridermansb commented 10 years ago

Message:

Could not find a reader codec for 'text/html' + UsuarioRM

Stacktrace:

at Ramone.Implementation.CodecManager.GetReader(Type t, MediaType mediaType) in c:\Projects\Ramone\Ramone\Implementation\CodecManager.cs:line 84 at Ramone.Response.Decode[T]() in c:\Projects\Ramone\Ramone\Response.cs:line 58 at Ramone.Response1.get_Body() in c:\Projects\Ramone\Ramone\Response.cs:line 144 at AeCAudit.Web.Infra.IntegracaoWebServiceRM.GetRMUser(Int32 matricula) in w:\Clients\aec-audit\src\AeCAudit\AeCAudit.Web\Infra\IntegracaoWebServiceRM.cs:line 42 at AeCAudit.Repositorios.UsuariosRepositorio.GetRMUser(Int32 matricula) in w:\Clients\aec-audit\src\AeCAudit\AeCAudit.Repositorios\UsuariosRepositorio.cs:line 21 at AeCAudit.Web.Api.LegacyController.RMUser(Int32 id) in w:\Clients\aec-audit\src\AeCAudit\AeCAudit.Web\Api\LegacyController.cs:line 27 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken)

JornWildt commented 10 years ago

The problem is your server, which has nothing to do with Ramone. You may be asking for XML via AcceptXml - but it is ignored by your server which apparently sends text/html. That's not a problem I can fix for you.

Try to use ASP.NET Web API instead of using old style .asmx files: http://www.asp.net/web-api

Ridermansb commented 10 years ago

in my WebService project, it's just a project with default settings and a method for query. I made no extra configuration

I have two projects, one is the WebService (Only to simulate the WS client) Another is my project (which I access the WS and query the data).

In WS project, I created a class UsuarioRM and returned an instance of this class (generic, just to simulate the environment of the client)

In my project, I created another UsuarioRM class (with the same structure in my app UsuarioRM WS).

JornWildt commented 10 years ago

Have you tried to point your web browser to http://localhost:58592/WSDadosAeC/DadosRmService.asmx?op=ConsultaFuncionarioRm ? If it returns HTML then it might just be trying to tell you something.

Besides that: if you are implementing SOAP services using .asmx technology then Ramone cannot help you. Ramone are for lower level/bare bones web access.

Ridermansb commented 10 years ago

Another odd detail is that in my WebService project, when I run the WebService (by VS) the result is returned in XML

result

Ridermansb commented 10 years ago

I found the problem. It was exactly what you said. The correct link is: http://localhost:58592/WSDadosAeC/DadosRmService.asmx/ConsultaFuncionarioRm

and NOT: http://localhost:58592/WSDadosAeC/DadosRmService.asmx?op=ConsultaFuncionarioRm

Thanks!

JornWildt commented 10 years ago

Good :-)