【问题标题】:The Namespace X already contains a definition for XX?命名空间 X 已经包含 XX 的定义?
【发布时间】:2015-10-27 14:52:11
【问题描述】:

我不明白我遇到的这个错误。我曾多次尝试清理和构建我的项目。谁能帮我? 程序.cs

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;

namespace HTTPrequestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var lstWebSites = new List<string>
            {
                "www.mearstransportation.com",
                "www.amazon.com",
                "www.ebay.com",
                "www.att.com",
                "www.verizon.com",
                "www.sprint.com",
                "www.centurylink.com",
                "www.yahoo.com"
            };
            string filename = @"RequestLog.txt";
            {
                using (var writer = new StreamWriter(filename, true))
                {
                    foreach (string website in lstWebSites)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            MyWebRequest request = new MyWebRequest();
                            request.Request(website);
                        }
                    }
                }
            }
        }
    }
}

MyWebRequest.cs - 问题出在:公共类 MyWebRequest 错误是:“命名空间 HttpRequestApp 已经包含 MyWebRequest 的定义”

using HTTPrequestApp.MyWebRequest;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace HTTPrequestApp
{
    public class MyWebRequest : IWebRequest
    {
        public void Request(string strWebSite)
        {
            List<string> lstWebSites = Program.GetList(strWebSite);
            using (var client2 = new TcpClient(strWebSite, 80))
            {
                using (NetworkStream stream = client2.GetStream())
                using (StreamWriter writer = new StreamWriter(stream))
                using (StreamReader reader2 = new StreamReader(stream))
                {
                    //writer.AutoFlush = true;
                    writer.WriteLine("GET / HTTP/1.1");
                    writer.WriteLine("HOST: {0}:80", lstWebSites[1]);
                    writer.WriteLine("Connection: Close");
                    writer.WriteLine();
                    writer.WriteLine();

                    string theresponse = reader2.ReadToEnd();
                    Console.WriteLine(theresponse);
                }
            }
        }
    }
}

IWebRequest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.IO;
using System.Threading.Tasks;
//using MyWebRequest.Lib;

namespace HTTPrequestApp.MyWebRequest
{
    public interface IWebRequest
    {
        Task<List<strWebSite>> GetList();
        void Request();
    }
}

为了概述我在这里要完成的工作是:发送 HTTP 请求以获取初始页面。取回 HTTP 响应并检查它是否为 200 响应代码。以及检索响应所需的时间。 这是一个控制台应用程序,但我不需要它依赖于控制台,它需要是一个独立的应用程序,以便我可以在其他地方使用它。 如果有人对如何简化代码有任何建议,请告诉我。 谢谢。

【问题讨论】:

    标签: c# .net http interface request


    【解决方案1】:

    你有一个 HTTPrequestApp.MyWebRequest 命名空间和一个 HTTPrequestApp.MyWebRequest 类名:c# 编译器会感到困惑(人类也是......)

    如果您真的想要不同的命名空间,请考虑将命名空间重命名为 HTTPrequestApp.MyWebRequestNameSpace

    【讨论】:

    • 错误是'HTTPrequestApp.MyWebRequest'没有实现接口成员'HTTPrequestApp.IWebRequest.Request();
    • 接口声明了一个不带参数的方法Request,MyWebRequest有一个带字符串参数的方法Request,但是没有带接口签名的方法。在 MyWebRequest 类中添加方法public virtual void Request()(或更改IWebRequest接口中的声明)
    • 当然,您还必须在 MyWebRequest 类中实现 Task&lt;List&lt;strWebSite&gt;&gt; GetList()
    • 我宁愿更改 IWebRequest 中的声明,我该怎么做?
    • void Request(string strWebSite); 而不是接口声明中的void Request();
    猜你喜欢
    • 2016-07-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 2013-02-22
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多