开发者

Pass and get object with ASMX WebService

开发者 https://www.devze.com 2023-03-25 18:47 出处:网络
We have a web service like this: [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

We have a web service like this:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public object[] DoSmt(object[] inParams)
{
    List<object> rsl = new List<object>();
    rsl.Add(DateTime.Now);
    rsl.Add(new CallResult());
    return rsl.ToArray();
}

CallResult class is defined in web service. We call this method from WinForms (before that we add a web reference to this web service):

Service svc = new Service();
object[] arrRsl = svc.DoSmt(new object[] { "hi there", "hello" });

We get an exception says

App do not know how to deserialize CallResult

开发者_如何学Go

But if we put a funny function like this into the web service:

[WebMethod(EnableSession = true)]
public void Fun(CallResult abc)
{
    // Do nothing
}

then everything is ok. It is because CallResult does not appear in WSDL file before adding of funny function because it does not appear in any WebMethod.

The question is: How to inform C# to generate CallResult in WSDL file even if it does not explicitly be used in any WebMethod. We use VS2005.

Thanks.


You can mark the class with the GenerateScriptTypeAttribute.

In some cases, even if the type corresponds to an input parameter or return value of a Web service method, the proxy will not be generated automatically. In that case, you must use the GenerateScriptTypeAttribute attribute to generate the proxy object for the type.

[GenerateScriptType(typeof(ColorObject), ScriptTypeId = "Color")]
[WebMethod]
public string[] GetDefaultColor()
{
    // Instantiate the default color object.
    ColorObject co = new ColorObject();

    return co.rgb;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消