WebAPI compatibility with DefaultBodyStyle Wrapped

Andreas Welte 1 Reputation point
2021-06-04T09:08:16.37+00:00

I am trying to migrate a C# 4.6 WCF service to a ASP.Net Core 5.0 WebAPI.
In the old WCF code we used WebMessageBodyStyle.Wrapped as DefaultBodyStyle in the WebHttpBehavior.

So all request and response messages are wrapped in a object with the method name.
ResultObject Method1(string param1)
class ResultObject
{
string Parameter1;
int ConnectionTimeoutSeconds;
}

The above method would result in this answer JSON:

{
"Method1Result": {
"Parameter1": "b8aef61d-af18-4985-a239-182ce22cd6d5",
"ConnectionTimeoutSeconds": 600
}
}

My test WebAPI server sends this JSON answer:
{
"Parameter1": "b8aef61d-af18-4985-a239-182ce22cd6d5",
"ConnectionTimeoutSeconds": 600
}

Is there a possible way to get the same behavior with WebAPI?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-06-04T15:05:33.237+00:00

    Yes you can define a class, instance it as a object and send it back to the client-side program. The WebAPI will Jason serialize the object and send it. The client program will have to deserialize the Jason object back into a .NET object.The class must be known by the client program and WebAPI.

    I can't say that what you have implemented in a WCF solution is applicable to a WebAPI.

    0 comments No comments