Consuming WCF Service In Java

Introduction 

 
We know that .NET and Java are the two different platforms. Hence, in some cases, the Java Application wants to consume the .NET WCF Service.
  
Even I came across the same thing, so I hope this article may help you.
 
Please make sure of the datatype compatibility between both Java & .NET.(Example data table type is specific for .NET, and it won't be accepted by JAVA).
 

Create a Class Library Project

 
Create a class library project add System.ServiceModel library to the References.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6. namespace WcfService {  
  7.  [ServiceContract]  
  8.  public interface ISampleService {  
  9.   [OperationContract]  
  10.   string Message();  
  11.  }  
  12.  public class SampleService: ISampleService {  
  13.   public string Message() {  
  14.    return ".NET Web Service";  
  15.   }  
  16.  }  
  17. }   

 
Create Host Application for the Service

 
Here, I am doing self-hosting with Console Application. 
  1. Add System.ServiceModel library to the References. 
     
    System.ServiceModel library
     
  2. Add a reference to the created Class Library Project, given above.
     
    reference
     
  3. Create an Application Configuration file.
    1. < configuration > < system.serviceModel > < services > < service name = "WcfService.SampleService"  
    2. behaviorConfiguration = "servicebehavior" > < endpoint address = ""  
    3. binding = "basicHttpBinding"  
    4. contract = "WcfService.ISampleService"  
    5. bindingConfiguration = "basicbinding" > < /endpoint><endpoint address="mex"binding="mexHttpBinding"contract="IMetadataExchange"></endpoint > < host > < baseAddresses > < add baseAddress = "http://localhost:8080/SampleService" / > < /baseAddresses></host > < /service></services > < behaviors > < serviceBehaviors > < behavior name = "servicebehavior" > < serviceMetadata httpGetEnabled = "true" / > < /behavior></serviceBehaviors > < /behaviors></system.serviceModel > < /configuration>  
  4. Create an Instance for ServiceHost Class to host the Service.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.ServiceModel;  
    6. using WcfService;  
    7. namespace SampleServiceHost {  
    8.  class Program {  
    9.   static void Main(string[] args) {  
    10.    ServiceHost hostobj = new ServiceHost(typeof(WcfService.SampleService));  
    11.    hostobj.Open();  
    12.    Console.WriteLine("Service Started");  
    13.    Console.Read();  
    14.   
    15.   }  
    16.  }  
    17. }  
    18. using System;  
    19. using System.Collections.Generic;  
    20. using System.Linq;  
    21. using System.Text;  
    22. using System.ServiceModel;  
    23. using WcfService;  
    24. namespace SampleServiceHost {  
    25.  class Program {  
    26.   static void Main(string[] args) {  
    27.    ServiceHost hostobj = new ServiceHost(typeof(WcfService.SampleService));  
    28.    hostobj.Open();  
    29.    Console.WriteLine("Service Started");  
    30.    Console.Read();  
    31.   }  
    32.  }  
    33. }  
Execute the Service.
 
Service
 
Now Service starts successfully.
 
You need to consume the Service in Java Application. Here, I am using Eclipse IDE. Fire Eclipse Select->New->Java Project.
 
New
 
Enter the project name.
 
Project Name
 
Click Next->
 
Next
 
Click Finish to create the project.
 
Now, add the Class file to the project. In order to do this, right-click on Project in Package Explorer -> new -> Class.
 
Now add the Class file
 
Enter the class name.
 
Class
 
Click Finish to create the client class.
 
Add main method to the client class.
  1. public class Client {  
  2.  public static void main(String[] args) {}  
  3. }   
Add web service client to the Project folder.
 
Right click on Project Folder-> Select New -> Under Web Service-> Select Web Service Client.
 
New
 
Select Next.
 
Enter the Service definition URL.
 
Service definition URL
 
Click Browse.
 
Browse
 
Select the WSDL document and click OK.
 
Now, the proxy class will be added to the project.
 
Proxy class   
  1. Import ISampleServiceProxy  
  2. import org.tempuri.ISampleServiceProxy;  
Create an instance of ISampleServiceProxyand and call the Service Method.
  1. import java.rmi.RemoteException;  
  2. import org.tempuri.ISampleServiceProxy;  
  3. public class Client {  
  4.  public static void main(String[] args) {  
  5.   ISampleServiceProxy obj = new ISampleServiceProxy();  
  6.   try {  
  7.    System.out.println(obj.message());  
  8.   } catch (RemoteException e) {  
  9.    e.printStackTrace();  
  10.   }  
  11.  }  
  12. }   
Run the Application.