Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web服务与表单转换器_C#_Asp.net_Web Services_Visual Studio_Webforms - Fatal编程技术网

C# Web服务与表单转换器

C# Web服务与表单转换器,c#,asp.net,web-services,visual-studio,webforms,C#,Asp.net,Web Services,Visual Studio,Webforms,我的项目是编写一个web服务和一个使用它的web表单。它应该有两个文本框和一个按钮。用户在第一个文本框中输入一个文本缩写词,然后按下按钮。web服务将textbox1条目与字典文件进行比较,并在第二个文本框中显示生成的完整单词。这是我到目前为止的代码,我真的很难让它工作,任何帮助将不胜感激。此时,我出现了“类型或命名空间定义,或预期文件结尾”错误。这是我的两个文件 Default.aspx.cs: using System; using System.Collections.Generi

我的项目是编写一个web服务和一个使用它的web表单。它应该有两个文本框和一个按钮。用户在第一个文本框中输入一个文本缩写词,然后按下按钮。web服务将textbox1条目与字典文件进行比较,并在第二个文本框中显示生成的完整单词。这是我到目前为止的代码,我真的很难让它工作,任何帮助将不胜感激。此时,我出现了“类型或命名空间定义,或预期文件结尾”错误。这是我的两个文件

Default.aspx.cs:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    private Dictionary<string, string> _dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    protected void Page_Load(object sender, EventArgs e)
    {
        using (var reader = new StreamReader(File.OpenRead(@"C:/dictionary.csv")))
        {
            while (!reader.EndOfStream)
            {
                string[] tokens = reader.ReadLine().Split(';');
                _dictionary[tokens[0]] = tokens[1];
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.Service obj = new localhost.Service();
        TextBox1.Text = (obj.Translate());
    }
}

不确定这个问题是否仍然没有答案。但以下是我的建议

在web服务文件(即Service1.cs)中,您没有声明_dictionary对象。因此,您将在服务的构造函数中移动字典对象声明和初始化

下面是这样的东西

 public WebService1()
    {
        using (var reader = new StreamReader(File.OpenRead(@"C:/dictionary.csv")))
        {
            while (!reader.EndOfStream)
            {
                string[] tokens = reader.ReadLine().Split(',');
                _dictionary[tokens[0]] = tokens[1];
            }
        }
    }
同样在split方法中,我假设您希望使用逗号而不是分号(在您的示例中使用了分号)

然后在使用服务的过程中,你会做下面这样的事情。我不确定您在示例中使用localhost对象想要做什么

 ServiceReference1.WebService1SoapClient obj = new WebService1SoapClient();
        TextBox2.Text =  obj.Translate(TextBox1.Text);
希望这有帮助

-达伍德

 ServiceReference1.WebService1SoapClient obj = new WebService1SoapClient();
        TextBox2.Text =  obj.Translate(TextBox1.Text);