locked
Why do we need an interface? RRS feed

  • Question

  • Hi All,
     
    this question seems to be very naive however I am interested. The question is " Can't we write a wcf service where services can be exposed with out writing an interface.. ie. we generally implement the contract by defining a class. Can I write a class directly without specifying contract and expose the services.
     
    Is wcf services can be designed without interfaces?
    Thursday, February 19, 2009 12:14 PM

Answers

  • There is nothing stopping you from doing this.  You could put the ServiceContract and OperationContract attributes directly on your service and it will work just fine.  The wsdl will pull out the contract from this.  You just specify the class name as the "contract" on the endpoint instead of the interface name

    It is generally recommended to use interfaces though for a variety of reasons including:
    1. Since the endpoints only require interface, it means better seperation of concerns
    2. Multiple services can share a contract
    3. Easier readabiliy of what a service can do
    4. A service can inherit multiple contracts
    5. The interface can be used in a shared library between the client and server instead of using a proxy.
    • Marked as answer by Marco Zhou Tuesday, February 24, 2009 8:25 AM
    Thursday, February 19, 2009 2:15 PM
  •  You can completely avoid custom interfaces if you like and use pure messaging by leveraging the Message class and default interfaces available in WCF.

    Example.

    Server Side

    [ServiceContract]
    public class Service
    {
        [OperationContract]
        public string Echo(string input)
        {
            ......
        }
    }


    Client Side

     

    using System.ServiceModel;  
     
    string xml = "<test></test>";  
                byte[] data = Encoding.UTF8.GetBytes(xml);  
     
                NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);  
                EndpointAddress address = new EndpointAddress("net.tcp://YOURENDPOINT");  
     
                using (ChannelFactory<System.ServiceModel.Channels.IRequestChannel> factory = new ChannelFactory<System.ServiceModel.Channels.IRequestChannel>(binding, address))  
                {  
     
                    factory.Endpoint.Contract.SessionMode = SessionMode.Allowed;  
     
                    System.ServiceModel.Channels.IRequestChannel channel = factory.CreateChannel();  
                    System.Xml.XmlDictionaryReader xdr = System.Xml.XmlDictionaryReader.CreateTextReader(data, System.Xml.XmlDictionaryReaderQuotas.Max);  
     
                    System.ServiceModel.Channels.Message msg = System.ServiceModel.Channels.Message.CreateMessage(System.ServiceModel.Channels.MessageVersion.Default, "Echo", xdr);  
     
                    using (System.ServiceModel.Channels.Message reply = channel.Request(msg))  
                    {  
                        string result = reply.ToString();  
                    }  
                }  
     

     


    Raman http://www.kooveli.com
    • Marked as answer by Marco Zhou Tuesday, February 24, 2009 9:39 AM
    Thursday, February 19, 2009 3:48 PM

All replies

  • There is nothing stopping you from doing this.  You could put the ServiceContract and OperationContract attributes directly on your service and it will work just fine.  The wsdl will pull out the contract from this.  You just specify the class name as the "contract" on the endpoint instead of the interface name

    It is generally recommended to use interfaces though for a variety of reasons including:
    1. Since the endpoints only require interface, it means better seperation of concerns
    2. Multiple services can share a contract
    3. Easier readabiliy of what a service can do
    4. A service can inherit multiple contracts
    5. The interface can be used in a shared library between the client and server instead of using a proxy.
    • Marked as answer by Marco Zhou Tuesday, February 24, 2009 8:25 AM
    Thursday, February 19, 2009 2:15 PM
  •  You can completely avoid custom interfaces if you like and use pure messaging by leveraging the Message class and default interfaces available in WCF.

    Example.

    Server Side

    [ServiceContract]
    public class Service
    {
        [OperationContract]
        public string Echo(string input)
        {
            ......
        }
    }


    Client Side

     

    using System.ServiceModel;  
     
    string xml = "<test></test>";  
                byte[] data = Encoding.UTF8.GetBytes(xml);  
     
                NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);  
                EndpointAddress address = new EndpointAddress("net.tcp://YOURENDPOINT");  
     
                using (ChannelFactory<System.ServiceModel.Channels.IRequestChannel> factory = new ChannelFactory<System.ServiceModel.Channels.IRequestChannel>(binding, address))  
                {  
     
                    factory.Endpoint.Contract.SessionMode = SessionMode.Allowed;  
     
                    System.ServiceModel.Channels.IRequestChannel channel = factory.CreateChannel();  
                    System.Xml.XmlDictionaryReader xdr = System.Xml.XmlDictionaryReader.CreateTextReader(data, System.Xml.XmlDictionaryReaderQuotas.Max);  
     
                    System.ServiceModel.Channels.Message msg = System.ServiceModel.Channels.Message.CreateMessage(System.ServiceModel.Channels.MessageVersion.Default, "Echo", xdr);  
     
                    using (System.ServiceModel.Channels.Message reply = channel.Request(msg))  
                    {  
                        string result = reply.ToString();  
                    }  
                }  
     

     


    Raman http://www.kooveli.com
    • Marked as answer by Marco Zhou Tuesday, February 24, 2009 9:39 AM
    Thursday, February 19, 2009 3:48 PM