Building a Basic .NET Remoting Application 之三 Building a Client Application
2010-01-04 15:33
603 查看
Building a Client Application
To build a client of the remote type defined in Building a Remotable Type and hosted by the application created in Building a Host Application, your application must register itself as a client for that remote object, and then invoke it as though it were within the client's application domain. The .NET remoting system will intercept your client calls, forward them to the remote object, and return the results to your client. The following code example shows how to build a simple remoting client.
[C#]
// Client.cs
using System;
using System.Runtime.Remoting;
public class Client{
public static void Main(){
RemotingConfiguration.Configure("Client.exe.config");
RemotableTyperemoteObject = new RemotableType();
Console.WriteLine(remoteObject.StringMethod());
}
}
To compile this class into a client or calling executable using the command-line tools that ship with the .NET Framework SDK, save it as
[C#]
csc /noconfig /r:RemotableType.dll Client.cs
In this command, the file name is as follows:
[C#]
Client.cs
As you can see in the example, the
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="RemotableType, RemotableType"
url="http://localhost:8989/RemotableType.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
This file tells the remoting system that the type information for the
To build a client of the remote type defined in Building a Remotable Type and hosted by the application created in Building a Host Application, your application must register itself as a client for that remote object, and then invoke it as though it were within the client's application domain. The .NET remoting system will intercept your client calls, forward them to the remote object, and return the results to your client. The following code example shows how to build a simple remoting client.
[C#]
// Client.cs
using System;
using System.Runtime.Remoting;
public class Client{
public static void Main(){
RemotingConfiguration.Configure("Client.exe.config");
RemotableTyperemoteObject = new RemotableType();
Console.WriteLine(remoteObject.StringMethod());
}
}
To compile this class into a client or calling executable using the command-line tools that ship with the .NET Framework SDK, save it as
Client.language-extension (or use another file name of your choice, where the language extension is the language you want to compile). Save the file in the same directory in which you saved a copy of the
RemotableType.dllthat you built in the Building a Remotable Typetopic. (Note that this should not be the same directory as that of your
Listener.exeapplication. If it is, you will not be certain that you are receiving and making use of a remote reference, because assembly and type resolution can occur when applications are in the same directory.) At the command prompt in that directory, type the following command:
[C#]
csc /noconfig /r:RemotableType.dll Client.cs
In this command, the file name is as follows:
[C#]
Client.cs
As you can see in the example, the
Clientclass must be able to find the
Client.exe.configfile to load the configuration for
RemotableTypeclass. This file should be saved in the same directory as the
Client.exefile, or the configuration file will not be found and an exception will be thrown. The following code example shows the
Client.exe.configconfiguration file for this listening or host application domain.
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="RemotableType, RemotableType"
url="http://localhost:8989/RemotableType.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
This file tells the remoting system that the type information for the
RemotableTyperemote object can be found in the
RemotableTypeassembly, and that this client should attempt to create and use a
RemotableTypeobject located at http://localhost:8989/RemotableType.rem. For details about the URL attribute in this configuration file, see Activation URLs. If you want to run this application over a network, you must replace "localhost" in the client configuration with the name of the remote computer.
Note Although there are only a few settings in the preceding configuration file, most of the problems using .NET remoting occur because some of these settings are either incorrect or do not match the configuration settings for client applications. It is easy to mistype a name, forget a port, or neglect an attribute. If you are having problems with your remoting application, check your configuration settings first.
相关文章推荐
- Building a Basic .NET Remoting Application 之四 Compiling and Running the Basic Application
- Building a Basic .NET Remoting Application 之一 Building a Remotable Type
- Building a Basic .NET Remoting Application 之五 Basic Remoting Task List
- Building a Basic .NET Remoting Application 之二 Building a Host Application
- Building a Basic .NET Remoting Application
- Remoting from IIS Hosted component to ASP.NET Client application
- Tailor Your Application by Building a Custom Forms Designer with .NET
- How to write an application that supports the Fast User Switching feature by using Visual Basic .NET or Visual Basic
- .Net Remoting Application Architecture
- Building an AJAX Based Web Chatting Application using ASP.NET 2.0(转载)
- .NET Remoting——practice & progress(3):Client Activated客户端激活
- .Net Remoting Application Architecture
- How to call the EnumJobs function from a Visual Basic .NET application
- Building the DotNetNuke Module in Normal Asp.net Application
- .NET Remoting Basic(7)-信道
- Building a custom data provider for .Text (a custom ASP.NET blogging application)
- Global Text Chat Room Application using C#.NET Remoting Technology(转载)
- Building the DotNetNuke Module in Normal Asp.net Application
- .NET Remoting Basic(8)-事件
- Building a Robust Web Based Email Client (WebMail) Using the IP*Works! ADO.NET Data Provider(中文)