winform宿主webapi

引用的Dll

winform宿主webapi_第1张图片

添加配置文件packages.config



  
  
  
  

新建Controller文件夹里面的ValuesConntroller.cs类

 [RoutePrefix("api/values")]
    public class ValuesController : ApiController
    {
        [HttpGet]
        [Route("st")]
        public string GetServerTim(string id)
        {
            try
            {
                string result = Program.FormCheck.GetJsonById(id);

                return result;
            }
            catch
            {
                return "failed :"+ id;
            }
        }
        protected HttpRequestBase GetRequest()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_RequestContext"];//获取传统context

            HttpRequestBase request = context.Request;//定义传统request对象

            return request;
        }
        private string GetClientIp(HttpRequestMessage request = null)
        {
            request = request ?? Request;

            if (request.Properties.ContainsKey("MS_HttpContext"))
            {
                return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
            }
            else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
            {
                RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
                return string.Format("Address:{0}, Port: {1}", prop.Address, prop.Port.ToString());
            }
            else if (HttpContext.Current != null)
            {
                return HttpContext.Current.Request.UserHostAddress;
            }
            else
            {
                return null;
            }
        }
    }

Program类中这样写

 public static class Program
    {
        public static CheckInterface FormCheck = null;
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            FormCheck = new CheckInterface();
            Application.Run(FormCheck);
        }
    }

Form中这样写

  HttpSelfHostConfiguration config = null;
        HttpSelfHostServer server = null;
      
        public CheckInterface()
        {
            InitializeComponent();
            renderDgv_Interface.AllowUserToAddRows = false;
        }

        private void CheckInterface_Load(object sender, EventArgs e)
        {
            RefreshWebApi();
        }
        private void RefreshWebApi()
        {
            config = new HttpSelfHostConfiguration(Url);// "http://192.168.0.163:8085");
            config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional });
            server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
        }

启动之后浏览器访问http://192.168.0.163:8085/api/values?id=001即可看到weiapi接口已经成功在Winform上运行。

如果想要访问Webapi执行方法与WinformUI界面交互,用委托另外还要判断几个属性

winform宿主webapi_第2张图片

 

  delegate void DAddRdgvData(List DataSourse);

        private void AddRdgvData(List DataSourse)
        {
            if (renderDgv_Interface.InvokeRequired)
            {
                while (!this.renderDgv_Interface.IsHandleCreated)
                {
                    if (this.renderDgv_Interface.Disposing || this.renderDgv_Interface.IsDisposed)
                        return;
                }
                DAddRdgvData d = new DAddRdgvData(AddRdgvData);
                this.renderDgv_Interface.Invoke(d, new object[] { DataSourse });
            }
            else
            {

                for (int i = 0; i < DataSourse.Count; i++)
                {
                    if (ListId.Contains(DataSourse[i].Id))
                    {
                        for (int j = 0; j < renderDgv_Interface.RowCount; j++)
                        {
                            if (renderDgv_Interface.Rows[j].Cells["rdgv_Interface_id"].Value != null)
                            {

                                if (DataSourse[i].Id.Equals(renderDgv_Interface.Rows[j].Cells["rdgv_Interface_id"].Value.ToString()))
                                {
                                    renderDgv_Interface.Rows[j].Cells["rdgv_Name"].Value = DataSourse[i].Name;
                                    renderDgv_Interface.Rows[j].Cells["rdgv_Time"].Value = DataSourse[i].Time;
                                    renderDgv_Interface.Rows[j].Cells["Rdgv_NumberTimes"].Value = DataSourse[i].Numberoftimes;
                                    renderDgv_Interface.Rows[j].Cells["rdgv_Return"].Value = DataSourse[i].CheckResult;
                                }

                            }
                        }
                    }
                    else
                    {
                       //这里向控件传数据
                    }
                }
            }
        }

winform宿主webapi_第3张图片

你可能感兴趣的:(Winform)