tags:

views:

292

answers:

5

I want to expose an enum to my client application without referencing it in my WCF Service. However the enum is not visible in the client application. Below is my code:

[DataContract]
public enum Columns
{
    [EnumMember]
    Column1= 0,
    [EnumMember]
    Column2= 1
}

[ServiceKnownType(typeof(Columns))]
public interface IService
{
    [OperationContract]
    Response GetObjects(Request request);
}

Please let me know what am I doing wrong?

+1  A: 

The Columns enumeration needs to be a property of either Request or Response classes as these are the only types included in the WSDL. The types that are exposed in the WSDL and thus visible to the client are only those that are used as input or output parameters (and all that are part of their class hierarchy) to the operation contract methods. So for example you could add the enum to the Request class:

[DataContract]
public class Request
{
    [DataMember]
    public Columns Columns { get; set; }

    // ... some other properties
}
Darin Dimitrov
A: 

The ServiceKnownType attribute is designed to provide knowledge about subclasses not directly referenced. For instance, if you wanted a InheritedRequest class (which inherits from Request) to be able to be used in your "GetObjects" method.

Rich
A: 

Remove the [DataContract] from on top of the enum.

you only use [DataContract] and [DataMember] on the enum if you want some of the elements in the enum to be available to clients of the service, but not all. if you want all members of the enum, you leave it untagged.

Mike Jacobs
no, don't - the [DataContract] is quite needed - but it's not complete - we also need to know how "Request" and "Response" look like - those ought to be using the "Columns" type in some ways....
marc_s
+1  A: 

The metadata of a service is meant to describe the types that are necessary in order to interact with the service - the request and response messages. You should make no attempt to include random types in the service metadata.

If you want to provide some random type to your clients, then the clients need to reference the type library that contains the types.

John Saunders
A: 

I don't know any way to force your enum to appear in your WSDL when it isn't used by the service itself.

But if you maintain both the client and the server, you can use a shared library that contains types used by client and server.

In our projects, we don't use WSDL at all, we use the ChannelFactory to create client proxies and reference the interface from a shared dll.

chris166