C# CSV文件和Web服务

C# CSV文件和Web服务,c#,.net,web-services,csv,asmx,C#,.net,Web Services,Csv,Asmx,大家好,很抱歉打扰你们。我开了一个小博客,我只是想创建一个Web服务,允许输入单词并显示其含义。我(最近)在C#code自学成才,觉得自己在进步,但我遇到了一个障碍。这是我目前的工作代码 抱歉,我刚刚注意到我复制/粘贴了错误的代码。现在更新了吗 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace My

大家好,很抱歉打扰你们。我开了一个小博客,我只是想创建一个Web服务,允许输入单词并显示其含义。我(最近)在C#code自学成才,觉得自己在进步,但我遇到了一个障碍。这是我目前的工作代码

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

  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();
                    }
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
命名空间MyWebService
{
/// 
///服务1的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类服务1:System.Web.Services.WebService
{
[网络方法]
公共类表
{
私有字典_regionTimeValues=新字典();
私有字符串(字),;
公共表(字符串)
{
_文字=文字;
}
public void AddValue(字符串键、字符串值)
{
_wordsTimeValues.Add(键、值);
}
}
公共课程
{
静态void Main(字符串[]参数)
{
字典表=新字典();
使用(var reader=newstreamreader(“Data.csv”))
{
//第一行包含列名。
var columnNames=reader.ReadLine().Split(',');
for(int i=1;i

我原以为将代码从一个应用程序移动到另一个应用程序会很简单,因为我有类似的应用程序在控制台应用程序中工作,但尝试在web服务上执行此操作让我非常恼火。

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

从最基本的意义上说,

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

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


您在网页中有来自控制台应用程序的代码,仅通过复制粘贴是无法实现的。您需要稍微修改一下代码。您提供的代码示例目的不明确,因此我无法为您提供示例。

具体的障碍是什么?这是否编译?
getWords()
中的
words
是什么?而
publicstringwords()
似乎没有返回任何内容。您希望这些方法做什么?由于目前的情况,应用程序无法编译,更不用说运行了。方法
words()
不返回值,并且
getWords()
引用了一个尚未定义的变量。是的,当我尝试运行它时,什么都没有发生。它编译了,但在我的网页上什么也没有出现。我正在尝试从data.csv获取单词。输入一个单词,意思就出来了。e、 g输入世界输出地球欢迎使用堆栈溢出!您使用的是旧的ASMX技术,不应用于新的开发。WCF或ASP.NET Web API应用于Web服务客户端和服务器的所有新开发。一个提示:微软已经在MSDN上退出了。