0

I have created a simple self-hosted web api and a server that is basically a windows service.

Service has the following code:

protected override void OnStart(string[] args)
        {
            var config = new HttpSelfHostConfiguration("http://localhost:8085");

            config.Routes.MapHttpRoute(
               name: "API",
               routeTemplate: "{controller}/{action}/{id}",
               defaults: new { id = RouteParameter.Optional }
           );

            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
        }

Web API

 public class ValuesController : ApiController
    {
        public String GetString(Int32 id)
        {
            return "Test data";
        }
    }

Is there a way to do object sharing between the service and the web api? For example, connect to a database server in the service and use the connection object in web api etc.

Basically for this problem, I don't have any idea if this is possible and how. I searched on the internet but unable to reach the desired location.

  • 1
    It's not clear to me what you're trying to accomplish. If these are separate applications then they can share data, or they can share source code and other compiled dependencies, but "sharing an object" doesn't make much sense. What's the actual problem you're trying to solve here? – David Jul 05 '23 at 12:24
  • My goal is to connect to the hsm and do different tasks like encryption, creating keys etc. One of the problem that I want to solve is to create a temporary key in one function of web api and use that key in another function of the web api. For this purpose, I am thinking to connect to the hsm in windows service code and somehow keep the session open/connected so when the second function call comes, it can find that open session and get the temporary key generated in the previous function call. – Usama Alam Jul 06 '23 at 07:41
  • What is the actual requirement for “keeping the session open”? If a single application needs to maintain a socket connection somehow then I doubt two applications could share that. But it seems *very strange* to require that. When you generate this temporary key, is that just a value that you send in subsequent requests? Are these HTTP requests? Something else? If all you need to do is provide a key value then you can store that value anywhere you like for both applications, or directly send the value to the WebAPI application. It’s just not clear what you’re really trying to do. – David Jul 06 '23 at 11:39

0 Answers0