2013-11-20 47 views
1

嗨大家抱歉打擾你。我運行一個小博客,我只是試圖創建一個web服務,允許輸入單詞並顯示其意義。我最近在C#代碼中自學成功,認爲自己越來越好,但我遇到了一些困難。這是我目前的工作代碼;CSV文件和Web服務

SORRY JUST NOTICED HAD COPY/PASTED WRONG CODE。更新的IT NOW

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服務可以將其用作移動從一個應用程序代碼到另一個簡單已經把我拉我的頭髮。

+1

究竟是什麼? –

+1

這甚至編譯? getWords()中的'words'是什麼?而'public string words()'似乎沒有返回任何東西。 –

+0

你期待這些方法做什麼?正如他們目前的立場,應用程序不會編譯,更不用說運行。 'words()'方法不返回值,'getWords()'引用一個尚未定義的變量。 – Kami

回答

0

您的代碼將控制檯應用程序代碼與Web應用程序代碼混合在一起。兩者運行方式不同。

在最基本的意義上,

控制檯應用程序所需的方法通常static void Main()這是第一種方法運行的應用程序啓動時。

Web應用程序只運行給定頁面上對應於請求操作的方法。

你有一個網頁中的控制檯應用程序的代碼,這不會通過簡單的複製粘貼工作。你需要修改代碼。您提供的代碼樣本目的不明確,所以我無法爲您提供樣本。