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 add System.ServiceModel library to the References.
Create a Class Library Project
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- namespace WcfService {
- [ServiceContract]
- public interface ISampleService {
- [OperationContract]
- string Message();
- }
- public class SampleService: ISampleService {
- public string Message() {
- return ".NET Web Service";
- }
- }
- }
Create Host Application for the Service
- Add System.ServiceModel library to the References.

- Add a reference to the created Class Library Project, given above.

- Create an Application Configuration file.
- < configuration > < system.serviceModel > < services > < service name = "WcfService.SampleService"
- behaviorConfiguration = "servicebehavior" > < endpoint address = ""
- binding = "basicHttpBinding"
- contract = "WcfService.ISampleService"
- 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>
- Create an Instance for ServiceHost Class to host the Service.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using WcfService;
- namespace SampleServiceHost {
- class Program {
- static void Main(string[] args) {
- ServiceHost hostobj = new ServiceHost(typeof(WcfService.SampleService));
- hostobj.Open();
- Console.WriteLine("Service Started");
- Console.Read();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using WcfService;
- namespace SampleServiceHost {
- class Program {
- static void Main(string[] args) {
- ServiceHost hostobj = new ServiceHost(typeof(WcfService.SampleService));
- hostobj.Open();
- Console.WriteLine("Service Started");
- Console.Read();
- }
- }
- }
Execute the 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.
Enter the project name.
Click 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.
Enter the class name.
Click Finish to create the client class.
Add main method to the client class.






- public class Client {
- public static void main(String[] args) {}
- }



- Import ISampleServiceProxy
- import org.tempuri.ISampleServiceProxy;
Create an instance of ISampleServiceProxyand and call the Service Method.
- import java.rmi.RemoteException;
- import org.tempuri.ISampleServiceProxy;
- public class Client {
- public static void main(String[] args) {
- ISampleServiceProxy obj = new ISampleServiceProxy();
- try {
- System.out.println(obj.message());
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- }
Manish JainPosted Aug 26, 2019, 1:17 AM
Nice.Very simple and easy way of explanation. Thank you so much!
limPosted Oct 23, 2016, 2:13 AM
Hi. Can kindly help to elaborate what data types incompatiablity i should take note of when consuming .NET web service with Java client? Thanks!
SubashPosted Sep 15, 2016, 12:31 AM
Useful one