L A

L A

  • NA
  • 170
  • 168.2k

WCF service throws 'InvalidOperationException'

Jan 31 2018 2:35 PM
Hi, i was trying to create basic WCF service and access it.
WCF Appl - ISampleService.cs
  1. using System.ServiceModel;  
  2.   
  3. namespace MultipleSeviceContractAppl  
  4. {  
  5.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISampleService" in both code and config file together.  
  6.     [ServiceContract]  
  7.     public interface ISampleService  
  8.     {  
  9.         [OperationContract]  
  10.         string DoWork();  
  11.     }  
  12. }  
WCF Appl - SampleService.cs 
  1. namespace MultipleSeviceContractAppl  
  2. {  
  3.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SampleService" in both code and config file together.  
  4.     public class SampleService : ISampleService  
  5.     {  
  6.         public string DoWork()  
  7.         {  
  8.             return "Message from WCFservice";  
  9.         }  
  10.     }  
  11. }  
WCF Appl - App.config
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  

  4.     <behaviors>  
  5.       <serviceBehaviors>  
  6.         <behavior name="mexBehaviour">  
  7.           <serviceMetadata httpGetEnabled="false"/>  
  8.           <serviceDebug includeExceptionDetailInFaults="false" />  
  9.         </behavior>  
  10.       </serviceBehaviors>  
  11.     </behaviors>
  12.   
  13.     <services>  
  14.       <service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">  
  15.         <endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">  
  16.         </endpoint>  
  17.         <host>  
  18.           <baseAddresses>  
  19.             <add baseAddress="http://localhost:8733/"/>  <!--For metadata exchange-->
  20.             <add baseAddress="net.tcp://localhost:8737/" />  <!--Endpoint, netTCP binding-->
  21.           </baseAddresses>  
  22.         </host>  
  23.       </service>  
  24.     </services>
  25.   
  26.   </system.serviceModel>  
  27. </configuration>  
Console - Host WCF Appl
Added service refrence to ServiceModel added using System.ServiceModel; in program.cs file.
  1. using System;  
  2. using System.ServiceModel;  
  3. namespace ConsumeWCFApplicationAppl  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             using (ServiceHost host = new ServiceHost(typeof(MultipleSeviceContractAppl.SampleService)))  
  10.             {  
  11.                 host.Open();  
  12.                 Console.WriteLine("Host started @" + DateTime.Now.ToString());  
  13.                 Console.ReadKey();  
  14.             }  
  15.         }  
  16.     }  
  17. }  
On host application at line # 11, following exception was thrown.
 
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll
Additional information: Service 'MultipleSeviceContractAppl.SampleService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
Help me to figure out my mistake. Thanks