-
Notifications
You must be signed in to change notification settings - Fork 38
Getting started
Zyan application typically consists of a client and a server. The server provides services (in the form of components), and the client is able to utilize them (call component methods) remotely. Any .NET Framework class can be used as a component. Components are managed by the server, and their assemblies are only available on the server-side.
The client accesses remote component's methods through the proxy. Proxy is an object which actually handles remote calls at the client side. Proxy object implements the same interface (i.e., has the same public methods, properties and events) as the remote component hosted by server. Every hosted component needs to implement public interface known on both sides. So typical Zyan application has three separate assemblies, or three Visual Studio Projects:
Assembly | Description |
---|---|
Client Assembly | Contains client-side code (i.e. user interface, etc.) |
Shared Assembly | Contains public interfaces of the remote components |
Server Assembly | Contains hosting and components implementation code |
Important!
Both Client and Server assemblies should referenceZyan.Communications.dll
assembly.
A component is an ordinary .NET class, which implements some interface. Component implementation (non-abstract class) should be placed in the server assembly, while its interface should be located in the shared assembly, known both to the client and the server. Here is a sample component code:
namespace EchoExample.Server
{
public class EchoComponent : IEchoComponent
{
public string Echo(string message)
{
return message;
}
}
}
And here is its public interface:
namespace EchoExample.Shared
{
public interface IEchoComponent
{
string Echo(string message);
}
}
That's all. EchoComponent is ready to be published in the component catalog.
Tip: If you need to publish some existing class as a component with an interface, you can derive new class from it and implement the interface in the derived class.
Components are published by ZyanComponentHost
, a dedicated service which handles component creation, hosting and network communication (opening/closing network sockets, etc). ZyanComponentHost can handle any number of components. To create ZyanComponentHost you need to provide unique name, port and optional protocol setup object. Zyan has a number of ProtocolSetup classes, which you can find in Zyan.Communication.Protocols
namespace.
Network protocols have a few different protocol setups available. Protocol-related classes are placed in the separate namespaces (Zyan.Communication.Protocols.Tcp
for TCP, Zyan.Communication.Protocols.Http
for HTTP, etc.)
ProtocolSetup classes allow to tweak communication parameters, i.e.:
- Port number (default 80 for HTTP)
- Authentication provider
- Security settings, encryption algorithm, etc.
See Network protocols setup article for more information on this subject.
The following listing demonstrates hosting +EchoComponent+ listed above via HTTP channel (Port 8080) with encryption, but with no authentication:
using Zyan.Communication;
using Zyan.Communication.Security;
using Zyan.Communication.Protocols.Http;
...
var protocolSetup = new HttpCustomServerProtocolSetup(8080, new NullAuthenticationProvider(), true);
var host = new ZyanComponentHost("EchoExample", protocolSetup);
host.RegisterComponent<IEchoComponent, EchoComponent>(ActivationType.SingleCall);
That's all for the server-side. Application server is ready to process client requests.
Tip: There is no URL you can open in your browser. Zyan doesn't generate neither WSDL nor any other kind of metadata. Client uses shared interface assembly, so there is no need in metadata to communicate with the server. You cannot invoke Zyan-hosted component's methods via browser-based UI, you need to write client application to test it.
To use remote components, client should establish connection with the remote component host. ZyanConnection is used to connect client to the remote host. This is somewhat similar to ADO.NET model (for example, SqlConnection class is used to connect to remote database server). ZyanConnection constructor takes two arguments: remote host URL and optional protocol setup class.
The following code establishes secure connection to the +EchoComponent+ published above over HTTP protocol and 8080 port, creates proxy object and invokes +Echo+ method:
using Zyan.Communication;
using Zyan.Communication.Protocols.Http;
...
var protocolSetup = new HttpCustomClientProtocolSetup(true);
var connection = new ZyanConnection("http://localhost:8080/EchoExample", protocolSetup);
var proxy = connection.CreateProxy<IEchoComponent>();
var result = proxy.Echo("Hello, World!");
Proxy object behaves just like local EchoComponent. The only difference is that Echo method is executed on the server-side.
If you replace "localhost" with the name or IP-Adress of some other computer and run server application on that computer, you will have your first distributed application up and running.
Tip: Never hard-code port numbers and server URLs in real-world applications. Use configuration files or Windows registry instead to make your applications configurable.