Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ServiceStack

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use ServiceStack, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

ServiceStack is the most comfortable .net service development class library currently used. It is easy to develop and very fast, and it can also access multiple databases across platforms.

ServiceStack provides web-based services that can be published in iis and separate application-based running services, both of which are relatively simple.

Here is an example of a simple Rest service development that runs as a separate application:

/ / since services generally require high performance, separate programs should be based on console applications. The following is the simplest apphost initialization, and the most important thing is to specify the url address of the listener.

Static void Main (string [] args)

{

ServiceStackHost appHost

AppHost = new AppHost ()

.Init ()

.start ("http://localhost:7771/");"

Console.WriteLine ("specified service has been started, listening port is xxxx, please do not close the window

Mouth! ")

Console.ReadLine ()

}

/ / the implementation of apphost, which inherits servicestack's AppHostHttpListenerBase

Public class AppHost: AppHostHttpListenerBase

{

/ / configure the compilation collection that exists in the rest interface

Public AppHost (): base ("ServiceBaseST", typeof (AppHost) .Assembly) {}

/ / the configuration in apphost, mainly to configure the database connection, etc. After the configuration is completed here, all the rest services can be used.

Public override void Configure (Container container)

{

/ / initialize database connections, and ormlite can also support multiple data source connections

Container.Register (c = > new OrmLiteConnectionFactory (connection string, PostgreSqlDialect.Provider))

}

}

/ / the following is a simple helloworldservice that inherits the Service interface and configures the

/ / routing. The method in the service is Get,Post or Any, which is implemented according to the class name in the route.

Public class HelloWorldService: Service

{

Public Object Get (Hello hello)

{

String _ restStr = string.Format ("hello {0}!", hello.name)

Double [,] a2 = new double [,] {{1,2,3}, {4,5,6}}

Return new HelloWorldModel () {name = _ restStr, userid = "test", testDoule = a2}

}

}

/ / routing configuration of the service

[Route ("/ hello/ {name}", "GET")]

Public class Hello: IReturn

{

Public string name {get; set;}

}

/ / the service of ServiceStack is based on model, so model is generally defined to return.

/ / when the client uses url, add format=json or format=xml after it, and the corresponding grid will be returned.

/ / type of data

Public class HelloWorldModel

{

Public string userid {get; set;}

Public string name {get; set;}

Public double [,] testDoule {get; set;}

}

/ / url: http://localhost:7771//hello/name parameters accessed by the client? format=json or format=xml

Thank you for reading this article carefully. I hope the article "how to use ServiceStack" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 252

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report