【问题标题】:DataContract Attributes not being sent in responsesDataContract 属性未在响应中发送
【发布时间】:2011-11-18 05:26:15
【问题描述】:

我已经在发送请求时显示名称/命名空间/其他属性的位置,但现在它们已经消失了,我一生都无法弄清楚发生了什么变化......我正在尝试使用 WebApi 项目但是文档似乎有限。

WebServiceResource.cs:

using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Web;
using wsDAL.EDataTypes;
using System.Data;
using System.IO;
using System.Text;
using System.Net.Http;

namespace wsDAL
{
    [ServiceContract]
    public class WebServiceResources
    {
        [WebGet(UriTemplate = "/GetNameValueTest/{name}/{value}")]
        public NameValue GetNameValueTest(string name, string value)
        {
            NameValue nv = new NameValue("WS_" + name + "_WS", "WS_" + value + "_WS");  
            return nv;
        }
    }
}

GeneralResources.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Data;

namespace wsDAL.EDataTypes
{
    [DataContract(Name = "NameValueContract", Namespace = "http://fred.NameValue.com")]
    public class NameValue
    {
        private string _name;
        private string _value;

        public NameValue()
        {
            _name = null;
            _value = null;
        }

        public NameValue(string Name, string Value)
        {
            _name = Name;
            _value = Value;
        }

        [DataMember(Name = "NameMember")]
        public string Name { get { return _name; } set { _name = value; } }

        [DataMember(Name = "ValueMember")]
        public string Value { get { return _value; } set { _value = value; } }
    }
}

注意我使用 lightcore 作为 IOC 容器(对这个东西有点新) 最初是在http://blog.alexonasp.net/post/2011/04/15/Microsoft-Web-API-e28093-the-REST-is-done-by-WCF-(Part-1).aspx发帖的 但是,一旦我进入第六部分,他从 POST 中返回 HttpResponseMessage<Contact>,一切都开始分崩离析。客户端在返回 xml 时正在寻找命名空间,但这不是序列化响应的一部分...

Global.asax.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using LightCore;
using Microsoft.ApplicationServer.Http.Description;
using Microsoft.ApplicationServer.Http.Activation;

....

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    ContainerBuilder builder = new ContainerBuilder();
    builder.Register<IResourceFactory, LightCoreResourceFactory>();

    IContainer container = builder.Build();
    var configuration = HttpHostConfiguration.Create().SetResourceFactory((serviceType, instanceContext, request) => container.Resolve(serviceType), null);

    RouteTable.Routes.MapServiceRoute<WebServiceResources>("ws", configuration);

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    );
}

....

好吧,我似乎没有意识到,名称/命名空间信息被序列化到服务器,而不是客户端。

【问题讨论】:

    标签: wcf web-services asp.net-web-api


    【解决方案1】:

    你添加了路由表记录吗?

    RouteTable.Routes.MapServiceRoute<WebServiceResources>("GetNameValueTest");
    

    【讨论】:

      【解决方案2】:

      对此的更新答案...我在添加名称/命名空间信息的客户端上使用 DataContractSerializer,而在服务器上我使用未添加信息的默认 WebApi 序列化。感谢任何花时间研究此问题的人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 2013-08-31
        • 2019-12-24
        • 2020-12-31
        • 1970-01-01
        • 2014-08-29
        • 2020-07-30
        相关资源
        最近更新 更多