-
Notifications
You must be signed in to change notification settings - Fork 0
C# client
Using DTOs to define your web service interface makes it possible to provide strong-typed generic service clients without any code-gen or extra build-steps, leading to a productive end-to-end type-safe communication gateway from client to server.
In addition, the service clients also provide HTTP verbs (Get, Post & PostFile, Put, Delete) letting you call your custom user-defined routes REST-fully e.g:
Using the New Api
HelloResponse response = client.Get(new Hello { Name="World" });
response.Result.Print();
Async Example
client.GetAsync(new Hello { Name="World" },
r => r.Result.Print(), (r, ex) => { throw ex; });
var response = client.Get<HelloResponse>("/hello/World!");
response.Result.Print();
Async Example
client.GetAsync<HelloResponse>("/hello/World!",
r => r.Result.Print(), (r, ex) => { throw ex; });
C#/.NET Clients can call the above Hello Service using any of the JSON, JSV, XML or SOAP Service Clients with the code below:
Using the New Api
var response = client.Send(new Hello { Name = "World!" });
response.Result.Print();
Async Example
client.SendAsync(new Hello { Name = "World!" },
r => r.Result.Print(), (r, ex) => { throw ex; });
var response = client.Send<HelloResponse>(new Hello { Name = "World!" });
response.Result.Print();
Async Example
client.SendAsync<HelloResponse>(new Hello { Name = "World!" },
r => r.Result.Print(), (r, ex) => { throw ex; });
The service clients use the automatic pre-defined routes for each service.
ServiceStack's Auth Tests shows different ways of authenticating when using the C# Service Clients. By default BasicAuth and DigestAuth is built into the clients, e.g:
var client = new JsonServiceClient(baseUri) {
UserName = UserName,
Password = Password,
};
var request = new Secured { Name = "test" };
var response = client.Send<SecureResponse>(request);
Behind the scenes ServiceStack will attempt to send the request normally but when the request is rejected and challenged by the Server the clients will automatically retry the same request but this time with the Basic/Digest Auth headers.
To skip the extra hop when you know you're accessing a secure service, you can tell the clients to always send the BasicAuth header with:
client.AlwaysSendBasicAuthHeader = true;
The alternative way to Authenticate is to make an explicit call to the Auth
service (this requires CredentialsAuthProvider enabled) e.g:
var authResponse = client.Send<AuthResponse>(new Auth {
provider = CredentialsAuthProvider.Name,
UserName = "user",
Password = "p@55word",
RememberMe = true, //important tell client to retain permanent cookies
});
var request = new Secured { Name = "test" };
var response = client.Send<SecureResponse>(request);
After a successful call to the Auth
service the client is Authenticated and if RememberMe is set, the client will retain the Session Cookies added by the Server on subsequent requests which is what enables future requests from that client to be authenticated.
All REST and ServiceClients share the same interfaces (IServiceClient
, IRestClient
and IRestClientAsync
) so they can easily be replaced (for increased perf/debuggability/etc) with a single line of code.
Here's a list of all built-in clients:
-
IServiceClient
- Soap11ServiceClient (uses SOAP 11 endpoint)
- Soap12ServiceClient (uses SOAP 12 endpoint)
- JsonServiceClient (uses default endpoint with JSON)
- JsvServiceClient (uses default endpoint with JSV)
- XmlServiceClient (uses default endpoint with XML)
- MsgPackServiceClient (uses default endpoint with Message-Pack)
- ProtoBufServiceClient (uses default endpoint with Protocol Buffers)
-
IRestClient
- JsonServiceClient (uses custom REST endpoint with JSON) - recommended
- JsvServiceClient (uses custom REST endpoint with JSV)
- XmlServiceClient (uses custom REST endpoint with XML)
- MsgPackServiceClient (uses default endpoint with Message-Pack)
- ProtoBufServiceClient (uses default endpoint with Protocol Buffers)
-
IRestClientAsync
- JsonRestClientAsync (uses custom REST endpoint with JSON) - recommended
- JsvRestClientAsync (uses custom REST endpoint with JSV)
- XmlRestClientAsync (uses custom REST endpoint with XML)
- Why ServiceStack?
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
- Getting Started
- Reference
- Clients
- Formats
- View Engines 4. Razor & Markdown Razor
- Hosts
- Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in caching options
- Built-in profiling
- Messaging and Redis
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- Plugins
- Tests
- Other Languages
- Use Cases
- Performance
- How To
- Future