当前位置:网站首页>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 .
边栏推荐
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
- Technology sharing | what if Undo is too big
- 有沒有sqlcdc監控多張錶 再關聯後 sink到另外一張錶的案例啊?全部在 mysql中操作
- Introduction to robotframework (I) brief introduction and use
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
- 技术分享 | undo 太大了怎么办
- Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
- 2345文件粉碎,文件强力删除工具无捆绑纯净提取版
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
Referenceerror: primordials is not defined error resolution
有没有完全自主的国产化数据库技术
MySQL advanced notes
【Unity3D】GUI控件
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
随机推荐
inherited constructors
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
DDoS "fire drill" service urges companies to be prepared
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
Elimination games
[network security interview question] - how to penetrate the test file directory through
Day 50 - install vsftpd on ceontos6.8
A copy can also produce flowers
Thinking on Architecture Design (under continuous updating)
Data preparation
[postgraduate entrance examination English] prepare for 2023, learn list5 words
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
Y a - t - il des cas où sqlcdc surveille plusieurs tables et les associe à une autre? Tout fonctionne dans MySQL
Redis skip table
Qt发布exe软件及修改exe应用程序图标
MySQL winter vacation self-study 2022 11 (7)
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
Advanced technology management - what is the physical, mental and mental strength of managers
Introduction to robotframework (I) brief introduction and use
GifCam v7.0 极简GIF动画录制工具中文单文件版