views:

461

answers:

5

I have several constant defined for a webservice, and I would like to make these available to consuming client. Both the server and client are .Net.

I think it is possible to achieve this by using enum's, but many of my constants are text string with spaces, so I would have to write an extra function to convert the enum into the equivalent text string.

So, is there any way of defining the constants in the web service so they would then be available to the client??

Update (Kev): I want to expose them to the client via the web service itself, not via a separate assembly. Update #2 (Paige): So if I understand you, I will then have a new List object containing the constants, but how does the client use that? Wouldn't it look like (roughly): dim constants as List = mywebservice.GetConstants() dim someresult as Integer = mywebservice.somefunction(constants(3)) Unless I misunderstand you, that totally defeats the point of defining constants.

A: 

You could implement these constants in a standalone assembly and reference the assembly from both the web service and the client.

Kev
Um...why the downvote?
Kev
IMO, for "violation" of the spirit of the question. Of course I know you can distribute a seperate asssembly, I am trying to expose them via the webservice itself. And you knew this didn't you?
tbone
You did say that both client and server are both .NET so the assumption being you have control over both client and server. Hence the suggestion.
Kev
But at least you came back and commented/explained, so I'll take it on the chin like a man. :)
Kev
Haha, good.I don't think this problem is solveable the way I want to. It can be done with enumerations, but not constants it doesn't seem.
tbone
A: 

You could write the "constants" into a dictionary and then have a web method that returns the keys.

Or, using Kev's answer above:

Class:

public class Win32Message   {
    public const int WM_ACTIVATE =0x0006;
    public const int WM_ACTIVATEAPP = 0x001C;
    public const int WM_AFXFIRST = 0x0360;
    public const int WM_AFXLAST = 0x037F;
    public const int WM_APP = 0x8000;
    public const int WM_ASKCBFORMATNAME        = 0x030C;
}

And then in the web service use a web method like:

[WebMethod]
public System.Collections.Generic.List<string> GetConstants(System.Type type)
{
    System.Collections.Generic.List<string> constants = new 
        System.Collections.Generic.List<string>();

    System.Reflection.FieldInfo[] fieldInfos = type.GetFields(
        System.Reflection.BindingFlags.Public | BindingFlags.Static |
        System.Reflection.BindingFlags.FlattenHierarchy);

    // Go through the list and only pick out the constants
    foreach (System.Reflection.FieldInfo fi in fieldInfos)
    if (fi.IsLiteral && !fi.IsInitOnly)
        constants.Add(fi.Name);

    return constants;
}

I found this on Wes' Puzzling Blog

Of course, you can return it as an array, arraylist or however you'd like.

Paige Watson
Linky to Wes's article is broken...had a look on his site for it but couldn't spot.
Kev
So if I understand you, I will then have a new List object *containing* the constants, but how does the client use that? Wouldn't it look like (roughly):dim constants as List = mywebservice.GetConstants()dim someresult as Integer = mywebservice.somefunction(constants(3))Not very useful.
tbone
Wes's article....http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx
tbone
A: 

I don't know that there's any way to expose constants per se.

But perhaps you could just implement some functions which always return the same value and just give them a special naming convention? For example:

public int FLAG_READONLY()
{
   return 3;
}

I may be misunderstanding your need.

Spencer Ruport
A: 

As i see it, the real point of webservices is being able to retrieve data or execute processes on a server in a LANGUAGE and MACHINE-independent way, using xml or json or whatever as the language for data representation. When you call a service to list products, you just want to get the products, you don't want (or need) to know if you're calling a stored SQL procedure or a C# business function in the server. So i don't think it makes sense to share 'constants' from the client to the server unless you have a web method that lists those constants to you (again, in a text-only way). I think the whole Microsoft web service .asmx stack just sucks because it relies on the 'remote procedure call' metaphor instead of the 'service' part of the web service concept.

I agree this is subjective :P

axel_c
You're wrong about the ASMX stack relying on RPC. It handles "document" style web services just fine.
John Saunders
+1  A: 

The answer seems be.....cannot be done.

tbone