当前位置:网站首页>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 .
边栏推荐
- #PAT#day10
- Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
- 一个复制也能玩出花来
- Sword finger offer 29 Print matrix clockwise
- How to check the lock information in gbase 8C database?
- Building the prototype of library functions -- refer to the manual of wildfire
- Referenceerror: primordials is not defined error resolution
- DDoS attacks - are we really at war?
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
- Master data management theory and Practice
猜你喜欢
CobaltStrike-4.4-K8修改版安装使用教程
微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
Introduction to robotframework (I) brief introduction and use
Pure QT version of Chinese chess: realize two-man, man-machine and network games
Fault analysis | analysis of an example of MySQL running out of host memory
Blue Bridge Cup group B provincial preliminaries first question 2013 (Gauss Diary)
Redis delete policy
Taobao focus map layout practice
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
随机推荐
微服务注册与发现
如何精准识别主数据?
2020.02.11
Pure QT version of Chinese chess: realize two-man, man-machine and network games
Template_ Quick sort_ Double pointer
Force buckle 146 LRU cache
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
Large scale DDoS attacks take Myanmar offline
RobotFramework入门(一)简要介绍及使用
张丽俊:穿透不确定性要靠四个“不变”
【Unity3D】GUI控件
After changing the GCC version, make[1] appears in the compilation: cc: command not found
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
A doctor's 22 years in Huawei
C language - Blue Bridge Cup - promised score
2022.02.13
Universal crud interface
2345 file shredding, powerful file deletion tool, unbound pure extract version