user1632018
user1632018

Reputation: 2555

WCF Service- Does not implement interface member

I am following a guide on how to add authorization in a WCF service located here.

Now my problem is when I create the service and remove the .DoWork() method from it, I get an error that says:

'Testing.HelloService' does not implement interface member 'Testing.IHelloService.DoWork()

This is obviously because I removed it, but is it needed? In the guide it basically said to remove the .DoWork() method from it, so I am geussing the person who wrote it missed something.

When I create it service it adds the HelloService and IHelloService files to the project. Do I need to add changes to IHelloService?

Here is the code in HelloService.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web;
using System.ServiceModel.Activation;

namespace MLA_Test_Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in code, svc and config file together.
    [AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class HelloService : IHelloService
    {
        public string HelloWorld()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                return HttpContext.Current.User.Identity.Name;
            else
                return "Unauthenticated Person";
        }
    }
}

Here is the code from IHelloService.cs

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

namespace MLA_Test_Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        void DoWork();
    }
}

Upvotes: 0

Views: 10841

Answers (2)

Manish Mishra
Manish Mishra

Reputation: 12375

its simple C#, you are inheriting an interface, you must implement all the declared methods of it in the class.

DoWork() exists in your Interface, hence implement it in your HelloService class.

Moreover, only those methods will be visible to your WCF-Service client, which will be declared in OperationContract i.e. the Interface and is marked [OperationContract]

Upvotes: 2

Jay S
Jay S

Reputation: 7994

Your implementation needs to match your interface. If you don't want to implement the DoWork method, it needs to go from the implementation and the Interface.

In fact, you probably should just replace DoWork with the name of the method you actually want to invoke on the service and implement that method instead. It's supposed to serve as an example of how to initiate an operation on the WCF service.

Upvotes: 4

Related Questions