6
votes

I created a WCF service and to the default service I added another operation contract on the main DataContract:

[OperationContract]
void DoSomething(UserData data);

Then I have something like this (simplified for the purpose of example) below. The problem is that even though ALL classes in the hierarchy are decorated with DataContract and ALL their members decorated with DataMember, when I use the WCF Test Client it has a red icon indicating that "the operation is not supported in the WCF test client".

[DataContract]
public class UserData {
   [DataMember]
   public uint One { get; set; }

   [DataMember]
   public CompositeType Extra { get; set; }

   public UserData() { ctor. code }
}


[DataContract]
public class CompositeType {
    [DataMember]
    public uint Two { get; set; }

    public UserData() { ctor code }
}
4
I see two classes with the same name but one is missing the composite type, what do you mean by it? You also forgot to post the class of the composite type.Silvermind
I see you are talking about hierarchy. Are you implementing any kind of recursive relationship? Since that would require the class being used recursively to be decorated with [DataContract(IsReference=True)]Silvermind
@Silvermind sorry typo error, the 2nd (already corrected) was CompositeType and not UserData. I also added the IsReference parameter to the subtypes used in the main DataContract but that did not resolve the problem.Lord of Scripts

4 Answers

5
votes

OK, having gone through the whole thing (thanks to all for the tips) the solution was this:

  • IsReference attribute in DataContract was not needed at all
  • IsOneWay attribute in DataContract was not needed at all even when OperationContract was returning void.
  • KnownType was also not needed provided all the subtypes in the hierarchy were mine, in other words defined by me rather than .NET and marked with DataContract or DataMember as appropriate
  • Getting rid of OperatingSystem and building a wrapper DataContract that extracted the necesary information from OperatingSystem got around the issue.

Now there is no error in the WCF Test Client

4
votes

Add the attribute to your 'UserData' class [KnownType(typeof(CompositeType))]

Like:

[DataContract]
[KnownType(typeof(CompositeType))]
public class UserData 
{
   [DataMember]
   public uint One { get; set; }

   [DataMember]
   public CompositeType Extra { get; set; }

   public UserData() { ctor. code }
}

http://msdn.microsoft.com/en-us/library/ms730167.aspx

Edit:

http://msdn.microsoft.com/en-us/library/system.operatingsystem.aspx

The OperatingSystem class has a few properties which relate to other classes. You could include all these classes in the known types but the dependency chain could get rather large and I would highly recommend not using the Operating System class at all.

You should work out what information you actually need from the Operating System class and create your own DTO to pass back in the response. That way you can ensure all the types are easily definable on your contract.

0
votes

Does the service work if you create a test client(like a console app) and add a service reference to the wcf? If it does, then your datacontract probably has one of those types not supported by WCF Test client.

See this related issue

0
votes

The WCF default expectation for a service call is request-response - WCF expects some kind of a response back.

If you want to use void (as in: no return value), you need to decorate those methods with

[OperationContract(IsOneWay = true)]
void DoSomething(UserData data);

to tell the WCF runtime not to expect any return value from the call

Read more about WCF: Working with One-Way Calls, Callbacks and Events here in MSDN Magazine.