1

看到一个奇怪的问题,获取 xml、json 和 jvs 的空元页面。使用以下命令行应用程序。如何调试这些问题?

namespace ConsoleApplication2
{
    public struct NativeUser
    {
        public int login;
        public string group;
        public string name;
    }


    [DataContract]
    public class User
    {
        private NativeUser _native;

        public User() { }


        public User(NativeUser native)
        {
            _native = native;
        }

        public static implicit operator NativeUser(User user)
        {
            return user._native;
        }

        public static implicit operator User(NativeUser native)
        {
            return new User(native);
        }

        // ReSharper disable InconsistentNaming
        [DataMember]
        public int login
        {
            get { return _native.login; }
            set { _native.login = value; }
        }

        [DataMember]
        public string group
        {
            get { return _native.group; }
            set { _native.group = value; }
        }
        [DataMember]
        public string name
        {
            get { return _native.name; }
            set { _native.name = value; }
        }
    }


    [Description("GET account, all or by list of groups or by list of logins")]
    [Route("/accounts/{groups}", "GET")]
    [Route("/accounts/{logins}", "GET")]
    [Route("/accounts/", "GET")]
    public class Accounts : IReturn<User[]>
    {
        public string[] groups { set; get; }
        public int[] logins { set; get; }

        public Accounts() { }

        public Accounts(params int[] logins)
        {
            this.logins = logins;
        }

        public Accounts(params string[] groups)
        {
            this.groups = groups;
        }
    }

    public class Host : AppHostHttpListenerBase
    {
        public Host() : base("Test", 
                            typeof(Accounts).Assembly)
        {

        }

        public override void Configure(Funq.Container container)
        {

        }
    }

    public class Servce : IService
    {
        public object Get(Accounts request)
        {
            return new List<User>(){new User(new NativeUser())};
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            var host = new Host();
            host.Init();
            host.Start("http://+:12345/");

            global::System.Console.ReadLine();

        }
    }
}
4

1 回答 1

1

Nm,发现错误:

public class Accounts : IReturn<User[]>

需要是

public class Accounts : IReturn<List<User>>

另一个非常值得注意的事情是:所有 DTO 和被传回的对象和 DTO 中的第四个对象都需要一个空的构造函数才能正确生成元数据。不确定这是设计使然还是错误

于 2013-01-27T22:25:06.423 回答