1

我现在快疯了......我不明白为什么如果我在事件 button2_Click_2 中创建变量“服务器”,当它在 button3_Click_1 为空的情况下尝试访问它时。

我应该怎么做才能在 button3_Click_1 中访问它?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCWindows.Lib;
using System.Web.Http.SelfHost;
using System.Web.Http;


namespace TCCWindows
{
    public partial class FormPrincipal : Form
    {   
        HttpSelfHostServer server;
        HttpSelfHostConfiguration config;

        public FormPrincipal()
        {
            InitializeComponent();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            var config = new HttpSelfHostConfiguration(textBox1.Text);

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync();              
            MessageBox.Show("Server is ready!");  
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            server.CloseAsync();    
        }
    }

    public class ProductsController : ApiController
    {

        public string GetProductsByCategory(string category)
        {
            return (category ?? "Vazio");
        }
    }
}
4

3 回答 3

4

您在 Button2_Click_2 方法中声明了一个名为 server 的新变量。您需要将其分配给该字段,因此请更改

HttpSelfHostServer server = new HttpSelfHostServer(config);

进入

server = new HttpSelfHostServer(config);
于 2013-06-07T00:04:27.043 回答
1

请注意删除了局部变量声明符varHttpSelfHostServerin HttpSelfHostServer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCWindows.Lib;
using System.Web.Http.SelfHost;
using System.Web.Http;


namespace TCCWindows
{
    public partial class FormPrincipal : Form
    {   
        HttpSelfHostServer server;
        HttpSelfHostConfiguration config;

        public FormPrincipal()
        {
            InitializeComponent();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            config = new HttpSelfHostConfiguration(textBox1.Text);

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            server = new HttpSelfHostServer(config);
            server.OpenAsync();              
            MessageBox.Show("Server is ready!");  
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            server.CloseAsync();    
        }
    }

    public class ProductsController : ApiController
    {

        public string GetProductsByCategory(string category)
        {
            return (category ?? "Vazio");
        }
    }
}
于 2013-06-07T00:13:52.130 回答
1

您在两次按钮单击中都指的是server,但您实际上并没有实例化该对象。

在实例化config之后button2_Click_2,您还需要实例化server

server = new HttpSelfHostServer(config);

但是如果button3_Click_1事件在之前运行button2_Click_2,你仍然会抛出异常,因为server它仍然为空。

除非您有某种方式来强制单击哪个按钮,否则您可能希望将正在实例化的对象移动到构造函数中,以便在引用它们时确定它们不为空。

于 2013-06-07T00:04:29.967 回答