I'm currently enabling JSON calls to my web services using the ScriptService attribute. The problem is that one of my classes references a second class and .Net is not picking up and writing out the JavaScript for the second class.
我目前正在使用ScriptService属性为我的Web服务启用JSON调用。问题是我的一个类引用了第二个类,而.Net没有拿起并写出第二个类的JavaScript。
As a workaround I can write a dummy method that just returns the second class. Then .Net writes the JSON to allow it to be serialized. So in the following example:
作为一种解决方法,我可以编写一个只返回第二个类的虚方法。然后.Net写入JSON以允许它被序列化。所以在下面的例子中:
[ScriptService]
public class MyService : WebService {
[WebMethod]
public void SaveClass1(Class1 class1) {
...
}
}
[Serializable]
public class Class1 {
public Class2 class2 { get; set; }
}
[Serializable]
public class Class2 {
}
MyService.asmx/js won't write code to allow me to instantiate Class2 in order for me to populate Class1. But I can make it work if I add:
MyService.asmx / js不会编写代码以允许我实例化Class2以便我填充Class1。但是,如果我添加:
[WebMethod]
public Class2 Dummy() {
return new Class2();
}
to MyService. Any alternatives to my nasty workaround would be greatly appreciated.
到MyService。任何替代我讨厌的解决方法将不胜感激。
2 个解决方案
#1
You need to use System.Web.Script.Services.GenerateScriptTypeAttribute, which specifies that a server type should be included in the generated proxy code. You can apply this attribute to the web service itself or any method marked with WebMethodAttribute.
您需要使用System.Web.Script.Services.GenerateScriptTypeAttribute,它指定服务器类型应包含在生成的代理代码中。您可以将此属性应用于Web服务本身或使用WebMethodAttribute标记的任何方法。
For example:
[ScriptService]
[GenerateScriptType(typeof(Class2))]
public class MyService : WebService {
[WebMethod]
public void SaveClass1(Class1 class1) {
// ...
}
}
#2
Lee, not sure if this works in your JSON/[ScriptService]
scenario, but to serialize an object graph in Searcharoo.net I used the System.Xml.Serialization.XmlInclude
attribute on the 'base' class that I needed to serialize.
Lee,不确定这是否适用于您的JSON / [ScriptService]场景,但是为了在Searcharoo.net中序列化对象图,我使用了我需要序列化的'base'类的System.Xml.Serialization.XmlInclude属性。
Basically this tells the XmlSerializer about Class2
so that it can be included in the Class1
serialization... kinda like an "opt-in" strategy for handling deep hierachies of objects.
基本上,这告诉XmlSerializer关于Class2,以便它可以包含在Class1序列化中......有点像用于处理对象的深层次的“选择加入”策略。
You would add it like this (in your example):
你可以像这样添加它(在你的例子中):
[Serializable]
[System.Xml.Serialization.XmlInclude(typeof(Class2))]
public class Class1 {
public Class2 class2 { get; set; }
}
...and then this
......然后呢
[WebMethod]
public void SaveClass1(Class1 class1) {
...
}
should return JSON with Class2
data inside Class1
.
应该在Class1中返回带有Class2数据的JSON。
I did a quick google to find troubleshooting...XmlSerializer which might be a good read. HTH
我做了一个快速谷歌找到故障排除... XmlSerializer,这可能是一个很好的阅读。 HTH
#1
You need to use System.Web.Script.Services.GenerateScriptTypeAttribute, which specifies that a server type should be included in the generated proxy code. You can apply this attribute to the web service itself or any method marked with WebMethodAttribute.
您需要使用System.Web.Script.Services.GenerateScriptTypeAttribute,它指定服务器类型应包含在生成的代理代码中。您可以将此属性应用于Web服务本身或使用WebMethodAttribute标记的任何方法。
For example:
[ScriptService]
[GenerateScriptType(typeof(Class2))]
public class MyService : WebService {
[WebMethod]
public void SaveClass1(Class1 class1) {
// ...
}
}
#2
Lee, not sure if this works in your JSON/[ScriptService]
scenario, but to serialize an object graph in Searcharoo.net I used the System.Xml.Serialization.XmlInclude
attribute on the 'base' class that I needed to serialize.
Lee,不确定这是否适用于您的JSON / [ScriptService]场景,但是为了在Searcharoo.net中序列化对象图,我使用了我需要序列化的'base'类的System.Xml.Serialization.XmlInclude属性。
Basically this tells the XmlSerializer about Class2
so that it can be included in the Class1
serialization... kinda like an "opt-in" strategy for handling deep hierachies of objects.
基本上,这告诉XmlSerializer关于Class2,以便它可以包含在Class1序列化中......有点像用于处理对象的深层次的“选择加入”策略。
You would add it like this (in your example):
你可以像这样添加它(在你的例子中):
[Serializable]
[System.Xml.Serialization.XmlInclude(typeof(Class2))]
public class Class1 {
public Class2 class2 { get; set; }
}
...and then this
......然后呢
[WebMethod]
public void SaveClass1(Class1 class1) {
...
}
should return JSON with Class2
data inside Class1
.
应该在Class1中返回带有Class2数据的JSON。
I did a quick google to find troubleshooting...XmlSerializer which might be a good read. HTH
我做了一个快速谷歌找到故障排除... XmlSerializer,这可能是一个很好的阅读。 HTH