views:

599

answers:

3

I'm building an ASP.NET 3.5 application and would like to expose an enum I have in my WebService class without using it as a parameter in once of my WebMethods.

I have a really simple example to illustrate what I want... Let's say I have the following WebService (I'll call it Agent.asmx):

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")&gt; _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Agent
    Inherits System.Web.Services.WebService

<WebMethod(EnableSession:=True)> _
Public Function DoSomething(ByVal doWhat As WhatEnum) As Boolean
    Return ...
End Function

End Class

Public Enum WhatEnum
    AskStackOverflowQuestion
    PhoneAFriend
    FiftyFifty
End Enum

Because I used the Enum in one of the WebMethods, it will output the following in the JavaScript in generates for the Agent when the WebService is referenced in my .aspx page (from http://localhost/Agent.asmx/js):

if (typeof(SomeNamespace.WhatEnum) === 'undefined') {
    SomeNamespace.WhatEnum = function() { throw Error.invalidOperation(); }
    SomeNamespace.WhatEnum.prototype = { AskStackOverflowQuestion: 0,PhoneAFriend: 1,FiftyFifty: 2}
    SomeNamespace.WhatEnum.registerEnum('SomeNamespace.WhatEnum', true);
}

Now I can go and use these enums wherever I want on the client side. Now, that's all fine and dandy, but I have another Enum in my Agent that I don't actually use in any of the WebMethods, which I would also like to be automatically exposed in the Agent's JavaScript. Is there a way, perhaps an attribute or something I could use, to cause it to be included?

If not, my fallback solution is to just create a dummy method with my Enum as a parameter so that it will be available, and simply never call it from the JS, though I'd rather not litter the code. Thanks!

A: 

Do you at least use the enum in the service? Otherwise, it doesn't belong there.

I belive you should look at the [XmlInclude] attribute.

John Saunders
The enum is used in the service; it's passed back to the client as a property on a class that is serialized and returned as JSON.
Cory Larson
So, if you just returned that class normally, the enum would be there, right? Did [ScriptService] not work?
John Saunders
I tried XmlInclude, I guess that's only for WSDL, and you have to create a dummy class wrapper for the Enum because it won't work directory on Enums.
Cory Larson
Work directly* on Enums, my bad
Cory Larson
+1  A: 

Obviously the easiest solution would be to create the dummy method. I guess I am wondering why you need an enumeration from your web service to be available in your JavaScript if you never pass that enumeration to the service or receive it from the service in any way.

Andrew Hare
The enum is used in the service; it's passed back to the client as a property on a class that is serialized and returned as JSON.
Cory Larson
I chose to use the dummy method to make it all work -- it was the most painless to implement.
Cory Larson
A: 

I think it should also have the Serializable() attribute

Rashmi Pandit