Click here to Skip to main content
15,884,838 members
Articles / Programming Languages / C#

Publishing Remote Object From IIS

Rate me:
Please Sign up or sign in to vote.
4.13/5 (6 votes)
30 Apr 2008CPOL2 min read 29K   208   13   6
How to publish a remote object using IIS (Internet Information Server)

Introduction

In this article, we will study how to publish a remote object using IIS, creating an XML document of the remote object and enrichment of an XML document with an XSL file.

Create Remote Object

Firstly, let's start with designing a remote object. The object will have two methods which process Input Output operations.

C#
namespace Remote
{
    /// <summary>

    /// This object can reach the methods which operate processes related with folders
    /// of the remote computer.
    /// </summary>
    public class RemoteObject:MarshalByRefObject
    {
        /// <summary>
        /// This method reaches files in a specific folder.
        /// </summary>
        /// <param type=""string"" name=""path"" >

        /// The path of the folder of desired files.</param >
        /// <param type=""string"" name=""pattern"" >
        /// The type of the desired file.</param >

        /// <returns >FileInfo array</returns >
        /// 
        /// class TestClass
        /// {
        ///     public void Get()
        ///     {
        ///         ...
        ///         FileInfo[] files = myRemoteNesne.getFiles(@"C:\","*.*");
        ///         ...
        ///     }
        /// }
        /// 
        public FileInfo[] getFiles(string path, string pattern)
        {
            return new DirectoryInfo(path).GetFiles(pattern);
        }

        /// <summary>
        /// This method is used to reach all physical and exterior disks of remote
        /// computer.
        /// </summary>

        /// <returns >Disks names as a string array.</returns >
        /// 
        /// class TestClass
        /// {
        ///     public void Get()
        ///     {
        ///         ...
        ///         string[] disks = myRemoteNesne.getDrives();
        ///         ...
        ///     }
        /// }
        /// 
        public string[] getDrives()
        {
            return Environment.GetLogicalDrives();
        }
    }
}

Create Output File

At this point, the description fields above the methods are more important than the methods themselves (To obtain this fields: Press "/" (AltGr+7) three times). These fields will constitute necessary documentation for our object. When the remote object is compiled, the compiler will create a nested XML document automatically. If we wish, we can enrich the context of nodes in this XML document. For example, the <PARAM> node does not have a property named type.

1.jpg

To take output of the description we have prepared for remote object as an XML document, you should check the option "XML documentation file" which is located at the Build|Output menu of properties of your project. Blocks of code should be set as style "Formatted" like this:

Compile the Project with Ctrl+Shift+B. Now we are ready to create a Web application to publish our remote object. Create a Web Project named RemoteObject and add a reference of our object to the Project.

2.jpg

After adding a reference, Visual Studio will add DLL and XML documents to the bin folder.

Now we add a web config file to Project. With the help of web config file, we will make necessary settings for remote object. In the web config file:

XML
<system.runtime.remoting >
    <application >
      <service >
        <wellknown type="Remote.RemoteObject,remoteSide" 
                mode="Singleton" objecturi="totalRecall.rem" />

      </service >
      <channels >
        <channel ref="http" />
      </channels >
    </application >

  </system.runtime.remoting >

Create XSLT File

With these settings, remote object will be ready to use after publication of Web Project. But before this, we have to prepare the necessary documentation for people who will use remote object. To do it, add an XSLT file to our application. The aim of this file is to produce a dynamic HTML output for XML document which is defined as a data source. Now let's write the necessary code to file with the XSL extension:

XML
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <div style="padding:2px;color:#003399;font-family:verdana;width:100%;height:80px;
border:outset gray 1px;background-color:#D4DFFF;">

      <table border="0" width="100%" height="100%" cellspacing="0" cellpadding="0">
        <tr>

          <td style="font-size:12px;">RemoteObject Referans</td>
        </tr>
        <tr>
          <td style="font-size:20px;">
            <xsl:value-of select="doc/members/member/@name"/>

          </td>
        </tr>
        <tr>
          <td style="font-size:10px;">Language : C#</td>
        </tr>

      </table>
    </div>
    <div style="margin:5px 5px 5px 5px;padding:5px 5px 5px 5px;font-family:tahoma;
