views:

36

answers:

1

Hi!

I don’t know if it’s possible, but I want to be able to refer to enums from my WCF service on the client side. I have one core project, and in that project the enums are:

public enum StatusType
{

    Ok = 1,

    Error = 2, 
    Unknown = 0

}

public enum DirectionType
{
    None = 0,
    ToSystem = 1,
    FromSystem = 2
}

I have one Service project using the core project and it is setting the enum types from the core project likes this:

[DataContract()]
static class EnumHelper
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
        List<Type> knownTypes = new List<Type>();

        // Add any types to include here.
        knownTypes.Add(typeof(StatusType));
        knownTypes.Add(typeof(DirectionType));
        return knownTypes;
    }
}

And in the interface :

[ServiceKnownType( typeof(EnumHelper))]
[ServiceContract( SessionMode =  SessionMode.Allowed)]
public interface HandlerService

when I call a method either who takes or returns an enum, it works fine, but I then have to refer to the core project in the client project to use the enums client side, I would want to do that from the Service if it’s possible.

I have tried to set the enums in the core project to

[DataContract]
public enum StatusType
{
    [EnumMember]
    Ok = 1, /*!<Done with no error */
    [EnumMember]
    Error = 2, /*!<Done with error */
    [EnumMember]
    Unknown = 0, /*!<No data registered, default value */
}

with no effect.

I want to use it like this in my client project:

Either like client.StatusType.Ok or Servicereference1.StatusType.Ok or something like that, note like Core.StatusType.Ok

The reason I want this, is because the Service should be used in different projects, and we don’t want everyone to be dependent on a common dll liberary, if it’s possible to skip it. I use net.tcp binding for the service. Hope it was understandable, thanks for any help :)

A: 

If you want to share types and classes between server and client, you have to put them into a separate assembly, and use that on both the server and the client side. This only works if you control both ends of the wire, e.g. write both the server and the client side of the code (which I believe you are).

If you create a separate MyWCFTypes assembly on the server side, you can reference that assembly in your client projects as well, and when importing the service definition, WCF should reuse existing classes, e.g. should reuse your MyWCFTypes classes without creating new classes for the same enums.

marc_s
Thanks for the answer. We thought about having it in a separate assembly, and it would work fine for now, since as you mentioned, we control both the server and the client. But in the future the service may be made fully public, and then we don’t want the other clients who use the service to be dependent on a separate assembly for the types. Is that possible?
WcfEnum
@WcfEnum: no, if you can't share the common assembly, there's nothing you can do to enable the clients to use the server types. They'll get their own separate copies of the types when adding the service reference.
marc_s