【问题标题】:CSV Files and Web ServicesCSV 文件和 Web 服务
【发布时间】:2013-11-20 18:10:06
【问题描述】:

大家好,很抱歉打扰您。我运行一个小博客,我只是想创建一个允许输入单词并显示其含义的网络服务。我(最近)在 C# 代码中自学,并认为我正在变得更好,但我遇到了障碍。这是我当前的工作代码;

抱歉,刚刚发现我复制/粘贴了错误的代码。现在更新了

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace MyWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public class Table
        {
            private Dictionary<string, string> _regionTimeValues = new Dictionary<string, string>();
            private String _words;

            public Table(String words)
            {
                _words = words;
            }

            public void AddValue(string key, string value)
            {
                _wordsTimeValues.Add(key, value);
            }
        }

        public class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, Table> tables = new Dictionary<string, Table>();

                using (var reader = new StreamReader("Data.csv"))
                {
                    // First line contains column names.
                    var columnNames = reader.ReadLine().Split(',');
                    for (int i = 1; i < columnNames.Length; ++i)
                    {
                        var columnName = columnNames[i];
                        tables.Add(columnName, new Table(columnName));
                    }

                    var line = reader.ReadLine();
                    while (line != null)
                    {
                        var columns = line.Split(',');

                        for (int i = 1; i < columns.Length; ++i)
                        {
                            var table = tables[columnNames[i]];
                            table.AddValue(columns[0], double.Parse(columns[i]));
                        }

                        line = reader.ReadLine();
                    }
                }
            }
        }
    }
}

我认为这就像将代码从一个应用程序移动到另一个应用程序一样简单,因为我有类似的应用程序在控制台应用程序中工作,但尝试在 Web 服务上执行此操作让我大吃一惊。

【问题讨论】:

  • 障碍到底是什么?
  • 这还能编译吗? getWords() 中的 words 是什么?而public string words() 似乎没有返回任何东西。
  • 您希望这些方法能做什么?按照他们目前的立场,应用程序无法编译,更不用说运行了。 words() 方法没有返回值,getWords() 引用了一个尚未定义的变量。
  • 是的,当我尝试运行它时没有任何反应。它可以编译,但我的网页上没有出现任何内容。我正在尝试从 data.csv 中获取单词。输入1个单词,意思就出来了。例如输入世界输出地球
  • 欢迎来到 Stack Overflow!您正在使用旧版 ASMX 技术,不应将其用于新开发。 WCF 或 ASP.NET Web API 应用于 Web 服务客户端和服务器的所有新开发。一个提示:微软已经在 MSDN 上停用了ASMX Forum

标签: c# .net web-services csv asmx


【解决方案1】:

您的代码将控制台应用程序代码与 Web 应用程序代码混合在一起。两者的运行方式不同。

在最基本的意义上,

控制台应用程序需要一个方法,通常是static void Main(),这是应用程序启动时运行的第一个方法。

Web 应用程序仅在给定页面上运行与请求的操作相对应的方法。

您在网页中有来自控制台应用程序的代码,这不能通过简单的复制粘贴来工作。您需要稍微修改一下代码。您提供的代码示例目的不明确,所以我无法为您提供示例。

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 2011-10-27
    • 2012-05-25
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多