.NET Web服务和输出/ REF的WebMethod参数 [英] .Net WebServices and out/ref WebMethod arguments

查看:115
本文介绍了.NET Web服务和输出/ REF的WebMethod参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经收到了一些文件,从我们的供应商,因为他们要发布一个Web服务中的一个,他们是非常具体的,关于他们的WebMethods的一个参数有out修饰符(?不知道这是正确的描述)比如考虑下面的WebMethod签名:

I've received some documentation from one of our suppliers for a webservice they're publishing and they're very specific that on one of their WebMethods that an argument has the out modifier(? not sure if that's the right descriptor) for instance consider the following WebMethod signature:

[WebMethod]
public void HelloWorld(out string strVal) 
{ 
    strVal = "Hello World";
}

[显然实际方法不是一个Hello World方法]

[Obviously the actual method isn't a Hello World method]

现在,我从来没有考虑设计带有OUT / REF参数一个WebMethod,这让我不知道他们为什么会一直用它。

Now, I'd never considered designing a WebMethod with an out/ref argument and it got me wondering why they would've used it.

试图了解这一设计决定我扔的原型加上一些基本的Hello World风格的webMethods ......一用一单出字符串参数,一拖二出字符串参数和一个不接受任何应用程序论点,但返回一个字符串。

Trying to understand an application for this design decision I threw a prototype together with a few basic Hello World style webmethods...one with a single out string argument, one with two out string arguments and one that doesn't receive any arguments but returns a string.

当试图引用我的webMethods从一个单独的应用程序,我注意到,我访问了法与单出字符串参数完全一样,如果我定义的方法来输出字符串,这样实际上就作为客户端关注:

Upon trying to reference my webmethods from a separate application I notice that I have to access the method with the single out string argument exactly as if I'd defined the method to output the string so that in effect as far as the client is concerned:

public string HelloWorld1()
{
  return "Hello World";
}

public void HelloWorld2(out string strVal)
{
  strVal = "Hello World";
}

是完全一样的......在我不得不引用它们既是这样[其中x代替正确的方法]:

are exactly the same...in that I have to reference them both as such [where x is substituted for the correct method]:

string val = HelloWorldX();

已经试图引用我想访问他们的方式方法,如果他们不是Web方法[像这样]:

Having attempted to reference the methods in the way I would access them if they weren't web methods [like so]:

string val = string.Empty;
MyService1.HelloWorld(out val);
Console.WriteLine(val);

这会导致编译错误,指出没有任何方法的参数接受1输入。这是为什么?有显然是接受一个参数一个Web方法 - 我看着它[HelloWorld2]

which causes a compilation error stating that no method arguments accept 1 input. Why is that? There's obviously a web method that accepts one argument - I'm looking at it [HelloWorld2].

当检查SOAP响应,我注意到,对于HelloWorld1响应的内容是:

Upon examining the SOAP responses, I notice that the content of the response for HelloWorld1 is:

<HelloWorld1Response xmlns="http://tempuri.org/">
  <HelloWorld1Result>string</HelloWorld1Result>
</HelloWorld1Response>

和HelloWorld2是

And HelloWorld2 is

<HelloWorld2Response xmlns="http://tempuri.org/">
  <strVal>string</strVal>
</HelloWorld2Response>

再进一步​​我想,如果我有2个裁判争论......

Going a step further I thought, what if I have 2 ref arguments...

public void HelloWorld3(out string strVal1, out string strVal2)
{
    strVal1 = "Hello World";
    strVal2 = "Hello World Again!";
}

这产生了SOAP内容:

This generates the SOAP content:

<HelloWorld3Response xmlns="http://tempuri.org/">
  <strVal1>string</strVal1>
  <strVal2>string</strVal2>
</HelloWorld3Response>

我觉得不够公平,所以理论上[提供我可以想出一个办法来传递出/ REF参数的WebMethods],这意味着我可以传递,可以通过该方法设置两个参数,但是当我这样做:

I thought fair enough, so theoretically [providing I can figure out a way to pass out/ref arguments to WebMethods] that means I can just pass in two arguments that can be set by the method, but when I do this:

string val1 = string.Empty;
string val2 = string.Empty;
MyService1.HelloWorld3(out val1,out val2);
Console.WriteLine(val1);
Console.WriteLine(val2);

我应该得到相同的编译错误,我看到,当我试图引用HelloWorld2这种方式。随着明显的例外,它的抱怨2个参数,而不是1 [事实上我得到了同样的异常,我测试了它。

I should get the same compilation error I saw when I tried to reference the HelloWorld2 this way. With the obvious exception that it's complaining about 2 arguments instead of 1 [and in fact I do get the same exception, I tested it].

  • 怎么办?
  • 还有一个原因或一种使用在的WebMethods,我很想念/ REF参数呢?
  • 如果有,我怎么引用的WebMethods多出/ REF参数?

推荐答案

呃......我不知道该协议是提供解答自己的问题,而是由史蒂芬贝肯引用的文章提供了一些线索,我演绎的解决方案这种奇怪的情况,而不是留给其他人来弄清含义是,我想我会分享我的发现...

Er... I dunno what the protocol is for providing answers to your own questions but the article referenced by Steven Behnke provided some clues for me to deduce a solution to this bizarre situation, and rather than leave everyone else to figure out what the implications are, I thought I'd share my findings...

因此​​,考虑我的web服务定义了以下的webMethods

So, consider the following webmethods defined in my WebService

[WebMethod]
public string Method1()
{
    return "This is my return value";
}

[WebMethod]
public void Method2(out string strVal1)
{
    strVal1 = "This is my value passed as an output";
    //No return value
}

[WebMethod]
public void Method3(out string strVal1, out string strVal2)
{
    strVal1 = "This is my strVal1 value passed as an output";
    strVal2 = "This is my strVal2 value passed as an output";
    //No return value
}

[WebMethod]
public string Method4(out string strVal1, out string strVal2)
{
    strVal1 = "This is my strVal1 value passed as an output";
    strVal2 = "This is my strVal2 value passed as an output";
    return "This is my return value";
}

现在......根据该文件,第一个参数定义为停止,如果该方法返回无效,则第一参数被自动用作返回参数。所以,我会按如下方法访问我的每一个方法:

Now...according to the document, the first parameter defined as Out, if the method returns void, then the first parameter is automatically used as the return parameter. So I would access each of my methods as follows:

方法一:公共字符串方法1(){}

Method1: public string Method1() {}

var str = svc.Method1();
Console.WriteLine(str);

方法二:公共无效方法2(出字符串strVal1){}

Method2: public void Method2(out string strVal1) {}

var str = svc.Method2();
Console.WriteLine(str);

所以,你访问它们无论是在完全相同的方式......这是非常令人困惑。到底是谁也明白这一点,而不必被告知别人?这超出了我的COM prehension如何,这可能是个好主意......

So you access them both in exactly the same manner...which is extremely confusing. Who on earth would figure that out without having been told that by someone else? It's beyond my comprehension how this could be a good idea...

Method3:公共无效Method3(出字符串strVal1,出字符串strVal){}

Method3: public void Method3(out string strVal1, out string strVal) {}

var str2 = String.Empty;
var str1 = svc.Method3(out str2);
Console.WriteLine(str1);
Console.WriteLine(str2);

Method4:公共字符串Method4(出字符串strVal1,出字符串strVal2){}

Method4: public string Method4(out string strVal1, out string strVal2) {}

var str1 = String.Empty;
var str2 = String.Empty;
var str3 = svc.Method4(out str1, out str2);
Console.WriteLine(str1);
Console.WriteLine(str2);
Console.WriteLine(str3);

所以,当你发现 - 如果方法签名不提供返回值[即返回void],那么第一个参数就返回值。如果它已经提供了一个返回值,那么它没有。

So as you notice - if the method signature doesn't provide a return value [that is returns void], then the first param becomes the return value. If it already provides a return value, then it doesn't.

这非常令人迷惑的人还没有遇到那个文件。非常感谢提供的链接史蒂芬 - 我真的AP preciate它

This can be extremely confusing for someone that hasn't come across that document. Many thanks for providing that link Steven - I really appreciate it.

...并给谁就给谁决定设计模式是一个好主意,被写入.NET框架 - 我不认为你会一直posessed你认为这是一个好主意......我真的不喜欢你很强烈毕竟这一点。

...and to whomever decided that design pattern was a good idea to be written into the .NET Framework - I can't think what would've posessed you to think that was a good idea... I really dislike you quite intensely after all that.

附录:

ADDENDUM:

我才刚刚意识到的是,要增加混乱,如果你使用的 REF 的而不是的,那么你的的做这一点,你会治疗的WebMethods完全一样,你会如果你用它们来调用一个普通的方法你的应用程序中:

What I only just realised is that to add to the confusion, if you use ref instead of out then you don't do this, you'd treat the WebMethods exactly as you would have if you'd used them to call a regular method inside your application:

[WebMethod()]
public void Method3(ref string strVal1, ref string strVal2)
{
    strVal1 = "First argument return value";
    strVal2 = "Second argument return value";
}

现在打电话,你会使用:

Now to call that you'd use:

string val1 = String.Empty;
string val2 = String.Empty;
svc.Method3(ref val1, ref val2);
Console.WriteLine(val1);
Console.WriteLine(val2);

这不一致性mindboggling ......事实上,它的设计是INCOM prehensible给我。

This inconsistency is mindboggling...the fact that it's by design is incomprehensible to me.

这篇关于.NET Web服务和输出/ REF的WebMethod参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