以下是我的 WCF 服务代码的简化版本。此代码工作正常并返回我的数据。
我的问题是,这仅在我通过 ref 或 out 传递客户对象时才有效。如果我修改整个代码以便在没有 ref 或 out 的情况下传递客户对象,则 i 变量中的计数为 0。
如果 List 是一个引用变量,为什么它适用于 ref/out 而没有 ref/out 则不起作用。
另请注意,我将无法从该方法返回一个值,因为我必须返回多个值。
客户代码:
List<Customer> customers = null;
ClientProxy proxy = new ClientProxy();
proxy.GetCustomers(ref customers);
int i = customers.Count;
服务代理:
public class ClientProxy
{
public void GetCustomers(ref List<Customer> customers)
{
INWGetCustomers proxy = new ChannelFactory<INWGetCustomers>("netNamedPipeBinding").CreateChannel();
proxy.GetCustomers(ref customers);
}
}
服务合同和数据合同:
[DataContract]
public class Customer
{
[DataMember]
public System.String CustomerId;
[DataMember]
public System.String CompanyName;
}
[ServiceContract(Namespace = "http://www.temp.com")]
public interface INWGetCustomers
{
[OperationContract()]
void GetCustomers(ref List<Customer> customers);
}
服务代码:
public class NWGetCustomersService : INWGetCustomers
{
public void GetCustomers(ref List<Customer> customers)
{
customers = new List<Customer>();
customers.Add(new Customer() { CustomerId = "1", CompanyName = "A" });
customers.Add(new Customer() { CustomerId = "2", CompanyName = "B" });
customers.Add(new Customer() { CustomerId = "3", CompanyName = "C" });
customers.Add(new Customer() { CustomerId = "4", CompanyName = "D" });
}
}