Skip to content

Client Providers (Client Side Example)

Ali Yousefi edited this page Jun 4, 2019 · 10 revisions

After create your server side project you must create your client side project for connect to your server.

In signalgo new version all services will generate automatically for client side with visual studio extension,so you can use you services very easy.

1. Example for ServerServices (duplex real-time services):

                //your client connector that will be connect to your server
                ClientProvider provider = new ClientProvider();
                //connect to your server must have full address that your server is listen
                provider.Connect("http://localhost:1132/SignalGoTestService");
                //register your service interfacce for client
                //AuthenticationService is you service class name in server side that generated in client side with the namespace you add in signal visual studio extension

                ServicesNameSpace.Interfaces.IAuthenticationService authenticationService = provider.RegisterServerService<ServicesNameSpace.ServerServices.AuthenticationService>();
                //call server method and return value from your server to client
                var result = authenticationService.HelloWorld("ali");
                //for async call
                var result2 = await authenticationService.HelloWorldAsync("ali");
                //print your result to console
                Console.WriteLine(result.Item1);

2. Example for HttpServices:

                ServicesNameSpace.Interfaces.IAuthenticationService authenticationService = new ServicesNameSpace.HttpServices.AuthenticationService("http://localhost:1132",new SignalGo.Client.HttpClient() {
 Encoding= System.Text.Encoding.UTF8 });
                //call server method and return value from your server to client
                var result = authenticationService.HelloWorld("ali");
                //for async call
                var result2 = await authenticationService.HelloWorldAsync("ali");
                //print your result to console
                Console.WriteLine(result.Item1);

Auto Reconnection:

If you are using signalgo real-time and your connection to be alive always you can use ConnectAsyncAutoReconnect Example:

 ClientProvider clientProvider = new ClientProvider();
            clientProvider.ConnectAsyncAutoReconnect($"http://localhost:6541/Signalgo", (changed) =>
            {
                 //if changed is false its mean you disconnected from server and if its true you are connected to server
            });

when client disconnected from server ConnectAsyncAutoReconnect method always try to connect to server again so you don't need manage your connection. if you used ConnectAsyncAutoReconnect method you don't need use Connect or ConnectAsync methods. ConnectAsyncAutoReconnect is enough

Note:

if you using HttpService over your service you don't need use ClientProvider.

[ServiceContract("Authentication", ServiceType.HttpService, InstanceType.SingleInstance)]

But if you using ServerService over your Service you need to Connect with ClientProvider. in Signalgo you can use both of protocols for your service. example:

//for http calls
[ServiceContract("Authentication", ServiceType.HttpService, InstanceType.SingleInstance)]
//for duplex calls
[ServiceContract("Authentication", ServiceType.ServerService, InstanceType.SingleInstance)]
 public class AuthenticationService
 {
 }

if you used HttpServices over your server services,you can call them like:

var service = new MyTestServices.HttpServices.HelloWorldService("http://localhost:9674");
var result = service.Login("test","test");

HttpServices will generate with code generator in "MyServiceNameSpaces.HttpServices" but duplex services will generate with code generator in "MyServiceNameSpaces.ServerServices". so you dont need register http services,you can instance them directly and call methods but for server services (duplex) you need register them in ClientProvider then Connect then call methods.