Consuming WCF Service In Java

We know that .NET and Java are the two different platforms. Hence, in some cases, the Java Application wants to consume .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 datatable type is specific for .NET and it wont be accepted by JAVA).

Create 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. {  
  8.     [ServiceContract]  
  9.     public interface ISampleService  
  10.     {  
  11.         [OperationContract]  
  12.         string Message();  
  13.     }  
  14.   
  15.     public class SampleService : ISampleService  
  16.     {  
  17.         public string Message()  
  18.         {  
  19.             return ".NET Web Service";  
  20.         }  
  21.     }  
  22. }  
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 reference to the created Class Library Project, given above.

    reference

  3. Create Application Configuration file.
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <configuration>  
    3.   <system.serviceModel>  
    4.     <services>  
    5.       <service name="WcfService.SampleService" behaviorConfiguration="servicebehavior">  
    6.         <endpoint address="" binding="basicHttpBinding" contract="WcfService.ISampleService" bindingConfiguration="basicbinding"></endpoint>  
    7.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>  
    8.         <host>  
    9.           <baseAddresses>  
    10.             <add baseAddress="http://localhost:8080/SampleService" />  
    11.           </baseAddresses>  
    12.         </host>  
    13.       </service>  
    14.         
    15.     </services>  
    16.     <behaviors>  
    17.       <serviceBehaviors>  
    18.         <behavior name="servicebehavior">  
    19.           <serviceMetadata httpGetEnabled="true"/>  
    20.         </behavior>  
    21.       </serviceBehaviors>  
    22.     </behaviors>  
    23.   </system.serviceModel>  
    24. </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. {  
    9.     class Program  
    10.     {  
    11.         static void Main(string[] args)  
    12.         {  
    13.             ServiceHost hostobj = new ServiceHost(typeof(WcfService.SampleService));  
    14.             hostobj.Open();  
    15.             Console.WriteLine("Service Started");  
    16.             Console.Read();  
    17.   
    18.         }  
    19.     }  
    20. }  

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.       
  3.     public static void main(String[] args)  
  4.     {  
  5.           
  6.     }  
  7.   
  8. }  
Add WebServiceClient to the Project folder.

Right click on Project Folder-> Select New -> Under Web Service-> Select Web Service Client.

New

Select Next.

Enter Service definition URL.

Service definition URL

Click Browse.

Browse

Select WSDL document and click OK.

Now, 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.   
  3. import org.tempuri.ISampleServiceProxy;  
  4.   
  5.   
  6. public class Client {  
  7.       
  8.     public static void main(String[] args)  
  9.     {  
  10.         ISampleServiceProxy obj=new ISampleServiceProxy();  
  11.         try {  
  12.             System.out.println(obj.message());  
  13.         } catch (RemoteException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }  
  17.     }  
  18.   
  19. }  
Run the Application.

 

Ebook Download
View all
Learn
View all