使用 System.Array 作为 DataMember 时 WCF AJAX 响应失败

发布于 2024-12-27 21:57:53 字数 1045 浏览 2 评论 0原文

当我尝试获取 Sytem.Array 作为 Json 类对象中的数据成员时,它会给出失败响应。

我的 WCf Web 服务正在使用以下方法 userActivities ,该方法返回类 Activity。我已在 Ajax 类 Activity 中设置参数。

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    Activity userActivities(Activity activity, string action);
}

[DataContract]
partial class Activity
{

}

在 MyService.cs 中,服务类是

public Activity userActivities(Activity activity, string action)
{
    if (action.ToLower() == "myactivities")
    {
      activity.get_MyActivities();
    }
    return activity;
}

,活动类是:

[DataMember]
public int userID;

[DataMember]
public Array _getList_MyActivities;

public void get_MyActivities()
{
    _DL_Activity = new DL_Activity();
    if (this.userID > 0)
        this._getList_MyActivities = _DL_Activity.get_MyActivity(this.userID);

}

When i m trying to get Sytem.Array As data member in Json Class object it gives failure response .

My WCf web-service i am using following method userActivities which return class Activity.I have set parameter in class Activity from Ajax .

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    Activity userActivities(Activity activity, string action);
}

[DataContract]
partial class Activity
{

}

And in the MyService.cs Class of Service is

public Activity userActivities(Activity activity, string action)
{
    if (action.ToLower() == "myactivities")
    {
      activity.get_MyActivities();
    }
    return activity;
}

And Class Activity is :

[DataMember]
public int userID;

[DataMember]
public Array _getList_MyActivities;

public void get_MyActivities()
{
    _DL_Activity = new DL_Activity();
    if (this.userID > 0)
        this._getList_MyActivities = _DL_Activity.get_MyActivity(this.userID);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2025-01-03 21:57:53

得到这篇文章。根据此尝试使用 ServiceKnownType 装饰您的服务。

像这样的

[ServiceContract]
[ServiceKnownType(typeof(string))]
[ServiceKnownType(typeof(string[]))]
public interface IMyService

根据您的评论,您在数组中存储不同的数据类型,我想您需要将数组中的所有项目的 typeof 作为 ServiceKnownType

希望这对你有用。

Got this post. According to this try decorating your service with ServiceKnownType.

Something like this

[ServiceContract]
[ServiceKnownType(typeof(string))]
[ServiceKnownType(typeof(string[]))]
public interface IMyService

According to your comment you are storing different data types in the array, I guess you will need to put typeof for all items in array as ServiceKnownType.

Hope this works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文