Blog from Saravanan Arumugam

Let us talk about Technologies

The OperationContractAttribute declared on method ‘Add’ in type ‘TransactionPractice.CalculatorService’ is invalid


I got this exception accidentally since I placed my OperationContract attribute in the type declaration and kept the ServiceContract attribute in Interface.

Exception

The OperationContractAttribute declared on method ‘Add’ in type ‘TransactionPractice.CalculatorService’ is invalid. OperationContractAttributes are only valid on methods that are declared in a type that has ServiceContractAttribute. Either add ServiceContractAttribute to type ‘TransactionPractice.CalculatorService’ or remove OperationContractAttribute from method ‘Add’.

Here is the code where I misplaced the OperationContract and ServiceContract attributes.

  [ServiceContract()]
    public interface ICalculatorService
    {        
        long Add(int a, int b);
 
    }
 
    public class CalculatorService: ICalculatorService
    {
        [WebGet(ResponseFormat = WebMessageFormat.Xml)]       
        [OperationContract()]
        public long Add(int a, int b)
        {
            return (a + b);
        }
     }

 

Solution

Solution is simple. Just follow the instruction in the exception detail itself.

Either add ServiceContractAttribute to type ‘TransactionPractice.CalculatorService’ or remove OperationContractAttribute from method ‘Add’.

Advertisements

One response to “The OperationContractAttribute declared on method ‘Add’ in type ‘TransactionPractice.CalculatorService’ is invalid

  1. ciaonet.altervista.org April 28, 2013 at 2:14 am

    Your way of telling the whole thing in this article is genuinely fastidious, all be capable of simply be aware of it, Thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: