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.
- 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 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) {}
- }
Add web service client to the Project folder.
Right click on Project Folder-> Select New -> Under Web Service-> Select Web Service Client.
Select Next.
Enter the Service definition URL.
Click Browse.
Select the WSDL document and click OK.
Now, the 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.