简体   繁体   中英

How can I call the wcf service method?

I have a method like this in wcf service

public string PostAllSurveList(List<Survey> surveyList)
        {
            var surveyDbList = ConstructDbServeyList(surveyList);
            foreach (var survey in surveyDbList)
            {
                _db.surveys.Add(survey);
            }
            _db.SaveChanges();
            return "Successfully Saved";
        }

Now my question is How can I call this method from the client code. That means firstly I have to construct the Survey List. How can I construct this list.

string reply = client.PostAllSurveList(How can I construct this List?);

For your kind information, I am writing client code in C#.

Thanks in Advance.

var list = new List<Survey>();    
string reply = client.PostAllSurveList(list);

And you really need to make sure you know how to spell Survey, because you have 3 different spellings in 6 lines of code. Code is written language, it doesn't work if it's somewhat similar if spoken aloud.

Edit: Make sure that when you generate the client, you chose "List" as the option for any collections. It seems you chose array, which means your function now takes an array on the client side:

var list = new List<Survey>();    
string reply = client.PostAllSurveList(list.ToArray());

Create survery items and add them to a list, pass the list as parameter:

Survey survey1 = new Survey();

survey1.property1= value;
survey1.property2= value;

Survey survey2 = new Survey();

survey2.property1= value;
survey2.property2= value;

List<Survey> listSurvey = new List<Survey>();
listSurvey.add(survey1);
listSurvey.add(survey2);

string reply = client.PostAllSurveList(listSurvey);

Creare List like this and supply :

var list = new List<Survey>();
string reply = client.PostAllSurveList(list);

Edit : Update Service Reference To ObservableCollection 在此处输入图片说明

Try:

var list = new List<Survey>();
string reply = client.PostAllSurveList(list);

Or using an Collection Initializer :

string reply = client.PostAllSurveList(new List<Survey> { });

Although list should be populated elsewhere in your business logic, unless ConstructDbServeyList manipulates the variable in the method directly.

You can add to the list using the .Add() method. For instance:

list.Add(new Survey() { Property = "Property"; });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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