修饰符'public'对此项无效

时间:2012-03-15 22:38:02

标签: c# asp.net web-services

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface HelloWorldService
    {
        [OperationContract]
        [WebGet(UriTemplate = "")]

        public string HelloWorld() //here
        {
            return "Hello world!";
        }
    }
}

我收到此错误:

Error   1   The modifier 'public' is not valid for this item

这就是我在svc.cs文件中的内容

public class Service1 : HelloWorldService
{
    public string HelloWorld()
    {
        return string.Format("Hello World!");
    }
}

这实际上来自我的大学的教程:

与我们首先为我们的服务创建接口的上周不同,我们只是在第一个示例中创建WCF服务对象。这是为了在教程中节省时间。最佳实践意味着我们应该为所有服务创建接口,这些接口与定义的服务合同相关。首先在Visual Studio 2008中创建一个新项目(记得添加System.ServiceModel和System.ServiceModel.Web引用,以及这两个命名空间的using声明),并添加以下类定义:

[ServiceContract]
public class HelloWorldService
{
[OperationContract]
[WebGet(UriTemplate="")]
public string HelloWorld()
{
return "Hello world!";
}
}

您应该熟悉上周教程中此WCF服务的基本结构。区别在于我们添加到方法的额外属性: [WebGet(UriTemplate = “”)] 此属性声明服务接收HTTP GET消息(WebGet部分),并且URL的其余部分与服务端点无关(在后面的示例之后,这将变得更加清晰)。要托管此服务,我们需要以下主要应用程序:

4 个答案:

答案 0 :(得分:6)

interface声明中的成员不能拥有访问修饰符。它们隐含地具有与包含interface相同的可访问性。如果您可以访问界面,则可以访问其所有成员

答案 1 :(得分:3)

您无法在界面中指定范围或方法主体。试试这个:

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface HelloWorldService
    {
        [OperationContract]
        [WebGet(UriTemplate = "")]

        string HelloWorld();
    }
}

答案 2 :(得分:1)

来自文档:

  

直接在命名空间内声明的接口可以声明为   公共或内部,就像类和结构一样,接口   默认为内部访问。界面成员始终是公开的   因为接口的目的是使其他类型能够访问   一个类或结构。没有访问修饰符可以应用于接口   成员。

来源:http://msdn.microsoft.com/en-us/library/ms173121%28v=vs.100%29.aspx

答案 3 :(得分:0)

您无法在C#接口上声明可访问性。您的界面方法也不能实现。

相关问题