G Gr
G Gr

Reputation: 6090

The modifier 'public' is not valid for this item

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!";
        }
    }
}

I get this error:

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

This is what I have in my svc.cs file

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

This is actually from a tutorial from my uni:

Unlike last week, where we created interfaces for our services first, we are simply going to create WCF service objects in this first example. This is to save on time within the tutorial. Best practice means that we should be creating interfaces for all our services, which relates to the defined service contract. First of all create a new project within Visual Studio 2008 (remember to add the System.ServiceModel and System.ServiceModel.Web references, and the using declaration for both of these namespaces), and add the following class definition:

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

You should be familiar with the basic structure of this WCF service from last week’s tutorial. The difference lies in the extra attribute we have added to the method: [WebGet(UriTemplate="")] This attribute states that the service receives HTTP GET messages (the WebGet part), and that the rest of the URL is not relevant to the service end point (this will become clearer after the later examples). To host this service, we require the following main application:

Upvotes: 2

Views: 9951

Answers (4)

CodeZombie
CodeZombie

Reputation: 5377

From the documentation:

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

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

Upvotes: 1

Kendall Frey
Kendall Frey

Reputation: 44374

You can't specify scope or method bodies in an interface. Try this:

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();
    }
}

Upvotes: 3

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

You can't declare accessibility on C# interfaces. Nor can your interface methods have an implementation.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755229

Members on an interface declaration can't have an access modifier. They implicitly have the same accessibility as the containing interface. If you can access an interface, you can access all it's members

Upvotes: 6

Related Questions