Introduction
Have you ever wished you could run ASP.NET applications on Linux®, without having to rewrite your code or leave the Visual Studio® development environment? In this article, we show you how to port Steve Clements' AJAX ASP.NET RSS Reader to native Java™ and deploy it to Apache Tomcat on Linux. We also show you how to add an AnimationExtender and a HoverMenu from the AJAX Control Toolkit in Visual Studio, while targeting Java.
How is this possible? The answer is to use Grasshopper, a free Visual Studio-based SDK built by Mainsoft in collaboration with the Mono open source community. Grasshopper 2.2 introduces full support for AJAX and seamlessly integrates with ASP.NET 2.0 AJAX Extensions and AJAX Control Toolkit, so you can add popular features such as partial-page rendering, Accordion navigation, and rounded corners, by simply adding a few server controls.
In this article, we present how to:
- Analyze the source code to identify possible porting issues.
- Convert the RSS Reader and RSS Toolkit to Grasshopper project types.
- Build the ASP.NET solution for Java.
- Use the AJAX Control Toolkit to add rich client-side functionality.
- Deploy the ASP.NET application on Linux.
Analyze the Source Code
First, we identify the third-party dependencies in the source code. The RSS Reader has two: AJAX Control Toolkit and ASP.NET RSS Toolkit, both of which are open source projects hosted on CodePlex. Since these are .NET libraries, they won't run on Java until we cross-compile them to the Java platform.
There are various strategies for porting third-party dependencies to Java. You can:
- Check our samples and code blocks to see if the component has already been ported.
- Port the component to Java EE yourself, if the source code has a permissive license.
- Check if the vendor has a Java EE alternative. Many third-party components are available for both the .NET and Java platforms, including Crystal Reports, Corda OptiMap, and Software FX.
The AJAX Control Toolkit belongs to the first category and will be discussed below, while the RSS Toolkit, licensed under the permissive Microsoft Public License (Ms-PL), will be ported here.
Converting the Web Application
Let the porting process begin!
First, we install Grasshopper, a free Visual Studio add-on from Mainsoft that allows all the magic between .NET and Java to happen. Grasshopper is in essence a cross-compiler that turns .NET bytecode (MSIL) into Java bytecode. The build output is a binary *.jar file, not Java source code, although you could run the bytecode through a Java disassembler if you wanted to. Grasshopper integrates seamlessly with Visual Studio's build system and triggers its own code validation, code generation, and deployment build steps after Microsoft's regular build to MSIL is finished. On top of that, Grasshopper comes bundled with the Apache Tomcat Web application server, so there is no need to deploy test builds on a remote Web server during development.
The RSS Reader project uses the Visual Studio Web Site format, which was the default project format in Visual Studio 2005 (sans SP1). In order for Grasshopper to work with the project, we first convert the Web Site into the Web Application project type:
In Visual Studio, create a new "RSSReader" project from the Visual C# for Java EE > ASP.NET AJAX-enabled Web application template. Grasshopper adds two new project types to Visual Studio's New Project dialog: Visual C# for Java EE and Visual Basic for Java EE.
Open the RSS Reader Web Site in the same solution.
Drag-and-drop all project files (except for the references) from the Web Site to the RSSReader project, overwriting any default items.
Right-click the RSSReader project node in the Solution Explorer and select Convert to Web Application.
More details about the conversion process can be found in Mainsoft's online documentation.
The RSS Reader uses version 1.0.0.1 of the ASP.NET RSS Toolkit. Since the download package from CodePlex does not include any Visual Studio project files, we create a new .NET class library project ("RSSToolkit") from the source code and add it to our solution. To build RSS Toolkit, we also need to add references to System.Configuration
, System.Design
, System.Drawing
, System.Web
and System.Windows.Forms
to the class library project. We verify that the build succeeds and then generate a Grasshopper project from RSS Toolkit.
Grasshopper adds a new context menu item to Solution Explorer's project nodes: Generate Java EE Project. Choosing this option for the RSSToolkit project brings up a wizard that analyzes project dependencies and suggests alternatives for unresolved references. In this case, because the RSS Toolkit doesn't have any non-framework dependencies, the wizard does not present any options. When finished, a new project is created in our solution: RSSToolkit.JavaEE.
| ![Adding a Java Reference](/KB/showcase/DeployReaderOnLinux/image01.gif) |
| Figure 1: Adding a Java Reference
|
Now we add a reference from RSSReader to the Java-version of RSSToolkit by selecting Add Java Reference... (another new menu item added by Grasshopper, see Figure 1) and select RSSToolkit.JavaEE from the Project tab.
We remove the .NET versions of the Web Site and the RSS Toolkit project from our solution because they are no longer required.
Building for Java
If we try to build the newly generated Java projects, we see errors referring to classes and methods that are "not supported." Due to the fundamental differences between the .NET and Java platforms, there are some limitations in the Java runtime libraries used to implement the .NET Framework. The product documentation Runtime Framework overview section provides more details on this subject. These limitations are responsible for the build errors above and they are caught when Grasshopper validates the .NET code.
To build the application successfully, we need to modify the source code to use only supported methods and classes. When targeting the Java platform, the full Java Framework is callable from Visual C# or Visual Basic code, so we always have the choice of either using supported .NET classes and methods, or using the Java Framework. If you are looking to maintain a single source code base for targeting both platforms, use the conditional compilation directives (#if JAVA
) to separate sections that are unique to .NET or Java.
In our sample application, the errors we see are due to the usage of BuildProvider, which is unsupported due to its tight coupling to System.CodeDom
. We inspect the RSS Toolkit classes that are generating errors and see that they are only needed for generating RSS feeds, and not for consuming them – which is the only requirement in the RSS Reader. The pragmatic solution here is to ignore the three classes that are responsible for generating RSS when building for Java. There are two ways to accomplish this, and the choice between them is more a matter of style than anything else. We can either exclude the files from the RSSToolkit.JavaEE project, or we can wrap them with a pre-processor directive:
#if !JAVA
#endif
We also need to wrap the code accessing these classes with the #if !JAVA
directive.
Another issue to keep in mind when moving from the .NET platform to Java is that we need to use the correct case for file names. This is because .NET and Windows don't distinguish between lowercase or uppercase, while Java does.
Using ASP.NET AJAX in Grasshopper
The RSS Reader also depends on the AJAX Control Toolkit, a joint project between Microsoft and the developer community. Grasshopper provides full support for the AJAX Control Toolkit (as well as the Anthem.NET AJAX Toolkit) and comes bundled with the ported source code. In order to reference the AJAX Control Toolkit, open the project (by default available at C:\Program Files\Mainsoft for Java EE\Samples\CS\Tomcat\AjaxControlToolkit\), build it with Grasshopper and add the output AjaxControlToolkit.jar file to your project's references with the Add Java Reference... option. Make sure that the assembly file (AjaxControlToolkit.dll) resides in the same folder as the *.jar file.
The original RSS Reader project used a preview release of ASP.NET AJAX and, as such, contains the tagMapping
element in Web.config for enabling the AJAX validators. These types were removed from the official release of ASP.NET AJAX. To correct the problem, simply remove the tagMapping
element from Web.config.
This time, the build succeeds. Start the Grasshopper bundled Tomcat server from Start > All Programs > Mainsoft for Java EE > Start Tomcat and run the application from Visual Studio with Ctrl-F5. Voilà! Tomcat runs the Java application, which was ported from ASP.NET.
Of course, this conversion is a one-time process. Now we can continue to develop the source code in Visual C# or Visual Basic inside Visual Studio, like we would any .NET application, taking advantage of Visual Studio's visual designer, debugger, IntelliSense and other development features.
Adding new AJAX functionality to the RSS Reader for Java is virtually the same process as adding AJAX controls to an ASP.NET application deployed on Windows. As an example, we'll use the AnimationExtender to blur the feed list when the mouse cursor isn't hovering over it.
For the AJAX AnimationExtender control to be able to access the feed list, we wrap it with an ASP.NET control; <asp:panel>
serves this purpose well. Drag-and-drop the AnimationExtender control to the Default.aspx page's design surface in Visual Studio and set its TargetControlID
property to the ID of the <asp:Panel>
. Now we'll define the animations for fading the list in and out on specific events:
<ajaxT:AnimationExtender ID="AnimationExtender1"
runat="server" TargetControlID="FeedList">
<Animations>
<OnLoad>
<FadeOut MinimumOpacity=".2" />
</OnLoad>
<OnHoverOver>
<FadeIn MinimumOpacity=".2" Duration=".3" />
</OnHoverOver>
<OnHoverOut>
<FadeOut MinimumOpacity=".2" />
</OnHoverOut>
</Animations>
</ajaxT:AnimationExtender>
While we're at it, let's also replace the "(read more)" link to the full article text with an Ajaxy HoverMenu. Drag-and-drop the HoverMenuExtender
control, also found in the AJAX Control Toolkit, to the DataList's Item Template. Then drop a HyperLink control there and databind the NavigateUrl
property to the item's link value:
<asp:HyperLink ID="Popup" runat="server" BackColor=Beige BorderColor=DarkKhaki
Text="View Original Item" NavigateUrl='<%# Eval("link") %>'/>
<ajaxT:HoverMenuExtender TargetControlID="panelHeader" runat="server"
PopupControlID="Popup" PopupPosition=Right OffsetX=15 />
Finally, we build and run the application to see that our changes work.
Deploying on Linux
So far, we have deployed the Java build target on the Grasshopper-bundled Tomcat Web application server, running locally on Windows. Now let's fulfill our promise to run this Web application on Linux. This is a straightforward process.
First, we'll build the Release_Java
configuration of RSSReader to create a deployment-friendly WAR file. The release configuration's default setting enables Full Deployment Package -- a Grasshopper-specific project property -- and tells the build system to generate a Web Archive (WAR) file, a compressed archive with all needed resources (HTML, JAR, image files, etc.).
Then we'll upload the WAR file to Tomcat on Linux via Tomcat Manager's Web administration interface. The WAR file created in the previous step is located inside the local Tomcat's installation directory, by default at C:\Program Files\Mainsoft for Java EE\jakarta-tomcat\webapps\.
| ![The RSS Reader Running on Linux](/KB/showcase/DeployReaderOnLinux/image02.gif) |
| Figure 2: The RSS Reader Running on Linux
|
Here is a screenshot of the ASP.NET AJAX application hosted on Linux/Apache Tomcat:
Summary
This article shows you how to use ASP.NET AJAX to build applications for Linux, which is a very practical tool for multi-platform deployments. However, we're only scraping the surface of Grasshopper's full potential to enable.NET-Java interoperability here. Unleash your creativity and use the power of Visual Studio development and the ASP.NET Framework to deploy Web applications to open source Web application servers.
Learn more at dev.mainsoft.com.
Jonas Martinsson is an entrepreneur and member of Mainsoft's development team. Jonas helped to integrate Mainsoft's .NET-Java EE interoperability products with the Visual Studio IDE.