当前位置:网站首页>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 .
边栏推荐
- [untitled] a query SQL execution process in the database
- Sword finger offer 30 Stack containing min function
- Redis cluster deployment based on redis5
- "Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.5 automatic differentiation_ Learning thinking and exercise answers
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
- MySQL winter vacation self-study 2022 11 (9)
- 2020.02.11
- GifCam v7.0 极简GIF动画录制工具中文单文件版
- 550 permission denied occurs when FTP uploads files, which is not a user permission problem
- 4. File modification
猜你喜欢
Introduction to robotframework (III) Baidu search of webui automation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
Introduction to robotframework (I) brief introduction and use
4. File modification
【Unity3D】GUI控件
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
RobotFramework入门(三)WebUI自动化之百度搜索
如何精准识别主数据?
Referenceerror: primordials is not defined error resolution
2345 file shredding, powerful file deletion tool, unbound pure extract version
随机推荐
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
Atcoder beginer contest 233 (a~d) solution
RobotFramework入门(二)appUI自动化之app启动
How to read excel, PDF and JSON files in R language?
Function knowledge points
Introduction to robotframework (II) app startup of appui automation
Pat 1084 broken keyboard (20 points) string find
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
[kubernetes series] learn the exposed application of kubernetes service security
MySQL winter vacation self-study 2022 11 (9)
High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis
Introduction to robotframework (III) Baidu search of webui automation
Pure QT version of Chinese chess: realize two-man, man-machine and network games
张丽俊:穿透不确定性要靠四个“不变”
Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
Pat 1046 shortest distance (20 points) simulation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
DDoS attacks - are we really at war?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
Maturity of master data management (MDM)