This article is mainly from the MSDN magazine Building Cross-Platform Web Services with ServiceStack. Windows Communication Foundation (WCF) is a very good service framework. When we discuss Cross-Platform Services, although WCF supports WebService, Microsoft has re-developed ASP in the face of poor performance in some advanced applications. NET WebAPI framework. For more information about the two frameworks, see my other article, "WCF and ASP. NET Web API selection on the application. ASP. NET Web API is an important option. In this article, I will show you how to use ServiceStack (open source code.. NET and Mono REST service framework) to complete this task, without leaving Visual Studio or Microsoft. NET/Mono, in addition to ServiceStack, there is also a Nancy framework. For details, see 《. NET micro Web framework Nancy.
A typical Web service structure is as follows:
<Add path = "*" type = "ServiceStack. WebHost. Endpoints. ServiceStackHttpHandlerFactory, ServiceStack" verb = "*"/>
</HttpHandlers>
</System. web>
<System. webServer>
<Modules runAllManagedModulesForAllRequests = "true"/>
<Validation validateIntegratedModeConfiguration = "false"/>
<Add path = "*" name = "ServiceStack. factory "type =" ServiceStack. webHost. endpoints. serviceStackHttpHandlerFactory, ServiceStack "verb =" * "preCondition =" integratedMode "resourceType =" Unspecified "allowPathInfo =" true "/>
</Handlers>
</System. webServer>
</Configuration>
You need to inherit from ServiceStack. WebHost. End to implement the endpoint.
Public class AppHost: AppHostBase
{
Public AppHost () // Tell ServiceStack the name and where to find your web services
: Base ("StarterTemplate ASP. NET Host", typeof (HelloService). Assembly ){}
Public override void Configure (Funq. Container container)
{
// Set JSON web services to return idiomatic JSON camelCase properties
ServiceStack. Text. JsConfig. EmitCamelCaseNames = true;
// Configure User Defined REST Paths
Routes
. Add <Hello> ("/hello ")
. Add <Hello> ("/hello/{Name *}");
// Uncomment to change the default ServiceStack configuration
// SetConfig (new EndpointHostConfig {
//});
// Enable Authentication
// ConfigureAuth (container );
// Register all your dependencies
Container. Register (new TodoRepository ());
}
/* Uncomment to enable ServiceStack Authentication and CustomUserSession
Private void ConfigureAuth (Funq. Container container)
{
Var rule ettings = new rule ettings ();
// Default route:/auth/{provider}
Plugins. Add (new AuthFeature () => new CustomUserSession (),
New IAuthProvider [] {
New CredentialsAuthProvider (deleetprovider ),
New FacebookAuthProvider (deleetask ),
New TwitterAuthProvider (appSettings ),
New BasicAuthProvider (appSettings ),
}));
// Default route:/register
Plugins. Add (new RegistrationFeature ());
// Requires ConnectionString configured in Web. Config
Var connectionString = ConfigurationManager. ConnectionStrings ["AppDb"]. ConnectionString;
Container. Register <IDbConnectionFactory> (c =>
New OrmLiteConnectionFactory (connectionString, SqlServerDialect. Provider ));
Container. Register <IUserAuthRepository> (c =>
New OrmLiteAuthRepository (c. Resolve <IDbConnectionFactory> ()));
Var authRepo = (OrmLiteAuthRepository) container. Resolve <IUserAuthRepository> ();
AuthRepo. CreateMissingTables ();
}
*/
Public static void Start ()
{
New AppHost (). Init ();
}
}
When the ServiceStack Web application is started, your service contract is listed as metadata operation ,:
Related Articles:
Interview With Demis Bellot, Project Lead of ServiceStack-Part 1
Interview With Demis Bellot, Project Lead of ServiceStack-Part 2
SignalR, Filters and ServiceStack