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.
- 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
Here, I am doing self hosting with Console Application.
- Add System.ServiceModel library to the References.
- Add reference to the created Class Library Project, given above.
- Create Application Configuration file.
- <?xml version="1.0" encoding="utf-8" ?>
- <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();
-
- }
- }
- }
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)
- {
-
- }
-
- }
Add WebServiceClient to the Project folder.
Right click on Project Folder-> Select New -> Under Web Service-> Select Web Service Client.
Select Next.
Enter Service definition URL.
Click Browse.
Select WSDL document and click OK.
Now, proxy class will be added to the project.
- 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();
- }
- }
-
- }
Run the Application.