Pages

Monday, June 15, 2009

Consuming WCF in Java(Java Client)

Hi,
Only basicHttpBindling works here we will get the proxy class. In wsHttpBinding No proxy class will get generated.


Java Client Code :-


import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.tempuri.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class WCFClient
{
      public static void main(String[] args)
      {
           
      
      URL a;
      try {
            a = new URL("http://localhost:1192/Service1.svc?wsdl");
     
                  Service1Locator locator;
                  try {
                        locator = new Service1Locator();
                        locator.getbasicHttpBinding_IService1(a);
                        IService1 service = new IService1Proxy().getIService1();
                        String result;
                        try {
                              result = service.getData(17);
                              System.out.println(result);
                        } catch (RemoteException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }
                       
                  } catch (ServiceException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                  }} catch (MalformedURLException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                  }
                }
}



In the server side WCF service app.config settings are given below



xml version="1.0"?>
<configuration>
    <configSections>
      <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
            <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          sectionGroup>
        sectionGroup>
      sectionGroup>
    configSections
    <appSettings/>
    <connectionStrings/>
    <system.web>
       
        <compilation debug="false">
          <assemblies>
            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          assemblies>
        compilation>
       
        <authentication mode="Windows" />
       
      <pages>
        <controls>
          <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        controls>
      pages>
      <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      httpHandlers>
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      httpModules>
    system.web>
    <system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <providerOption name="CompilerVersion" value="v3.5"/>
          <providerOption name="WarnAsError" value="false"/>
        compiler>
      compilers>
    system.codedom>
   
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules>
        <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      modules>
      <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      handlers>
    system.webServer>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0" proxyAddress="http://localhost:1192/Service1.svc?wsdl" />
      basicHttpBinding>
    bindings>
    <services>
      <service behaviorConfiguration="MainWCF.Service1Behavior" name="MainWCF.Service1">
        <endpoint address="" binding="basicHttpBinding"
          bindingConfiguration="NewBinding0" bindingName="basicHttpBinding"
          contract="MainWCF.IService1">
          <identity>
            <dns value="localhost" />
          identity>
        endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      service>
    services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MainWCF.Service1Behavior">
         
          <serviceMetadata httpGetEnabled="true"/>
         
          <serviceDebug includeExceptionDetailInFaults="false"/>
        behavior>
      serviceBehaviors>
    behaviors>
  system.serviceModel>
configuration>

0 comments: