Reputation: 1046
I am developing a scenario in which i have to insert the records in xml file using the WCF Rest Service.
My Interface:
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/InsertData/")]
string InsertData(string Name, string Email, string Category, string Mobile, string Message);
}
}
My Class:
public string InsertData(string Name, string Email, string Category, string Mobile, string Message)
{
string file = AppDomain.CurrentDomain.BaseDirectory + "\\DataFile.xml";
DataTable dtUser = ReadXML(file);
DataRow dr = dtUser.NewRow();
dr["Name"] = Name;
dr["Email"] = Email;
dr["Category"] = Category;
dr["Mobile"] = Mobile;
dr["Message"] = Message;
dtUser.Rows.Add(dr);
dtUser.WriteXml(file);
return "Success";
}
public DataTable ReadXML(string file)
{
//create the DataTable that will hold the data
DataTable table = new DataTable("User");
//create the table with the appropriate column names
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Email", typeof(string));
table.Columns.Add("Category", typeof(string));
table.Columns.Add("Mobile", typeof(string));
table.Columns.Add("Message", typeof(string));
try
{
//open the file using a Stream
if (File.Exists(file))
{
using (Stream stream = new FileStream(file, FileMode.Open,
FileAccess.Read))
{
//use ReadXml to read the XML stream
table.ReadXml(stream);
//return the results
}
}
return table;
}
catch (Exception ex)
{
return table;
}
}
Now Is it necessary to pass all these parameters in the browser URL like in the following code:
UriTemplate = "/InsertData/{Name}/{Email}/{Category}/{Mobile}/{Message}/"
Or is there any way?
Upvotes: 0
Views: 2379
Reputation: 862
In this case, you have two way. Either create overload functional for "InsertData" method or in your function write code to insert only those value which is passed through parameter and for other value pass default value.
below is example of call service method via web browser (http)
...localhost/pricedataservice/DataService.svc/web/GetSnapshot?symbol=vod.l&nocache=1...
OR
...localhost/pricedataservice/DataService.svc/web/GetSnapshot?symbol=vod.l...
$j.ajax({
cache: false,
url: URL,
data: "{}",
type: "GET",
async: false,
//jsonpCallback: "Success",
contentType: "application/json",
dataType: "json",
error: function (request, error) {
alert("GetDividendData - " + error);
},
success: function (data) {
}
});
If you call your WCF service through HTTP (web browser) then it's not mandatory to pass value for each parameter but if add reference of service in your project and call your service method through service object then you have pass all parameter(here, function overloading will help you)
Below is example of calling service method via adding reference of service in your project
DataService.DataServiceClient objDataServiceClient = new DataService.DataServiceClient();
//Get data from service
objSnapshotData = objDataServiceClient.GetSnapshot(ticker, nocache);
Upvotes: 1