2011-01-07 24 views
7

我在WebApplication项目中创建了一个简单的WCF服务。WCF:不支持带输出参数的操作

[ServiceContract(Namespace = "http://my.domain.com/service")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService 
{ 
    [OperationContract] 
    public string PublishProfile(out string enrollmentId, string registrationCode) 
    { 
     enrollmentId = null; 
     return "Not supported"; 
    } 

建 - 一切成功编译

那之后我试图在浏览器中打开的服务,我有以下错误:

Operation 'PublishProfile' in contract 'MyService' specifies an 'out' or 'ref' parameter. Operations with 'out' or 'ref' parameters are not supported

我不能用' out'参数?

这里有什么问题?

谢谢

P.S.我使用VS2008 SP1,.NET 3.5

回答

4

在我的情况下,问题是我的ASP.NET应用程序与Visual Studio向导中创建的默认服务配置是一种服务类型。端点绑定是“webHttpBinding”。据我现在了解,它对REST服务具有约束力,而且他们没有物理能力来处理输出参数。对于它们而言,参数不受支持。而我真正需要的是一个“basicHttpBinding的”允许与out参数工作。

非常感谢大家谁帮我弄清楚这一点。

0

我认为Out参数应该来之后。

它应该是这样的:

public string PublishProfile(string registrationCode, out string enrollmentId) 

而且,你的字符串设定为null - 为什么不使用string.Empty

+0

那只是一个很短...但也许我需要知道更多为什么string.Empty更好 – Budda 2011-01-07 22:14:43

+0

更改'outed'参数的位置并没有帮助。 – Budda 2011-01-07 22:25:28

+0

http://codeasp.net/forums/asp-net-topics/getting-started-general-asp-net/117/ what-is-difference-between-null-vs-string-empty-vsin-c对于`string.empty`有一个很好的答案。关于你原来的问题,我真的难住你为什么会出错... – VoodooChild 2011-01-08 05:36:51

2

我找到的答案是:

“out参数的想法是,该方法将实例,你在通过空引用Web服务是无状态的,因此对于作为参数进入web服务的对象所拥有的句柄与进入web服务服务器端的句柄并不相同,这样可以防止出现参数。“

Source

1

试试这个:

... 
[OperationContract] 
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] 
public string PublishProfile(out string enrollmentId, string registrationCode) 
... 

我相信默认的车身风格(裸)只支持一个返回值。