C# 错误:成员名称不能与其封闭类型和接口声明类型相同

发布于 2024-10-19 20:49:16 字数 1346 浏览 3 评论 0原文

我正在尝试为 Windows Azure 启动 appFabric 服务。我是实现和 EchoService,我需要通过 IEchoContract 接口实现,所有这些都在服务器端。 所以我就这样进行。

在 IEchoContract.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;


namespace Service
{
[ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
interface IEchoContract
{
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
}}

和 EchoSErvice.cs 上

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
class EchoService
{
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }}}

我遇到了两个错误,我不是 C# 专家 所以第一个:当我放置 EchoService : IEchoContract 时,我得到了

'EchoService': member names cannot be the same as their enclosing type

第二个,当我放置公共接口 IEchoContract 时

'IEchoContract' : interfaces declare types

,所以请帮忙。谢谢。

I am trying to lauch a service for appFabric for windows Azure. I am implement and EchoService and i need to implement by the way and IEchoContract interface, all of this on server side.
So I proceed like this.

On the IEchoContract.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;


namespace Service
{
[ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
interface IEchoContract
{
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
}}

And on EchoSErvice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
class EchoService
{
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }}}

I got two errors, i am not an expert on C#
So first one: When i put EchoService : IEchoContract i got

'EchoService': member names cannot be the same as their enclosing type

Second when i put public interface IEchoContract

'IEchoContract' : interfaces declare types

So please help. Thx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

病女 2024-10-26 20:49:16

您已声明接口和类两次 - 每个仅声明一次。

IEchoContract.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
}

EchoService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }
}

You have declared the interface and the class twice - declare each just once.

IEchoContract.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
}

EchoService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }
}
dawn曙光 2024-10-26 20:49:16

如果您在代码中看到在名为 EchoService 的类中有一个名为 EchoSevice 的类,

namespace Service
{
  class EchoService
  {
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    ...

请尝试删除外部类,因为它在这里没有意义

namespace Service
{
  [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
  public class EchoService : IEchoContract
  ...

您还必须删除外部接口,因为这些接口也定义了两次(可能是您的原因)类最终也定义了两次)

If you see in your code you have a class called EchoSevice inside a class called EchoService

namespace Service
{
  class EchoService
  {
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    ...

Try removing the outer class as it has no meaning here

namespace Service
{
  [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
  public class EchoService : IEchoContract
  ...

You will have to remove your outer interface also, since those are also defined twice (probably the reason why your classes ended up defined twice as well)

森罗 2024-10-26 20:49:16

您在 EchoService 类中定义 EchoService 类 - 这是不可能的。只需删除外部的“EchoService 类”就可以了。

You define an EchoService class in the EchoService class - that is not possible. Just remove the outer "class EchoService" and you should be fine.

流云如水 2024-10-26 20:49:16

在 EchoService.cs 中,您无法调用内部类 EchoService,因为它上面已经有一个类 EchoService。您需要重命名其中之一。

In EchoService.cs, you can't call the internal class EchoService as you already have a class EchoService above it. You need to rename one of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文