WCF服务没有返回Json中的完整对象

时间:2013-09-15 15:38:00

标签: c# asp.net json wcf

[OperationContract]
    public FareDataModel GetFareAndDistanceJson(string source, string destination, string country, string city)
    {
       return  FareManager.GetFare(source, destination, country, city);


    }

我的svc类中有上面的方法。但这不能在Json中完全发送数据。我得到以下结果

 {
"d": {
    "__type": "FareDataModel:#MeterupAutoTaxiWebService",
    "Fares": [
        {
            "__type": "VehicleFare:#MeterupAutoTaxiWebService"
        }
    ]
}
}

但是当我把它作为字符串返回时,我得到数据但是以字符串的形式。我真的想得到json。 这是我的FareDataModel对象

 [DataContract]
    public class FareDataModel
    {
        public string SourceAddress;
        public string DestinationAddress;
        public int Duration;
        public int Distance;
        public string Error;

        [DataMember]
        public List<VehicleFare> Fares = new List<VehicleFare>();
    }


    [DataContract]
    public class VehicleFare
    {
        public string PhoneNo;
        public string Vehicle;
        public double Fare;
    }

我尝试了其中一篇文章中解释的所有更改,但没有任何内容对我有用。 当我通过修改我的代码返回字符串

[OperationContract]
    public string GetFareAndDistanceJson(string source, string destination, string country, string city)
    {

       return new JavaScriptSerializer().Serialize(FareManager.GetFare(source, destination, country, city));

    }

我得到以下结果

    {
    "d": "{\"SourceAddress\":\"Majestic, Bangalore, Karnataka, India\",\"DestinationAddress\":\"Marathahalli Market, Bangalore, Karnataka, India\",\"Duration\":2475,\"Distance\":19872,\"Error\":null,\"Fares\":[{\"PhoneNo\":null,\"Vehicle\":\"Auto\",\"Fare\":238}]}"
}

我真的需要一个真正的json响应。

这是我的班级

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{



    [OperationContract]
    public string GetFareAndDistanceJson(string source, string destination, string country, string city)
    {

       return new JavaScriptSerializer().Serialize(FareManager.GetFare(source, destination, country, city));

    }
    // Add more operations here and mark them with [OperationContract]
}

请帮帮我。

我的webconfig

    <?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />

    </system.web>

    <system.serviceModel>
        <services>
            <service name="MeterupAutoTaxiWebService.Service1">
                <endpoint address="" behaviorConfiguration="MeterupAutoTaxiWebService.Service1AspNetAjaxBehavior"
                    binding="webHttpBinding" contract="MeterupAutoTaxiWebService.Service1" />
            </service>
        </services>
        <behaviors>

            <endpointBehaviors>
                <behavior name="MeterupAutoTaxiWebService.Service1AspNetAjaxBehavior">
              <enableWebScript/>

                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
  <connectionStrings>
    <add name="MeterupAutoTaxiEntities" connectionString="metadata=res://*/MeterupTaxiAutoEntities.csdl|res://*/MeterupTaxiAutoEntities.ssdl|res://*/MeterupTaxiAutoEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=MeterupAutoTaxi;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

1 个答案:

答案 0 :(得分:0)

在向每个人添加[DataMember]之后工作。当我请求XML时,它返回了有效的XML而没有添加它。但是我必须添加它以返回json