webapi 寄生控制台程序

这个博客展示了如何在C#中使用WebAPI构建一个控制台程序。`Program`类初始化了一个自承载的HTTP服务器,配置了CORS并定义了路由。`YRTReaderController`实现了API控制器,提供读卡器数据的接口。配置文件中包含了读卡器的相关设置,如串口和API地址。
摘要由CSDN通过智能技术生成

三级目录

结构

在这里插入图片描述

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.SelfHost;

namespace WebApiInConsole
{
    class Program
    {

        public static HttpSelfHostServer _server;
        public static HttpSelfHostConfiguration _config;
        public static readonly string ServiceAddress = Common.AppConfigHelper.GetAppSettings("ApiAddress");

        static void Main(string[] args)
        {
            _config = new HttpSelfHostConfiguration(ServiceAddress);
            _config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
            _config.Formatters.JsonFormatter.MediaTypeMappings.Add(
                 new QueryStringMapping("datatype", "json", "application/json"));
            _config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            _config.Routes.MapHttpRoute("DefaultApi",
                "api/{controller}/{action}/{id}",
                new
                {
                    id = RouteParameter.Optional
                });
            try
            {
                _server = new HttpSelfHostServer(_config);
                _server.OpenAsync();
                // Common.SimpleLog.WriteLog("初始化室内读卡器");
                // mHSNReader = new MHSNReaderBLL(Common.AppConfigHelper.GetAppSettings("ReaderPort"));
                // Common.SimpleLog.WriteLog("打开服务");
                Console.WriteLine("打开服务");
                Console.ReadLine();
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr("打开服务异常 " + ex.ToString());
            }

        }
    }
}

YRTReaderController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace WebApiInConsole
{
    public class YRTReaderController : ApiController
    {
     // YRTRSNeaderBLL2 yRTRS;
        string epc;

        public YRTReaderController()
        {
            //yRTRS = new YRTRSNeaderBLL2();
            //yRTRS.getDataCallbackEvent = getDataCallback;
            //epc = string.Empty;
        }

        private void getDataCallback(string epc)
        {
            this.epc = epc;
        }

        /// <summary>
        /// 获取卡号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetCardNum()
        {
            //yRTRS.OpenCom();
            //yRTRS.BeginS();
            //while (string.IsNullOrEmpty(this.epc))
            //{
            //    System.Threading.Thread.Sleep(100);
            //}

            //yRTRS.Close();
            //return this.epc;
            return "123456789012";
        }
        [HttpGet]
        public string GetTest()
        {
            return "999";
        }

    }
}

配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
  <appSettings>
    <!--读卡器的串口号-->
    <add key="ReaderPort" value="com15"/>
    <!--室外读卡器读取间隔时间(毫秒)-->
    <add key="CardReaderTime" value="500"/>

    <!--读写卡apiIp-->
    <add key="ApiAddress" value="http://localhost:1234"/>

    <!--读卡器重连间隔-->
    <add key="DKQCLJG" value="20000" />
    <!--读卡器重连间隔睡眠-->
    <add key="DKQCLJGSM" value="5000" />
  </appSettings>
</configuration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值