1

Okay, I've got some sample code that I'm supposed to be going through to use an example for how to use a product...and I've got this code:

protected void checkout_Click(object sender, EventArgs e)
{
    OurWebServiceClient client = new OurWebServiceClient();
    this.session = client.BindAccount(ref this.session);
    client.FinalizeSession(this.session);
    client.Close();
    this.checkout.Text = "success";
    this.checkout.Enabled = false;
}

So this code is a bit strange to me for various reasons, but the line that stands out to me is the second line in the method:

this.session = client.BindAccount(ref this.session);

It's a ref, so this.session could actually point to a different object after the call. Except then they assign this.session to the value returned from the call, blowing away (as far as I can tell) anything they gained by having it as a ref. And what does it mean to have a ref variable passed into a web service anyway?

4

1 回答 1

0

该 Web 服务返回的任何内容都将分配给this.session. ref 的使用丢失了,因为this.session它被分配回了返回值。

通过将参数作为 ref 传递给 Web 服务,您可以获得任何其他方法调用所期望的功能,即您对该参数的更改会被反映回来。但是 IMO 这不是编写 Web 服务的好习惯。这很令人困惑。

于 2014-05-22T12:57:34.807 回答