简体   繁体   中英

How to allow service interaction through a web api

I have built an application to host a game server and would like to add a web API for external control. The application is currently just a console app but will eventually be a windows service. I've added the web API and am able to respond to requests. I've also added a TcpServer to the console app and a TcpClient to the web API to allow the web API to send commands to the console app.

However, since the web API is hosted by the same console app that hosts the game server, is there a way I can send commands directly from the web API controller class to the game server host class?? It currently looks something like this (common TCP code abbreviated to save space):

public class GameServiceManager
{
    private IDisposable _restApi;
    GameServer _gameServer;

    public void StartService(NameValueCollection appSettings)
    {
        _restApi = WebApp.Start<MyHttpHostController>(url: restApiUrl);
        _gameServer = new GameServer();
        // also starts TCP Server stuff to receive command
    }
}

public class MyHttpHostController
{
    public void Configuration(IAppBuilder appBuilder)
    {
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{action}/{id}",
            defaults: new { controller = "RestApi", id = RouteParameter.Optional }
        );

        appBuilder.UseWebApi(config);
    }
}

public class RestApiController : ApiController
{
    [HttpGet]
    public IHttpActionResult resetGameServer()
    {
        // creates TCP Client to send command
        return Ok();
    }
}

The GameServiceManager is instantiated in the console app and then calls StartService(). In other applications, I've used Events to pass commands back and forth between parent and child classes, but I don't seem to have much control over the web api part of things.

If you can't make your GameServiceManager class static, as someone else suggested, you could maybe apply a singleton pattern to it.

public class GameServiceManager
{
  public static GameServiceManager Instance { get; protected set; }

  public GameServiceManager()
  {
    if (GameServiceManager.Instance != null)
      throw new Exception("singleton has already been created");

    GameServiceManager.Instance = this;
  }
}

Now, anywhere in the rest of your code, you can easily get a reference to your GameServiceManager object by using the static property GameServiceManager.Instance

I have used SignalR pub and sub which works fine in web interface as well. I already had experience using EventAgregator in Prism and I was looking to implement same in my web api and found EventAggregator Proxy Signal R By Anders . Based on above article I done a quick HelloWorld Application which may help you out. This pattern can work between web, web api and also windows application.

Here are code samples from Anders

    [HttpPost]
    [Route("GenericEvent")]
    public void GenericEvent([FromBody] string text)
    {
        eventAggregator.Publish(new GenericEvent<string>(text));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM