当前位置:网站首页>C # create self host webservice
C # create self host webservice
2022-07-06 02:46:00 【flysh05】
C# Create word managed Web service .
1. Create a project , Originally used ConsoleApp From hosting one WebService.

Enter the project name ConsoleApp_SelfHost, Solution name :SelfHostWebServiceTest
2. Create data model classes , DataModel.cs
public class DataModel
{
public int Id { get; set; }
public string Name { get; set; }
public string DeviceType { get; set; }
}
3. install Microsoft.AspNet.WebApi.SelfHost Third party component

4. Create a data model control class DataModelController.cs
Add reference :
using System.Net;
using System.Web.Http;
public class DataModelController:ApiController
{
DataModel[] datas = new DataModel[]
{
new DataModel { Id = 1, Name = “DELL”, DeviceType = “PC” },
new DataModel{ Id = 2, Name = “HuaWei”, DeviceType = “Phone” },
new DataModel{ Id = 3, Name = “SanDisk”, DeviceType = “Assceory” }
};
public IEnumerable GetAllItems()
{
return datas;
}
public DataModel GetItemById(int id)
{
var item = datas.FirstOrDefault((i) => i.Id == id);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
public IEnumerable GetItemsByType(string deviceType)
{
return datas.Where(i => string.Equals(i.DeviceType, deviceType,
StringComparison.OrdinalIgnoreCase));
}
}
5. stay Program Created in the main program HttpSelfHostServer
Add reference to background code
using System.Web.Http;
using System.Web.Http.SelfHost;
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration(“http://localhost:9001”);
config.Routes.MapHttpRoute(
“API Default”, “api/{DataModelController}/{id}”,
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine(“Press Enter to quit.”);
Console.ReadLine();
}
}
}
Run to start the service .
边栏推荐
- 球面透镜与柱面透镜
- 2020.02.11
- Qt发布exe软件及修改exe应用程序图标
- 微服务间通信
- How to accurately identify master data?
- Redis delete policy
- Rust language -- iterators and closures
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
- CSP date calculation
- How does yyds dry inventory deal with repeated messages in the consumption process?
猜你喜欢

Introduction to robotframework (III) Baidu search of webui automation

主数据管理理论与实践

Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13](/img/29/49da279efed22706545929157788f0.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13

RobotFramework入门(一)简要介绍及使用

#PAT#day10

力扣今日题-729. 我的日程安排表 I
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14](/img/c5/dde92f887e8e73d7db869fcddc107f.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14

如何精准识别主数据?

Referenceerror: primordials is not defined error resolution
随机推荐
C语言sizeof和strlen的区别
Thinking on Architecture Design (under continuous updating)
Yyds dry inventory comparison of several database storage engines
How to read excel, PDF and JSON files in R language?
Shell脚本更新存储过程到数据库
PMP practice once a day | don't get lost in the exam -7.5
07 单件(Singleton)模式
QT release exe software and modify exe application icon
Shell script updates stored procedure to database
不赚钱的科大讯飞,投资价值该怎么看?
Function knowledge points
What should we pay attention to when using the built-in tool to check the health status in gbase 8C database?
微服务间通信
Network Security Learning - Web vulnerabilities (Part 1)
Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
A doctor's 22 years in Huawei
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
2.13 simulation summary
Microservice registration and discovery
After changing the GCC version, make[1] appears in the compilation: cc: command not found