当前位置:网站首页>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 .
边栏推荐
- MySQL winter vacation self-study 2022 11 (5)
- C language - Blue Bridge Cup - promised score
- Redis cluster deployment based on redis5
- Sword finger offer 29 Print matrix clockwise
- Differences and usage scenarios between TCP and UDP
- Trends in DDoS Attacks
- tcpdump: no suitable device found
- 如何精准识别主数据?
- [network security interview question] - how to penetrate the test file directory through
- PMP每日一练 | 考试不迷路-7.5
猜你喜欢
[network security interview question] - how to penetrate the test file directory through
主数据管理(MDM)的成熟度
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
MySQL advanced notes
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
不赚钱的科大讯飞,投资价值该怎么看?
微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
How to accurately identify master data?
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
随机推荐
微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
故障分析 | MySQL 耗尽主机内存一例分析
CSP numeric sort
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
Six stone management: why should leaders ignore product quality
Qt发布exe软件及修改exe应用程序图标
Advanced technology management - what is the physical, mental and mental strength of managers
Atcoder beginer contest 233 (a~d) solution
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
Introduction to robotframework (I) brief introduction and use
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
4. File modification
2022.02.13
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
Bigder: I felt good about the 34/100 interview, but I didn't receive the admission
会员积分营销系统操作的时候怎样提升消费者的积极性?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
Pat grade a 1033 to fill or not to fill
Briefly describe the implementation principle of redis cluster
力扣今日題-729. 我的日程安排錶 I