Introduction.
This article will illustrate how to send binary data from Windows Communication Foundation (WCF) to Oracle Application Server (Oracle AS) using Web Services specifications specifically MTOM standard specification, thus achieving interoperability between the two platforms. Some days ago, I blogged information in CSharpCorner blogs about MTOM specifications.
Implementing MTOM in Windows Communication Foundation (WCF). Creating the server side.
The first to do is to create a solution with a console application and name it WCF_MTOM_Server. Then go to Project | Add New Item and from the Add New Item windows, choose the WCF Service and name the service BinaryFileTransferService to get a very large binary document. Now, let's define the service contract as shown in Listing 1.
[ServiceContract()]
public interface IBinaryFileTransferService
{
[OperationContract]
byte[] GetFile(string strPath);
}
Listing 1. The BinaryFileTransferService service contract.
Afterwards, you need to create the service which implements the interface as shown in Listing 2.
public class BinaryFileTransferService : IBinaryFileTransferService
{
public byte[] GetFile(string strPath)
{
return File.ReadAllBytes(strPath);
}
}
Listing 2. The BinaryFileTransferService service implementation.
Now, we have the service host code generated by Visual Studio.NET. Listing 3.
internal class BinaryFileTransferServiceHost
{
internal static ServiceHost myServiceHost = null;
internal static void StartService()
{
// Consider putting the baseAddress in the configuration system
// and getting it here with AppSettings
Uri baseAddress = new Uri("http://localhost:8080/WCF_MTOM_Server/BinaryFileTransferService");
// Instantiate new ServiceHost
myServiceHost = new ServiceHost(typeof(BinaryFileTransferService), baseAddress);
myServiceHost.Open();
}
internal static void StopService()
{
// Call StopService from your shutdown logic (i.e. dispose method)
if (myServiceHost.State != CommunicationState.Closed)
myServiceHost.Close();
}
}
Listing 3. The Service Host instantiation.
In the application configuration file, we need to set the MTOM Encoding Binding element as part of our customized binding highlighted in yellow as show in Listing 4.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
<service name="WCF_MTOM_Server.BinaryFileTransferService" behaviorConfiguration="BinaryFileTransferServiceBeh">
<endpoint contract="WCF_MTOM_Server.IBinaryFileTransferService" binding="customBinding" bindingConfiguration="MTOMCustomBinding" />
</service>
</services>
<bindings>
<customBinding>
<binding name="MTOMCustomBinding">
<mtomMessageEncoding messageVersion="Soap12"/>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="true" keepAliveEnabled="true"
maxBufferSize="65536" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"/>
</binding>
</customBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="BinaryFileTransferServiceBeh" >
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Listing 4. The configuration file.
Implementing the client in Oracle.
The first step is to create a service proxy through the Web Service Proxy Wizard in JDeveloper. In order to configure MTOM in the client generated by the Wizard, you need to set the property MTOM_SUPPORT to true in the Web proxy or in the configuration file as illustrated in Listing 5 (highlighted in yellow).
public static void main(String[] args)
{
try
{
mtom_client.proxy.CustomBinding_IBinaryFileTransferServiceClient objProxyFactory = new mtom_client.proxy.CustomBinding_IBinaryFileTransferServiceClient();
IBinaryFileTransferService objProxy = objProxyFactory.getPort();
((OracleStub) objProxy)._setProperty(ClientConstants.MTOM_SUPPORT, true);
byte[] barrFile = objProxy.getFile("C:\\temp\\Communication\\In\\instal.iso");
int nLength = barrFile.length;
System.out.println("Length in bytes is "+nLength);
} catch (Exception ex)
{
ex.printStackTrace();
}
}
Listing 5. Invocation to the service proxy.
Conclusion
This article has shown the implementation techniques to send binary data between applications running in different platforms (Microsoft.NET and Oracle AS) using standard protocols.