Mehmet Keles

Mehmet Keles

  • NA
  • 4
  • 3.5k

RESTFUL WCF service in Windows Phone 8.1

Nov 18 2014 3:55 AM
Hello everyone,
 
   I am realy new developt windows phone app. I would like to connect remote SQL server via RESTFUL WCF service.
I can take a table from the server but i can not insert a row it. I tried many variation about Webget/Webinvoke such as belong 
 
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetData",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
List<tblKullanicilar> GetData();
[OperationContract]
[WebGet(UriTemplate = "AddData/{KulAdi}/{Sifre}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string AddData(string KulAdi,string Sifre);
} 
 
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "AddData",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string AddData(kullanici kul);
 
 
 
 
 
public class Service1 : IService1
{
public DataClasses1DataContext data { get; set; }
public List<tblKullanicilar> GetData()
{
data = new DataClasses1DataContext();
return data.tblKullanicilars.ToList();
}
public string AddData(string KulAdi, string Sifre)
{
tblKullanicilar gonder = new tblKullanicilar();
gonder.KulAdi = KulAdi;
gonder.Sifre = Sifre;
data.tblKullanicilars.InsertOnSubmit(gonder);
data.SubmitChanges();
try
{
data.SubmitChanges();
return "tamam";
}
catch (Exception e)
{
return e.ToString() ;
}
}
}
 
 
And i call these service like below,
 
 First one is GetData which is get full table 
 
private async void CallService()
{
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://10.215.0.162:18901/Service1.svc/GetData");
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
var sonuc = JsonConvert.DeserializeObject<Kullanici[]>(data);
listbox1.ItemsSource = sonuc;
}
 
 second one is insert method but this one is not working,
 
 
private async void CallService1()
{
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://10.215.0.162:18901/Service1.svc/AddData/" + adi.Text + "/" + sifre.Text);
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
}
 
other  trying 
 
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://10.215.0.162:18901/Service1.svc/AddData/" + adi.Text + "/" + sifre.Text);
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
//Frame.Navigate(typeof(MainPage));
 
and other  
 
var kullanici = new kullanici() { KulAdi = adi.Text, Sifre = sifre.Text };
var serialized = JsonConvert.SerializeObject(new { obj = kullanici });
var httpClient = new HttpClient();
var request = new StringContent(serialized, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://10.215.0.162:18901/Service1.svc/AddData/", request);
string content = await response.Content.ReadAsStringAsync();
 
 
I didn't find what is my real problem. Please can you help me, or anyone have a sample code about restful wcf service which is insert to sql server
sorry my english
 
Thank you