DataContract 属性未在响应中发送

发布于 2024-12-05 03:22:47 字数 3153 浏览 2 评论 0原文

我在发送请求时出现了名称/命名空间/其他属性,但现在它们消失了,我无法弄清楚发生了什么变化...我正在尝试利用 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-(第 1 部分)。 ASPX 但当我到达第六部分,他从 POST 返回 HttpResponseMessage时,一切就开始崩溃了。客户端在返回 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 } 
    );
}

....

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

I have had it where the name/namespace/other attributes show up when sending a request, but now they have disappeared and cant figure out for the life of me what changed... I am trying to utilize the WebApi project but the documentation seems limited.

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; } }
    }
}

Note I am using lightcore as an IOC container (kinda new to this stuff)
Was originally going of the post at http://blog.alexonasp.net/post/2011/04/15/Microsoft-Web-API-e28093-the-REST-is-done-by-WCF-(Part-1).aspx
but once I got to part six where he is returning HttpResponseMessage<Contact> from POSTs, it all started falling apart. client was looking for the namespace when returning xml but that was not part of the serialized response...

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 } 
    );
}

....

Ok, what it seems that I failed to realize, is that the name/namespace info gets serialized to the server, but not to the client.

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

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

发布评论

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

评论(2

对你再特殊 2024-12-12 03:22:47

是否添加了路由表记录?

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

Have you added a routing table record?

RouteTable.Routes.MapServiceRoute<WebServiceResources>("GetNameValueTest");
青春有你 2024-12-12 03:22:47

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

Updated answer on this... I was using a DataContractSerializer on the client which was adding the name/namespace info, while on the server I was using the default WebApi serialization which was not adding the info. Thanks for anyone who took time looking into this.

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