0、背景
公司网络管控无法提供接口给供应商进行异地调试,编写有个测试接口供他人使用,接口内容写在文本文件中,可以直接发给对方测试。
1、代码
主函数
class Program
{
static void Main(string[] args)
{
//var httpobj = new System.Net.HttpListener();
//httpobj.Prefixes.Add("http://+:7080/");
//httpobj.Start();
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:7080/");
options.Urls.Add("http://127.0.0.1:7080/");
//options.Urls.Add("http://10.245.68.11:7080");
//options.Urls.Add(string.Format("http://{0}:7080", Environment.MachineName));
options.Urls.Add("http://+:7080/");
using (WebApp.Start<Startup>(options))
{
Console.WriteLine("Conmet MESOperationServiceFake Service started...");
Console.WriteLine("http://localhost:7080/swagger");
System.Threading.Thread.Sleep(-1);
}
}
}
StartUp
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{message}",
defaults: new { message = RouteParameter.Optional }
);
config.EnableSwagger(c => c.SingleApiVersion("v1", "MESOperationService API")).EnableSwaggerUi();
//清除xml格式,使用json格式
config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
appBuilder.UseWebApi(config);
}
}
WebApi
[RoutePrefix("api")]
public class MESOperationServiceController : ApiController
{
[HttpGet]
[HttpPost]
public IHttpActionResult GetMesWorkOrderById(string wo_id = null)
{
//var table = MESOperationServiceFake.DBUtil.MSSQLHelper.ExecuteDataTable(" select top 100 * from mes_work_order where wo_id in ('000002846670-001','000002834455-001','000002835128-001','000002833985-001') order by req_finish_time_local desc");
//return Json(table);
string result = File.ReadAllText("FakeFile/workorders.txt");
var res = Newtonsoft.Json.JsonConvert.DeserializeObject<List<mes_work_order>>(result);
mes_work_order tempOrder = new mes_work_order();
if (wo_id != null)
{
tempOrder = res.Where(s => s.wo_id == wo_id).FirstOrDefault();
}
else
{
tempOrder = res.FirstOrDefault();
}
return Json(tempOrder);
}
}
workorders.txt 文件内容为json格式工单。
[
{
"complete_qty": 0,
"cust_info": "3116631",
"customer_part_no": "H430000000074-60",
"cycle_time": 21,
"last_edit_at": "/Date(1667382706140+0800)/",
"last_edit_by": "bian",
"last_edit_comment": null,
"line_ent_name": "AssyLine1",
"line_sched_id": 926,
"notes": "xxxxx",
"order_type": 1,
"part_no": "10088635",
"part_revision": "-",
"process_id": "ASSYL1-STAND",
"quality_ratio": 0,
"release_time_local": "/Date(1667431800000+0800)/",
"release_time_utc": null,
"req_finish_time_local": "/Date(1667469600000+0800)/",
"req_finish_time_utc": null,
"req_qty": 27,
"requirement": "xxxxxxxx",
"sap_work_order": "2022846670",
"state_cd": 2,
"wo_desc": null,
"wo_id": "2022846670-001",
"wo_priority": 80,
"customer_name": "xxxxx",
"ent_id": 7,
"part_description": "xxxxx",
"sap_short_name": "xxxxxx"
},{
"complete_qty": 0,
"cust_info": "4116631",
"customer_part_no": "xxxxxxx-60",
"cycle_time": 30,
"last_edit_at": "/Date(1667382706140+0800)/",
"last_edit_by": "abbb",
"last_edit_comment": null,
"line_ent_name": "AssyLine1",
"line_sched_id": 926,
"notes": "eeeeeee",
"order_type": 1,
"part_no": "10088635",
"part_revision": "-",
"process_id": "ASSYL1-STAND",
"quality_ratio": 0,
"release_time_local": "/Date(1667431800000+0800)/",
"release_time_utc": null,
"req_finish_time_local": "/Date(1667469600000+0800)/",
"req_finish_time_utc": null,
"req_qty": 27,
"requirement": "eeeeeee",
"sap_work_order": "3022846670",
"state_cd": 2,
"wo_desc": null,
"wo_id": "3022846670-001",
"wo_priority": 40,
"customer_name": "eeeee",
"ent_id": 7,
"part_description": "eeee",
"sap_short_name": "eeee"
}
]
SwaggerConfig
using System.Web.Http;
using WebActivatorEx;
using MESOperationServiceFake;
using Swashbuckle.Application;
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace MESOperationServiceFake
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "MESOperationServiceFake");
})
.EnableSwaggerUi(c =>
{
});
}
}
}