Search code examples
c#wcfrestdatacontract

Consume WCF Restful service with datacontract


I have created the following restfull web service:

Interface

[ServiceContract]
public interface ISIGService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "GetTicket/")]
    Ticket GetTicket(string user, string pwd);
}

Implementation

public class SIGService : ISIGService
{
    public Ticket GetTicket(string user, string pwd)
    {
        return new Ticket()
        {
            Usuario = "xx",
            UsuarioNombre = "xxx",
            UsuarioId = "xxx"
        };
    }

Contract

[DataContract]
public class Ticket
{
    [DataMember]
    public int UsuarioId { get; set; }

    [DataMember]
    public string UsuarioNombre { get; set; }

    [DataMember]
    public string Usuario { get; set; }
}

I need to consume this service, from a web application, and get the typed object Ticket, I have included a service reference for this.

Server side code:

string urlService = 
    String.Format("http://localhost:22343/SIGService.svc/GetTicket/?user='{0}'&pwd='{1}'", 
                 usuario, password);

var request = (HttpWebRequest)WebRequest.Create(urlService);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);

string text = reader.ReadToEnd();

I put a text variable just to get something, sort of lost here.

I don't seem to get this object, could you give some pointers on this?


Solution

  • Most likely, you just need to change your URL from

    http://localhost:22343/SIGService.svc/GetTicket/?user='{0}'&pwd='{1}'
    

    to using the proper REST syntax (since you're using a REST service):

    http://localhost:22343/SIGService.svc/GetTicket/{user}/{pwd}
    

    Sample:

    http://localhost:22343/SIGService.svc/GetTicket/daniel/topsecret
    

    No ? or user= or single quotes necessary ....

    With this, the value from {0} will be passed into the user parameter, and the value from {1} to the pwd parameter.

    For consuming the service, I would recommend you check out the excellent RestSharp library which makes using your REST service a breeze.

    Your code would look something like this:

    // set up the REST Client
    string baseServiceUrl = "http://localhost:22343/SIGService.svc";
    
    RestClient client = new RestClient(baseServiceUrl);
    
    // define the request
    RestRequest request = new RestRequest();
    request.Method = Method.GET;
    request.RequestFormat = DataFormat.Xml;
    request.Resource = "GetTicket/{user}/{pwd}";
    request.AddParameter("user", "daniel", ParameterType.UrlSegment);
    request.AddParameter("pwd", "top$ecret", ParameterType.UrlSegment);
    
    // make the call and have it deserialize the XML result into a Ticket object
    var result = client.Execute<Ticket>(request);
    
    if (result.StatusCode == HttpStatusCode.OK)
    {
        Ticket ticket = result.Data;
    }