QazPhilby
QazPhilby

Reputation: 117

Excluding DataContract Name from return JSON

I have the following DataContract in my WCF service

namespace MyNamespace
{
    [DataContract(Namespace = Constants.ContractNamespace)]
    public class MyResponse
    {
        [DataMember]
        public string ResponseCode { get; set; }

        [DataMember]
        public List<MyObject> MyObjects  { get; set; }

    }
}

And when I then use that as a return type from one of my OperationContracts like this

[OperationContract]
MyResponse ValidateById(string id);

I then get the following response

{"MyResponse":{"MyObjects":null,"ResponseCode":"ERR001"}}

is there any way to make this just return the properties so it would be like

{"MyObjects":null,"ResponseCode":"ERR001"}

Update: I am also using transportClientEndpointBehavior

<endpointBehaviors>
    <behavior name="EndpointBehavior_IMyService">
      <transportClientEndpointBehavior>
        <tokenProvider>
          <sharedAccessSignature keyName="RootManageSharedAccessKey" key="keyhere" />
        </tokenProvider>
      </transportClientEndpointBehavior>
    </behavior>
  </endpointBehaviors>

and a netTcpRelayBinding

<netTcpRelayBinding>
    <binding name="NetTcpRelayBinding_IMyService"/>
  </netTcpRelayBinding>

Upvotes: 0

Views: 90

Answers (2)

QazPhilby
QazPhilby

Reputation: 117

Turns out the issue was with my data contract.

I showed the wrong one above it is actually

   [DataContract(Name = "MyResponse", Namespace = Common.Constants.ContractNamespace)]
public class MyValidateModel
{
    [DataMember]
    public MyResponse MyResponse { get; set; }
}

but it needs to be

   [DataContract(Name = "MyResponse", Namespace = Common.Constants.ContractNamespace)]
public class MyValidateModel : Common.MyResponse
{
}

Upvotes: 0

Ronak Patel
Ronak Patel

Reputation: 2610

You can use Body Style attribute to set wrapped response or not from your config file. e.g.

<endpointBehaviors>
    <behavior name="WebWithDefaults">
        <webHttp defaultOutgoingResponseFormat="Json"
                 defaultBodyStyle="Bare" />
    </behavior>
</endpointBehaviors>

Above code assumes webHttp protocol. You can change it to yours

Upvotes: 1

Related Questions