C# Restful WebService Question(getting return int value)

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
This is my first semester taking C# and working on web services. I started the semester off learning SOAP, now I am learning REST. I hear REST is the more popular one to use. So far it seems harder to user to me, but I am still very new to it.

So I am trying to create restful service. Actually creating the service is working. I want to get a return int value from the service and on my client/console side store that int value into a int arrayList.

Problem is the rest service is returning the entire xml string. like this

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">0</int>

I just want the 0, so I can store that value in my arrayList. I feel like C# should have some simple library or something that allows me to just get the values in between the xml tags.

I wrote up test code to get me a better idea of how to get data from my REST services.


Here is code in the service contract of WCF(we are supposed to use WCF in this assignment, so I am using it in my test code).

[ServiceContract]
public interface IService1
{

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare)]
int getTrafficAccidentsByZip(int zCode);
[OperationContract]
[WebGet(UriTemplate = "latlonValues?lonVal={lonVal}&latVal={latVal}", BodyStyle =WebMessageBodyStyle.Bare)]
int getTrafficAccidentByLonLat(double lonVal, double latVal);



// TODO: Add your service operations here
}



Here is the implementation of the contracts. Like I said, this is test code. So I am just returning 0

public class Service1 : IService1
{
public int getTrafficAccidentsByZip(int zCode)
{
return 0;
}

public int getTrafficAccidentByLonLat(double lonVal, double latValue)
{
return 0;
}

This is what I have in main. You can see below that I tried to convert the string to int. That caused an error.



string url = @"http://localhost:14722/Service1.svc/getTrafficAccidentsByZip";


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);
// int test = Convert.ToInt32( reader);
String json = reader.ReadToEnd();
// int test = Convert.ToInt32(json);
Console.WriteLine(json);


I feel like this should be really easy. BUT I have been having a hard time finding simple examples of how to do this online.

Any good online C# REST examples or youtube videos would be greatly appreciated. WIth examples. I learn from examples.
 
Why aren't you using WebAPI 2? It's meant for REST, lol. You can do REST with WCF, but as you can see, way more painful.

Use the right tool for the job :)
 
Why aren't you using WebAPI 2? It's meant for REST, lol. You can do REST with WCF, but as you can see, way more painful.

Use the right tool for the job :)

Sadly, I am supposed to user wcf for this project.
 
Back
Top