【问题标题】:C# Web Service with a reference parameter带有引用参数的 C# Web 服务
【发布时间】:2013-04-05 03:20:10
【问题描述】:

我必须创建一个 C# Web 服务。我有个问题。可以使用ref 参数吗? 比如我有这个方法

//my web service will fill the parameter by reference
int myWSMethod(int parameterA, ref string parameterB);

这可以通过网络服务实现吗?

【问题讨论】:

  • 你为什么不将信息作为常规参数传递,你能解释一下为什么你认为你需要一个参考参数吗?看看这个SO张贴stackoverflow.com/questions/379648/…跨度>
  • 当您通过 Web 服务交换变量时,它会被序列化。你希望ref 做什么?请解释您要解决的真正问题。
  • @DJ KRAZE 我想使用这种类型的参数,因为我想要一个返回错误代码(return int)的方法,而且还有一个报告(ref 参数)的 web 服务执行跨度>
  • @GgSalent 更好的方法是返回字符串值并在错误时抛出自定义异常,而不是使用ref 参数
  • @DStanley 不,请参阅 google 的、twitter 的等 API。所有返回和对象都包含status 和实际数据。所以EkoostikMartin的解决方案很好。

标签: c# web-services visual-studio pass-by-reference


【解决方案1】:

如果您的问题只是想弄清楚如何从 Web 服务返回多个值,只需返回一个复杂类型即可。

[DataContract]
[Serializable]
public class myWSMethodResponse
{
    [DataMember]
    public int ErrorCode { get; set; }
    [DataMember]
    public string Report { get; set; }
}

public myWSMethodResponse myWSMethod(int parameterA)
{
  //code here
}

【讨论】:

  • 虽然不是 OP 所要求的,但这绝对是最明智的解决方案。
【解决方案2】:

我不确定你为什么要这样做,但根据MSDN,你可以这样做。

OutRef 参数

在大多数情况下,您可以使用 in 参数(在 Visual Basic 中为ByVal)和outref 参数(ByRef 在 Visual Basic 中)。因为outref 参数都表示 该数据是从操作返回的,操作签名如下 指定即使操作签名返回void,也需要请求/回复操作。

示例:

[ServiceContractAttribute]
public interface IMyContract
{
  [OperationContractAttribute]
  public void PopulateData(ref CustomDataType data);
}

【讨论】:

    猜你喜欢
    • 2017-03-10
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多