Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to maintain 2 interfaces in single wcf service application?

like IService1
IService2
Posted
Updated 3-Sep-13 23:55pm
v4
Comments
Rob Philpott 4-Sep-13 6:31am    
That means nothing. Please explain further what you want to do.

1 solution

Hi
The solution for your question is depend in the solution you are trying to achieve by
separating the service contract to 2 different interfaces.
In the following example i have chosen the simple answer to that problem
The current solution depend on the fact that WCF based on Service Contracts Implemented via Interfaces and exposed to the world via Endpoints (with the ABC properties)
Basically the Service Implementation does not exposed to the end user.
So we can implement the 2 different Service Contract in the same service Implementation
And expose it as 2 End Points
Check the following code:

[ServiceContract]
    public interface IFooService
    {
        [OperationContract]
        void Foo();

    }

    [ServiceContract]
    public interface  IBarService
    {
        [OperationContract]
        void Bar();
    }
    
//Service Implementation 
    public class Service : IFooService, IBarService
    {
        //Foo Service Implementation
        public void Foo()
        {
            Console.WriteLine("Foo Service Method Call"); 
        }

        //Bar Service Implementation
        public void Bar()
        {
            Console.WriteLine("Bar Service Method Call");
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (ServiceHost host = new ServiceHost(typeof (Service)))
                {
                    host.Open();

                    Console.WriteLine("S E R V E R");

                    Console.WriteLine("Press any key to shutdown");
                
                    Console.ReadKey();

                    host.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }


<configuration>
  <system.servicemodel>
        <behaviors>
              <servicebehaviors>
                    <behavior name="ServiceBehavior">
                        <servicemetadata httpgetenabled="true" />
                    </behavior>
                    <behavior name="">
                          <servicemetadata httpgetenabled="true" httpsgetenabled="true" />
                          <servicedebug includeexceptiondetailinfaults="false" />
                    </behavior>
              </servicebehaviors>
        </behaviors>
        <services>
              <service behaviorconfiguration="ServiceBehavior" name="ConsoleApplication2.Service">
                    <endpoint address="Foo" binding="basicHttpBinding" name="Foo">
                          contract="ConsoleApplication2.IFooService" />
                    <endpoint address="Bar" binding="basicHttpBinding" name="Bar">
                          contract="ConsoleApplication2.IBarService" />
                    <host>
                          <baseaddresses>
                                <add baseaddress="http://localhost:8733/Design_Time_Addresses/ConsoleApplication2/Service" />
                          </baseaddresses>
                    </host>
              </endpoint></endpoint></service>
             
        </services>
    </system.servicemodel>
</configuration>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900