简体   繁体   English

在WCF REST服务中传递参数

[英]Passing Parameters in WCF REST Service

I am developing a scenario in which i have to insert the records in xml file using the WCF Rest Service. 我正在开发一种方案,其中必须使用WCF Rest Service将记录插入xml文件中。

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: 现在是否需要像下面的代码一样在浏览器URL中传递所有这些参数:

UriTemplate = "/InsertData/{Name}/{Email}/{Category}/{Mobile}/{Message}/"

Or is there any way? 还是有什么办法?

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. 为“ InsertData”方法创建重载函数,或者在函数的编写代码中创建插入函数,以仅插入那些通过参数传递的值,而对于其他值则传递默认值。

below is example of call service method via web browser (http) 以下是通过网络浏览器(http)进行呼叫服务的方法示例

...localhost/pricedataservice/DataService.svc/web/GetSnapshot?symbol=vod.l&nocache=1... ...本地主机/priceataservice/DataService.svc/web/GetSnapshot?symbol=vod.l&nocache=1 ...

OR 要么

...localhost/pricedataservice/DataService.svc/web/GetSnapshot?symbol=vod.l... ...本地主机/priceataservice/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) 如果通过HTTP(Web浏览器)调用WCF服务,则不必为每个参数传递值,但是如果在项目中添加服务的引用并通过服务对象调用服务方法,则您将传递所有参数(此处为函数重载)会帮助你)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM