0

我想将我以前的 .NET Web 服务更改为 WCF 服务,因为我希望我的 Web 服务为我的 android 应用程序发布纯 JSON 变量。这是我到目前为止所做的,但我仍然遇到错误。如何修复此代码?

我的 IService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.Serialization.Json;

public class Service : IService
{
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "tampil")]
    public String GetReport()
    {
        List<Report> reports = new List<Report>();
        string connectionString = ConfigurationManager.ConnectionStrings["ConnWf"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string sql = "select type, sum(OrderQty) as total from tbl_weeklyflash_ID where type <> 'NULL' group by type";
            connection.Open();

            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Report report = new Report();
                report.type = reader["type"].ToString();
                report.total = reader["total"].ToString();
                reports.Add(report);
            }
        }

        MemoryStream stream = new MemoryStream();
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Report[]));
        serializer.WriteObject(stream, reports.ToArray());
        stream.Position = 0;
        StreamReader streamReader = new StreamReader(stream);
        return streamReader.ReadToEnd();
    }
}

我的Service.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.Serialization.Json;

public class Service : IService
{
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "tampil")]
    public String GetReport()
    {
        List<Report> reports = new List<Report>();
        string connectionString = ConfigurationManager.ConnectionStrings["ConnWf"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string sql = "select type, sum(OrderQty) as total from tbl_weeklyflash_ID where type <> 'NULL' group by type";
            connection.Open();

            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Report report = new Report();
                report.type = reader["type"].ToString();
                report.total = reader["total"].ToString();
                reports.Add(report);
            }
        }

        MemoryStream stream = new MemoryStream();
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Report[]));
        serializer.WriteObject(stream, reports.ToArray());
        stream.Position = 0;
        StreamReader streamReader = new StreamReader(stream);
        return streamReader.ReadToEnd();
    }
}
4

0 回答 0