Java 客户端和WCF服务端访问原理和部署步骤(英文) 分享(转)

http://chaizhixue2005.blog.sohu.com/139840107.html

WCF’s fundamental communication mechanism is SOAP-based Web services. Because WCF implements Web services technologies defined by the WS-* specifications, other software which is based on SOAP and supports WS-* specifications can communicate with the WCF-based applications.

To build up a cross-platform WCF server, you can use Metro. Metro is a Web Services framework that provides tools and infrastructure to develop Web Services solutions for the end users and middleware developers. It depends on Java programming language. The latest version of Metro is 1.4.

In the development process of SCM Anywhere (a SCM tool, with fully integrated Version Control, Issue Tracking and Build Automation, developed with WCF and METRO/WSIT), we found that METRO is NOT as mature as WCF. There are lots of small issues in METRO/WSIT. Luckily, METRO is an open source project and keeps evolving all the time. Our experience is that if you find some features are not working properly in METRO, keep downloading the latest version from Java.net. Several weeks later, you may discover that the features are working properly.

To implement a Java client to communicate with the WCF server, you can follow the steps below:

1. Download METRO/WSIT from the home page of Metro: https://metro.dev.java.net.

2. Download Eclipse. We use Eclipse + Metro to develop Dynamsoft SCM Anywhere.

3. Install Metro by executing the command: java –jar metro-1_4.jar. The installation folder of Metro contains some documents, tools and samples. You can find the documents in the “docs” folder.

4. Use the C# project “WcfService1″ (provided in my WCF client and WCF service article) as the WCF server. Go to Property of the WCF project, and set the server port to one that is not occupied by other services. Here we used 8888 for example. In the “web.config” file, change the string “wsHttpBinding” to “basicHttpBinding”.

5. This is the key step. We use the wsimport tool included in Metro to generate the Java client code. Create a file named “service1.xml” and copy the following code to the file:

<bindings
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    wsdlLocaption="http://localhost:8888/Service1.svc?wsdl"
    xmlns="http://java.sun.com/xml/ns/jaxws">
    <bindings node="wsdl:definitions">
        <enableAsyncMapping>true</enableAsyncMapping>
    </bindings>
</bindings>

The parameter “enableAsyncMapping” means generating the asynchronous method communicating to WCF server. Save this file, and execute the following command:

bin\wsimport -extension -keep -Xnocompile -b TestService.xml http://localhost:8888/Service1.svc?wsdl

Then you can find two new directories in Metro folder: “org” and “com”. They are the generated Java code.

6. Open Eclipse IDE, create a new Java project named “SimpleWCFClient”, and copy the two new directories “org” and “com” to the “src” folder of the project. Refresh the project, and you can find that some new code files are in the project.

7. Create a test class named “WCFTest” and write the following code to the file:

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.tempuri.IService1;
import org.tempuri.Service1;

public class WCFTest {
    public static void main(String[] strArgs)
    {
        try
        {
            Service1 service1 = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org/", "Service1"));
            IService1 port = service1.getBasicHttpBindingIService1();
            ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
            int input = 100;
            String strOutput = port.getData(input);
            System.out.println(strOutput);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

8. Four .jar files need to be added to the Java project. You can get these files in the “lib” folder of Metro: webservices-api.jar, webservices-extra.jar, webservices-extra-api.jar, webservices-rt.jar. Then go to Property of the project and add these jars to the project.

9. Compile and run the Java project. If the Eclipse console outputs “You entered: 100″, congratulations, you are successful.

You can download these code from here. When you are familiar with these, you will find it’s very convenient to write a Java application communicating with a WCF client.

Links:
Previous article >>>>: WCF client and WCF service
Next article >>>>: Data types between Java and WCF
WCF & Java Interop series home page: WCF & Java Interop

5 responses so far

5 Responses to “Java client and WCF server”

  1. # Anaristoson 04 May 2009 at 1:44 am

    I have implemented both the VWD Server/Client and the WCF Server/Java Client schemes. The first one works just fine, as I stated before. However, with the second I have run into a big problem. My Java client runs into a “Permission denied: connect.” wall when attempting to access the WSDL. I thought that, since I am running under Vista (6SP1) that perhaps it was UAC that was causing the problem, but that is not it. Nor was it a binding mismatch. I think that because the Java side is coming in through a remote socket that Windows authentication is blocking the access. Perhaps you have some suggestions as to how to get around this?

  2. # Anaristoson 04 May 2009 at 4:56 pm

    Here is something interesting. I imported the Eclipse project into Netbeans 6.5.1 and it ran perfectly. So the problem is with Eclipse.

  3. # Julisaon 04 Jun 2009 at 9:41 pm

    It is possible to use javax.xml.ws.Dispatch to communicating with WCF Service?

  4. # NItinon 08 Sep 2009 at 7:55 am

    How would you create a java client when you have a WCF service running on net tcp binding.

  5. # Ronakon 13 Nov 2009 at 7:24 am

    Thanks for this really good Tutorial. I am having some issue right now. I am able to generate the proxy classes following your step.

    Problem 1: All the strings in Method are JAXBElement

    Solution i have found so far is using this bind.xml file instead of yours

    Is there any way i can combine both of yours and this one?

    Problem 2: I am getting NullPointerException, when execution client code..It occurs in the first line in Main function.

    Exception:
    java.lang.NullPointerException
    at com.sun.xml.ws.model.wsdl.WSDLOperationImpl.freez(WSDLOperationImpl.java:147)
    at com.sun.xml.ws.model.wsdl.WSDLPortTypeImpl.freeze(WSDLPortTypeImpl.java:91)
    at com.sun.xml.ws.model.wsdl.WSDLBoundPortTypeImpl.freeze(WSDLBoundPortTypeImpl.java:215)
    at com.sun.xml.ws.model.wsdl.WSDLModelImpl.freeze(WSDLModelImpl.java:241)
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:147)
    at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:265)
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:228)
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:176)
    at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:104)
    at javax.xml.ws.Service.(Unknown Source)
    at org.tempuri.BPAExecutionServerWCFServer.(BPAExecutionServerWCFServer.java:42)
    at WCF.WCFTest.main(WCFTest.java:20)

    Code:

    package WCF;

    import java.net.URL;

    import javax.xml.namespace.QName;
    import javax.xml.ws.BindingProvider;

    import org.datacontract.schemas._2004._07.automate.ClientType;
    import org.datacontract.schemas._2004._07.automate_messages.OpenSessionRequestMessage;
    import org.datacontract.schemas._2004._07.automate_messages.OpenSessionResponseMessage;
    import org.tempuri.IBPAExecutionServerHTTPServiceContract;
    import org.tempuri.BPAExecutionServerWCFServer;;

    public class WCFTest {
    public static void main(String[] strArgs)
    {
    try
    {
    BPAExecutionServerWCFServer service1 = new BPAExecutionServerWCFServer(new URL(”http://209.223.157.111:7102/BPAExecutionServer/?wsdl”), new QName(”http://tempuri.org/”, “BPAExecutionServerWCFServer”));
    IBPAExecutionServerHTTPServiceContract port = service1.getBasicHttpBindingIBPAExecutionServerHTTPServiceContract();
    ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, “http://209.223.157.111:7102/BPAExecutionServer/?wsdl”);
    OpenSessionRequestMessage request = new OpenSessionRequestMessage();
    request.setAttemptRegistration(true);
    request.setClientType(ClientType.PROCESS_AGENT);

    OpenSessionResponseMessage strOutput = port.openSessionRequest(request);
    System.out.println(strOutput);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

原文地址:https://www.cnblogs.com/quietwalk/p/2255120.html