简体   繁体   English

如何调用wcf服务方法?

[英]How can I call the wcf service method?

I have a method like this in wcf service 我在wcf服务中有这样的方法

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#. 对于您的信息,我正在用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. 确实需要确保知道如何拼写Survey,因为在6行代码中有3种不同的拼写。 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 : 像这样的Creare List并提供:

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

Edit : Update Service Reference To ObservableCollection 编辑:更新服务引用到ObservableCollection 在此处输入图片说明

Try: 尝试:

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

Or using an Collection Initializer : 或使用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. 尽管应该在您的业务逻辑中的其他地方填充list ,除非ConstructDbServeyList可以直接在方法中操作变量。

You can add to the list using the .Add() method. 您可以使用.Add()方法添加到列表中。 For instance: 例如:

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

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

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