本文介绍了在.NET 3.5中使用WCF DataContractJsonSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用地理编码code从这里开始我的ASP.NET MVC 2的网站。不幸的是,有些是code,特别是的DataContractJsonSerializer使用,只能通过.NET 4.0 。由于我的托管服务提供商不支持.NET 4中,我被迫实现.NET 3.5的功能。

我如何能返工code(这是我转载如下)工作在.NET 3.5?

在谷歌地图地理编码API还可以返回XML,如果这是更容易在3.5序列化...


下面是code我试图从.NET 4转换为.NET 3.5:

 使用系统;
使用System.Runtime.Serialization;
使用System.Runtime.Serialization.Json;
使用System.Net;
使用的System.Web;

。
。
。

私有静态GeoResponse CallGeoWS(串地址)
{
        字符串URL =的String.Format(
                http://maps.google.com/maps/api/geo$c$c/json?address={0}&region=dk&sensor=false
                HttpUtility.UrlEn code(地址)
                );
        VAR请求=(HttpWebRequest的)HttpWebRequest.Create(URL);
        request.Headers.Add(Htt的prequestHeader.AcceptEncoding,GZIP,放气);
        request.AutomaticDecom pression = DECOM pressionMethods.GZip | DECOM pressionMethods.Deflate;
        DataContractJsonSerializer串行=新DataContractJsonSerializer(typeof运算(GeoResponse));
        VAR RES =(GeoResponse)serializer.ReadObject(request.GetResponse()GetResponseStream());
        返回水库;
}

[DataContract]
类GeoResponse
{
        [数据成员(名称为状态)
        公共字符串状态{获得;组; }
        [数据成员(名称为结果)
        公共CResult [] {结果得到;组; }

        [DataContract]
        公共类CResult
        {
                [数据成员(名称为几何)
                公共CGeometry几何{获得;组; }

                [DataContract]
                公共类CGeometry
                {
                        [数据成员(名称为位置)
                        公共CLocation位置{获得;组; }

                        [DataContract]
                        公共类CLocation
                        {
                                [数据成员(名称为土地增值税)
                                公共双纬度{获得;组; }
                                [数据成员(NAME =LNG)
                                公共双LNG {获得;组; }
                        }
                }
        }
}
 

解决方案

什么是你打的具体问题?

如果没有更多的细节,很难诊断确切的问题,但 DataContractJsonSerializer 可用在.NET 3.5 - 你需要手动添加引用的 System.ServiceModel.Web.dll 的。

(注意MSDN文档误导指出 DataContractJsonSerializer 可以在 System.Runtime.Serialization.dll 的发现。这是真实的.NET 4中,.NET 3.5版本的 DataContractJsonSerializer 实际上住在的 System.ServiceModel.Web.dll 的。)

I'm trying to use the geocoding code from here in my ASP.NET MVC 2 site. Unfortunately, some of that code, specifically the DataContractJsonSerializer usage, is only possible through .NET 4.0. As my hosting provider doesn't support .NET 4, I'm forced to implement that functionality in .NET 3.5.

How can I rework the code (which I have reposted below) to work in .NET 3.5?

The Google Maps Geocoding API can also return XML, if that's easier to serialize in 3.5...


Below is the code I'm trying to convert from .NET 4 to .NET 3.5:

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Web;

.
.
.

private static GeoResponse CallGeoWS(string address)
{
        string url = string.Format(
                "http://maps.google.com/maps/api/geocode/json?address={0}&region=dk&sensor=false",
                HttpUtility.UrlEncode(address)
                );
        var request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
        var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
        return res;
}

[DataContract]
class GeoResponse
{
        [DataMember(Name="status")]
        public string Status { get; set; }
        [DataMember(Name="results")]
        public CResult[] Results { get; set; }

        [DataContract]
        public class CResult
        {
                [DataMember(Name="geometry")]
                public CGeometry Geometry { get; set; }

                [DataContract]
                public class CGeometry
                {
                        [DataMember(Name="location")]
                        public CLocation Location { get; set; }

                        [DataContract]
                        public class CLocation
                        {
                                [DataMember(Name="lat")]
                                public double Lat { get; set; }
                                [DataMember(Name = "lng")]
                                public double Lng { get; set; }
                        }
                }
        }
}
解决方案

What is the specific problem that you're hitting?

Without more details it's difficult to diagnose the exact problem, but DataContractJsonSerializer is available in .NET 3.5 - you'll need to manually add a reference to System.ServiceModel.Web.dll.

(Note that the MSDN documentation misleadingly states that DataContractJsonSerializer can be found in System.Runtime.Serialization.dll. While this is true for .NET 4, the .NET 3.5 version of DataContractJsonSerializer actually lives in System.ServiceModel.Web.dll.)

这篇关于在.NET 3.5中使用WCF DataContractJsonSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 02:17