returning dictionary from ASMX web service

A

Andrus

I tried to return dictionary from ASMX (not SVC) Web Service but got error.

How to fix ?

Andrus.

[WebMethod]
public List<Dictionary<string, object>> GetProductList(int pageNumber, int
itemsPerPage, string filter, string sortBy, out int totalProductCount)
{
totalProductCount = 10;
var list = new List<Dictionary<string, object>>();
for (var i = 0; i < 10; i++)
{
var dict = new Dictionary<string, object>();
dict["ID"] = "Product"+i.ToString();
dict["Name"] = "Name_" + i;
dict["Index"] = i;
dict["IsEven"] = (i % 2 == 0);
list.Add(dict);
}
return list;
}


updating service reference returns error

---------------------------
Visual Web Developer 2008 Express Edition
---------------------------
There was an error downloading
'http://localhost:1839/StockService.asmx?wsdl'.

The request failed with the error message:

--

System.InvalidOperationException: Method StockService.GetProductList can not
be reflected. ---> System.InvalidOperationException: There was an error
reflecting 'GetProductListResult'. ---> System.NotSupportedException: The
type System.Collections.Generic.Dictionary`2[[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because
it implements IDictionary.

at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String
memberInfo)

at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type,
MemberInfo memberInfo, Boolean directReference)

at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo
source, Boolean directReference, Boolean throwOnError)

at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type,
MemberInfo memberInfo, Boolean directReference)

at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo
source, Boolean directReference, Boolean throwOnError)

at
System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember
xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers,
Boolean rpc, Boolean openModel, RecursionLimiter limiter)

at
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[]
xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc,
Boolean openModel, RecursionLimiter limiter)

--- End of inner exception stack trace ---

at
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[]
xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc,
Boolean openModel, RecursionLimiter limiter)

at
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String
elementName, String ns, XmlReflectionMember[] members, Boolean
hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access)

at
System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter
xmlImporter, SoapReflectionImporter soapImporter, Boolean
serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle
paramStyle, String elementName, String elementNamespace, Boolean
nsIsDefault, XmlReflectionMember[] members, Boolean validate, Boolean
openModel, String key, Boolean writeAccess)

at
System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo
methodInfo, Boolean client, XmlReflectionImporter xmlImporter,
SoapReflectionImporter soapImporter, String defaultNs)

--- End of inner exception stack trace ---

at
System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo
methodInfo, Boolean client, XmlReflectionImporter xmlImporter,
SoapReflectionImporter soapImporter, String defaultNs)

at System.Web.Services.Description.SoapProtocolReflector.ReflectMethod()

at
System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding
reflectedBinding)

at System.Web.Services.Description.ProtocolReflector.Reflect()

at
System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[]
reflectors)

at
System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type
type, String url)

at System.Web.Services.Protocols.DiscoveryServerType..ctor(Type type,
String uri)

at System.Web.Services.Protocols.DiscoveryServerProtocol.Initialize()

at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type,
HttpContext context, HttpRequest request, HttpResponse response)

at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,
HttpContext context, HttpRequest request, HttpResponse response, Boolean&
abortProcessing)

--.

Metadata contains a reference that cannot be resolved:
'http://localhost:1839/StockService.asmx?wsdl'.

Metadata contains a reference that cannot be resolved:
'http://localhost:1839/StockService.asmx?wsdl'.
 
M

miher

Hi,

I think You can work around that by creating a custom class with those 4
properties(Id, Name,...), then return a list of those objects instead of a
list of dictionaries.

Hope You find this useful.
-Zsolt

Andrus said:
I tried to return dictionary from ASMX (not SVC) Web Service but got
error.

How to fix ?

Andrus.

[WebMethod]
public List<Dictionary<string, object>> GetProductList(int pageNumber, int
itemsPerPage, string filter, string sortBy, out int totalProductCount)
{
totalProductCount = 10;
var list = new List<Dictionary<string, object>>();
for (var i = 0; i < 10; i++)
{
var dict = new Dictionary<string, object>();
dict["ID"] = "Product"+i.ToString();
dict["Name"] = "Name_" + i;
dict["Index"] = i;
dict["IsEven"] = (i % 2 == 0);
list.Add(dict);
}
return list;
}


updating service reference returns error

---------------------------
Visual Web Developer 2008 Express Edition
---------------------------
There was an error downloading
'http://localhost:1839/StockService.asmx?wsdl'.

The request failed with the error message:

--

System.InvalidOperationException: Method StockService.GetProductList can
not be reflected. ---> System.InvalidOperationException: There was an
error reflecting 'GetProductListResult'. --->
System.NotSupportedException: The type
System.Collections.Generic.Dictionary`2[[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not
supported because it implements IDictionary.

at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type,
String memberInfo)

at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type,
MemberInfo memberInfo, Boolean directReference)

at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo
source, Boolean directReference, Boolean throwOnError)

at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type,
MemberInfo memberInfo, Boolean directReference)

at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo
source, Boolean directReference, Boolean throwOnError)

at
System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember
xmlReflectionMember, String ns, XmlReflectionMember[]
xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter
limiter)

at
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[]
xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc,
Boolean openModel, RecursionLimiter limiter)

--- End of inner exception stack trace ---

at
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[]
xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc,
Boolean openModel, RecursionLimiter limiter)

at
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String
elementName, String ns, XmlReflectionMember[] members, Boolean
hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess
access)

at
System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter
xmlImporter, SoapReflectionImporter soapImporter, Boolean
serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use,
SoapParameterStyle paramStyle, String elementName, String
elementNamespace, Boolean nsIsDefault, XmlReflectionMember[] members,
Boolean validate, Boolean openModel, String key, Boolean writeAccess)

at
System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo
methodInfo, Boolean client, XmlReflectionImporter xmlImporter,
SoapReflectionImporter soapImporter, String defaultNs)

--- End of inner exception stack trace ---

at
System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo
methodInfo, Boolean client, XmlReflectionImporter xmlImporter,
SoapReflectionImporter soapImporter, String defaultNs)

at System.Web.Services.Description.SoapProtocolReflector.ReflectMethod()

at
System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding
reflectedBinding)

at System.Web.Services.Description.ProtocolReflector.Reflect()

at
System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[]
reflectors)

at
System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type
type, String url)

at System.Web.Services.Protocols.DiscoveryServerType..ctor(Type type,
String uri)

at System.Web.Services.Protocols.DiscoveryServerProtocol.Initialize()

at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type,
HttpContext context, HttpRequest request, HttpResponse response)

at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,
HttpContext context, HttpRequest request, HttpResponse response, Boolean&
abortProcessing)

--.

Metadata contains a reference that cannot be resolved:
'http://localhost:1839/StockService.asmx?wsdl'.

Metadata contains a reference that cannot be resolved:
'http://localhost:1839/StockService.asmx?wsdl'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top