WCF: operations with out parameters are not suppor

2019-03-27 17:25发布

I've created a simple WCF service inside of WebApplication project.

[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";
    }

built - everything is compiled successfully

After that I've tried to open service in browser, I've got the following error:

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

Can't I use 'out' parameters?

What is wrong here?

Thanks

P.S. I use VS2008 SP1, .NET 3.5

标签: .net rest wcf
5条回答
混吃等死
3楼-- · 2019-03-27 17:59

Try this:

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

I believe the default body style (bare) only supports a single return value.

查看更多
仙女界的扛把子
4楼-- · 2019-03-27 18:03

I think the Out parameter should come after.

it should be like this:

public string PublishProfile(string registrationCode, out string enrollmentId)

Also, you are setting the string to null - why not use string.Empty?

查看更多
Bombasti
5楼-- · 2019-03-27 18:10

The problem in my case was that default service configuration created in my ASP.NET application with Visual Studio wizard was a service type. Endpoint binding was "webHttpBinding". As far as I understand now it is binding for REST services, and they just don't have physical ability to work with out parameters. For them out parameter is not supported. And what I actually needed was a 'basicHttpBinding" that allows to work with out parameters.

Many thanks to everybody who helped me to figure out that.

查看更多
贼婆χ
6楼-- · 2019-03-27 18:10

The answer I've found was:

"The idea of an out parameter is that the method will instantiate the null reference that you pass in. A web service is stateless; therefore the handle that you have on an object that goes into a webservice as a parameter will not be the same as the one that makes it into the webservice server side. The nature of this prevents out parameters."

Source

查看更多
登录 后发表回答