Basic usage

This example shows how easy SVConnect is in use.
The article discusses the simplest way to write, configure and consume
Windows Communication Foundation (WCF) service, using Visual Studio 2010
and Eclipse Platform

Creating and deploying a sample WCF Service

Let's create a WCF Service Application project. For the purpose of communication example we are going to use service and data contracts as also implementation provided by Visual Studio 2010 WCF Service Application project template. The project defines as follows:
  • Service and data contracts
[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

  • Service implementation
public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}
Let's deploy the application with a basicHttpBinding (configuration provided by default Web.config file) on ASP.net Web Development Server, port 35917.

As we hit F5 button the service page will be launched in the default web browser.
From now on the direct address to the service is
http://localhost:35917/Service1.svc
The "server" part of our architecture is done.
Let's stay in the debug mode and try to create Java client for that particular service.

Creating and deploying service client

Is is assumed that at this point you have successfully installed the SVConnectPlugin for Eclipse.

Download the SVConnect library archive from here and unzip it.
Launch Eclipse IDE and create a new Java project.

The next step is to include required libraries.
Right click on project->Build Path->Libraries->Add external jar

From the unzipped archive directory select

SVConnect-1.0.0.jar
It you want to use SVConnect outside the Android platform following HttpComponents dependencies must be added

commons-codec-1.4.jar
commons-logging-1.1.1.jar
httpclient-4.1.3.jar
httpclient-cache-4.1.3.jar
httpcore-4.1.4.jar
httpmime-4.1.3.jar
All required jars can be found in the archive (in httpcomponents directory)
Let's move on to plugin configuration

To configure the plugin go to Window › Preferences and from the list of features choose SVConnect

eclipsePreferences.png

Build path defines the directory in which all service client classes will be generated.
The convention is simple. The following path will be added to sources
{buildPath}/{serviceName}/{packagePath}
In our case all source files will be generated in
.svc/Service1/serviceinterface

When Java convention enabled flag is on, data contract classes properties
will be generated as private fields with lowercase-first names and Java getters and setters.
When Java convention enabled flag is off, data contract classes properties
will be generated as public fields with uppercase-first names (much like in C#).
In our case we're gonna use the classic java convention.

Let's now generate the client stub classes.
Right click on project->Add Service Reference
In the address textbox type http://localhost:35917/Service1.svc and click Go
The list of available services and operations will be created.
The last thing to do is to define the package name for generated classes (in our case serviceinterface).
Click Ok and we're done. Notice that given package was created and hooked up with project sources in .svc/Service1/serviceinterface directory

addServiceWindow.png

To manage created service references SVConnect Service Reference view must be activated.
To show the plugin service references list of current project
go to Window › Show view › Other... and from the tree choose SVConnect › Service References

eclipseAddView.png

As the result the following panel will pop out providing basic service references
management operations like creating new ones, and deleting existing ones.

eclipseView.png

The very last thing to do is to finally write a simple code connecting our java client to WCF service.
It's very simple and intuitive (much like using WCF service references in Visual Studio).

Let's create a Main class with
public static final void main(String[] args)
method.
Bear in mind that all classes we need can be found in the serviceinterface package.
Let's fill the main method with the code below:

/* create service client */
Service1Client client = new Service1Client();

/* invoke getData method */
String data = client.getData(15);
System.out.println(data);

/* create complex request */
CompositeType request = new CompositeType();
request.setBoolValue(true);
request.setStringValue("Lorem ipsum ");

/* invoke getDataUsingDataContract method */
CompositeType response = client.getDataUsingDataContract(request);
System.out.println(response.getStringValue());
System.out.println(response.getBoolValue());
Edit the Web.config file replacing the binding attribute with basicHttpBinding value.
After hitting Run as java application the following output will be generated.

You entered: 15
Lorem ipsum Suffix
true
Try adding some breakpoints in the Visual Studio IDE to see if it really works.
It's as simple as that.

Last edited Oct 18, 2012 at 7:27 AM by marochm, version 15

Comments

No comments yet.