# Webservice client in client scope OK but Exception in gateway scope

**URL:** <https://forum.inductiveautomation.com/t/webservice-client-in-client-scope-ok-but-exception-in-gateway-scope/16730>\
**Category:** Module Development\
**Created:** [January 25, 2018, 1:38pm UTC](https://forum.inductiveautomation.com/t/webservice-client-in-client-scope-ok-but-exception-in-gateway-scope/16730 "2018-01-25T13:38:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [January 25, 2018, 1:38pm UTC](https://forum.inductiveautomation.com/t/webservice-client-in-client-scope-ok-but-exception-in-gateway-scope/16730/1 "2018-01-25T13:38:42Z")

</div>

I develop a module which use **apache.cxf** for a client webservices SOAP 1.2 (with authentification)

When I create the client connexion in the module **client scope** and use it, everything is **OK**.

```auto
public final static QName SERVICE_NAME = new QName("http://www.onvif.org/ver20/ptz/wsdl", "PTZService");
public final static QName PORT_NAME = new QName("http://www.onvif.org/ver20/ptz/wsdl", "PTZPort");

...
String user = "ONVIFClient1";
String password = "ONVIFClient1";
String endpointAddress = "http://10.60.21.10:580/onvif/ptz_service";
service = Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP12HTTP_BINDING, endpointAddress);
port = service.getPort(PTZ.class);
client = ClientProxy.getClient(port); <======== EXCEPTION
endpoint = client.getEndpoint();
...
some code to add authentification in SOAP envelop

```

When I try to create the client connexion in the module **gateway scope** , with the same code, I have the following **Exception** :  
(Note : both client and gateway are launched on the same machine)

```auto
java.lang.ClassCastException: com.sun.xml.internal.ws.client.sei.SEIStub cannot be cast to org.apache.cxf.frontend.ClientProxy
at org.apache.cxf.frontend.ClientProxy.getClient(ClientProxy.java:127)
```

Both client and gateway .pom have the dependency to common.pom which contain the right dependency to apache-cxf :

```auto
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-security</artifactId>
            <version>3.2.1</version>
        </dependency>
```

Any Idea about the source of this exception ???  
I doesn’t figure out why the behaviour is different in the client and the gateway scope.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 25, 2018, 1:59pm UTC](https://forum.inductiveautomation.com/t/webservice-client-in-client-scope-ok-but-exception-in-gateway-scope/16730/2 "2018-01-25T13:59:10Z")

</div>

I’d guess the behavior is different because the ClassLoader structure is different. In the client all code runs under a single ClassLoader. In the gateway, each module gets its own ClassLoader.

You appear to be generating a dynamic client from a WSDL at runtime, which likely entails some code generation and might be the cause of your issues. [This page](http://cxf.apache.org/docs/dynamic-clients.html) mentions something about generated classes being created under a new ClassLoader.

I don’t know enough about CXF to really help any further.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [January 25, 2018, 3:12pm UTC](https://forum.inductiveautomation.com/t/webservice-client-in-client-scope-ok-but-exception-in-gateway-scope/16730/3 "2018-01-25T15:12:59Z")

</div>

Thanks @Kevin.Herron for the clarification about classloader which make sense for the different behavior between client and gateway scope.

I solve my problem by creating the webservice client endpoint with

```auto
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(PTZ.class);
factory.setAddress("http://10.60.21.10:580/onvif/ptz_service");
port = (PTZ) factory.create();
client = ClientProxy.getClient(port);
endpoint = client.getEndpoint();
```

I didn’t khow exactly why, but it works.
