locked
WCF ServiceContract Interface and .NET interface RRS feed

  • Question

  • User1315112726 posted

    What is the difference between servicecontract interface and normal interface in .NET.

    If we create a interface with 2 methods in .net then inherited class needs to implement two methods but methods in servicecontract does not need to implement.

    Please let me know the difference.

    Thanks,

    Saravana Prakash P.

    Thursday, May 30, 2013 3:30 PM

Answers

  • User220959680 posted

    difference between servicecontract interface and normal interface in .NET.

    There is no difference, both are same. Web Service methods are exposed/accessed through the contract.

    create a interface with 2 methods in .net then inherited class needs to implement two methods but methods in servicecontract does not need to implement.

    Service implementation class inherits the contract i.e., Interface and implements the service methods. Creating a WCF using Visual Studio creates an interface and the implementation by default.

    When WCF service application is created it creates an interface i.e., IService1.cs and the implementation code in Service1.cs as shown below.

    [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
        }
    
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
        // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    }
    

    Refer http://msdn.microsoft.com/en-us/library/ms731082.aspx and make sure that rest of the sections are read.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2013 4:51 PM

All replies

  • User220959680 posted

    difference between servicecontract interface and normal interface in .NET.

    There is no difference, both are same. Web Service methods are exposed/accessed through the contract.

    create a interface with 2 methods in .net then inherited class needs to implement two methods but methods in servicecontract does not need to implement.

    Service implementation class inherits the contract i.e., Interface and implements the service methods. Creating a WCF using Visual Studio creates an interface and the implementation by default.

    When WCF service application is created it creates an interface i.e., IService1.cs and the implementation code in Service1.cs as shown below.

    [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
        }
    
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
        // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    }
    

    Refer http://msdn.microsoft.com/en-us/library/ms731082.aspx and make sure that rest of the sections are read.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2013 4:51 PM
  • User-488622176 posted

    I'm afraid I do not understand your question entirely...

    Services have a contract and an implementation. The contract is implemented by an Interface in .NET. The service and service contract are abstract concepts & language/platform independent. Maybe this is causing the troubles.

    For service contracts, there is the ServiceContractAttribute to mark the service contract (interface) as a contract. More info can be found here : http://msdn.microsoft.com/en-us/library/ms731835.aspx

    If this does not answer your question, please provide some more info.

    Thursday, May 30, 2013 4:51 PM