WCF: operations with out parameters are not suppor

2019-03-27 17:42发布

问题:

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

回答1:

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.



回答2:

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



回答3:

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:

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?



回答5:

See Designing WCF interface: no out or ref parameters



标签: .net rest wcf