Browse DevX
Sign up for e-mail newsletters from DevX


EJBs for Everyone: Consume EJBs in .NET Using C# and a Web Service : Page 3

Getting your apps ready to interact with a J2EE application server is a daunting task for the average VB.NET or C# developer, but Visual MainWin uses Web services to simplify the process. Find out how and learn to write and consume EJBs using C# and a .NET Server.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Consume an EJB on a .NET Server
Some of you may have noticed a small problem. The C# code that consumes the EJB has to run on the J2EE application server! This seems to be a small disjoin, as you may prefer to have clean C# code running on a .NET server in a services or presentation tier consuming your EJB code running in a business logic tier, but you are tied to needing J2EE in the services and presentation tier too, which may break your design goals.

Here's one potential solution to this problem: Visual Studio allows you to add projects to a project. So, if the machine has both IIS and the J2EE application server, such as Weblogic 8.1 running on it, you can create a solution that has a Visual MainWin for C# project in it, exposing a public class that consumes the EJB, and a standard C# project that consumes this public class. The standard project runs on .NET and consumes the class directly. Unfortunately, this doesn't work (although it compiles and runs cleanly) because the application on the .NET server doesn't have the J2EE references, and as such crashes at runtime.

You want a logical separation of tiers, with the presentation tier running .NET and the business logic tier running J2EE. You also want the simplicity and ease of use with C# in the business logic tier.

Here is the best solution, and it's the obvious one: Web services.

In the above case, you built a Web application running on the same application server as your EJB that consumed the EJB. If you want a logical separation of the tiers as shown in Figure 1, with the presentation tier running .NET and the business logic tier running J2EE, but want the simplicity and ease of use with C# in the business logic tier, then you would build a C# Web service using Visual MainWin, and have that consume the EJB, exposing it's methods through a Web Services Webmethod. Your presentation tier component then simply consumes that Web service using SOAP.

To build a Web service using Visual MainWin for C#, create a new application and from the new application dialog box select 'New ASP.Net Web Service' from within the Visual MainWin for C# folder. This creates the common Microsoft 'Hello World' Web service. Add a reference to the EJB in exactly the same way as you did earlier in this article and then add the Webmethod shown below.

[WebMethod] public double getPE(string strTicker) { localhost.GetPERemote myProxy = new localhost.GetPERemote(); return myProxy.getPE(strTicker); }

Running the Web Service under Visual MainWin isn't very interesting as you don't get the test harness that .NET gives you, but it does give you the path to the WSDL, which is ultimately what you are interested in.

On your presentation tier, you can now create a standard Microsoft.NET application, Web application, or Web service that consumes this Web service by pointing it at this WSDL to create a Web reference in the standard way.

The download with this article includes the Weblogic Java code for the EJB as well as a .NET project that has a Web application consuming the EJB as well as this Web service and a simple client that consumes it.

So that's how you can use Visual MainWin to consume EJBs in your business logic tier. If necessary, you can further expose them to other tiers using Web services interop or through straightforward EJB references if the other tiers also run J2EE. The capability to consume EJBs now lies in the hands of your VB.NET and C# developers, freeing up your J2EE architects to concentrate on what they do best!

Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date