font-size:14px;">
      <xsl:value-of select="doc/members/member/summary"/>
    </div>

    <xsl:call-template name="methods" />
  </xsl:template>
  <xsl:template name="methods">
    <xsl:for-each select ="doc/members/member">

      <xsl:if test="position() > 1">
        <div style="font-family:tahoma;font-size:12px;font-weight:bold;color:green;
border-bottom:solid 1px green; height:20px;">
          <xsl:value-of select="@name"/>
        </div>

        <div style="margin:5px 5px 5px 5px;padding:5px 5px 5px 5px;
        font-family:tahoma; font-size:12px;">
          <xsl:value-of select="summary"/>
        </div>
        <div style="font-family:tahoma;font-size:12px;font-weight:bold;">
          Code
        </div>

        <div style="margin:15px 15px 15px 15px;padding:5px 5px 5px 5px;
font-family:tahoma;font-size:11px;background-color:#F7F7FF;color:#000066;
border:1px outset gray;width:90%">
          <pre>
            <xsl:value-of select="code"/>
          </pre>
        </div>

        <div style="font-family:tahoma;font-size:12px;font-weight:bold;">
          Parameters
        </div>
        <table border="1" width="90%" cellspacing="0" cellpadding="1" 

style="margin:15px 15px 15px 15px;padding:5px 5px 5px 5px;font-family:tahoma;
font-size:11px;background-color:#F7F7FF;color:#000066;">
          <xsl:for-each select="param">
            <tr>
              <td style="padding:2px;">
                <strong>

                  <xsl:value-of select="@name"/>
                </strong> (
                <i>
                  <xsl:value-of select="@type"/>
                </i> )
              </td>

              <td style="padding:2px;">
                <xsl:value-of select="."/>
              </td>
            </tr>
          </xsl:for-each>

        </table>
        <div style="font-family:tahoma;font-size:12px;font-weight:bold;">
          Return Type
        </div>
        <div style="margin:5px 5px 5px 5px;padding:5px 5px 5px 5px;
font-family:tahoma;font-size:12px;">
          <xsl:value-of select="returns"/>

        </div>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Show Output File

After coding XSLT file, open the Web form Default.aspx. Add an XML control. Match the properties of the control as shown in the image:

3.jpg

The XML control displays the XML data which is specified in DocumentSource on the Web form with the help of XSL file specified in TransformSource.

4.jpg

Create Client Application

Next, we should create an application to consume remote object. Design a very simple Windows Form as shown in the image below:

5.jpg

After that, add remote object to Project as a reference. To use remote object, create an app.config file. The necessary code is shown below:

XML
<system.runtime.remoting >
    <application >
      <channels >

        <channel ref="http" port="0" usedefaultcrediantial="true" >
          <clientprovider >
            <formatter ref="binary" />

          </clientprovider >
        </channel >
      </channels >
      <client >
        <wellknown type="Remote.RemoteObject,remoteSide" 
                url="http://localhost:80/webSide/totalRecall.rem" />

      </client >
    </application >
  </system.runtime.remoting>

Finally our code:

C#
private void button1_Click(object sender, EventArgs e)
{
    RemotingConfiguration.Configure("WinSide.exe.config", false);
    RemoteObject rem = new RemoteObject();
    lstFiles.DataSource = rem.getFiles(txtPath.Text, txtPattern.Text);
}

Conclusion

In my opinion, this application is a good sample about the usage of XSLT.

History

  • 30th April, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer speak
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Izzet Kerem Kusmezer16-Apr-10 3:05
Izzet Kerem Kusmezer16-Apr-10 3:05 
GeneralPerfect article ;) Pin
serdarsert11-Aug-09 7:22
serdarsert11-Aug-09 7:22 
GeneralTest Pin
Member 111566925-Mar-09 6:23
Member 111566925-Mar-09 6:23 
GeneralRe: Test [modified] Pin
ayrilmaz11-Aug-09 9:22
ayrilmaz11-Aug-09 9:22 
Selam,

gerekli düzeltmeler yapılmıştır.

No Pain No Gain

modified on Monday, August 17, 2009 3:18 AM

GeneralThis is quite good Pin
Dewey30-Apr-08 23:26
Dewey30-Apr-08 23:26 
GeneralGreat Article Pin
Ekin ÖZÇİÇEKÇİLER30-Apr-08 22:34
Ekin ÖZÇİÇEKÇİLER30-Apr-08 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.