top of page
  • Writer's picturemohit vashistha

Create embedded webserver in Windows application

Updated: May 10, 2022

There are many times we need to access the excel, word and other COM component from the web application. But we application runs in IIS scope and thing don’t work properly as they do in a normal windows application code. We can create a windows application that has a API handler that runs in user’s scope and in interactive mode to run VBA automation code properly.


Also, the webserver can be used when we need to send data from another application to our application. Some examples:


1. Passing a phone number from a VOIP to desktop CRM application to get caller’s details.

2. Passing data from Outlook Addin to CRM application.

3. Etc..


We can create a window application with webserver embedded that start on stating of windows application.


Following step will walk you through the process of creating the same.


Create a new WPF/ Window application project.


Install following Nuget Packages:

1. Microsoft.AspNet.WebApi

2. Microsoft.AspNet.WebApi.Client

3. Microsoft.AspNet.WebApi.Core

4. Microsoft.AspNet.WebApi.Owin

5. Microsoft.AspNet.WebApi.OwinSelfHost

6. Microsoft.AspNet.WebApi.Tracing

7. Microsoft.AspNet.WebHooks.Common

8. Microsoft.AspNet.WebHooks.Custom

9. Microsoft.AspNet.WebHooks.Custom.Api

10. Microsoft.Owin

11. Microsoft.Owin.Host.HttpListener

12. Microsoft.Owin.Hosting

13. Swashbuckle (if need swagger UI)


After installing all the nuget create a new namespace “WebServer”


Add 3 new class in webserver:


1. Startup.cs

/// <summary>

/// Class Startup that add configuration to web server

/// </summary>

public class Startup

{


#region Methods


/// <summary>

/// Configurations the specified application builder.

/// </summary>

/// <param name="appBuilder">The application builder.</param>

public void Configuration(IAppBuilder appBuilder)

{

// create new http configuration

var config = new HttpConfiguration();

// uncomment following code if you need to enable swagger UI

//SwaggerConfig.Register(config);



// map all the classes that have RoutePrefix attribute defined

config.MapHttpAttributeRoutes();




// initialize custom webhooks & APIs

config.InitializeCustomWebHooks();

config.InitializeCustomWebHooksApis();




// add a listner to log the webserver log

var listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];

listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;

// add tracing configured to log error only

var traceWriter = config.EnableSystemDiagnosticsTracing();

traceWriter.IsVerbose = true;

traceWriter.MinimumLevel = TraceLevel.Error;


// set the config to the application

appBuilder.UseWebApi(config);

}


#endregion

}


2. WebServer.cs

/// <summary>

/// Class MyWebServer for

/// </summary>

internal class MyWebServer

{

#region Methods


/// <summary>

/// Starts this instance.

/// </summary>

public void Start()

{

string webHookSenderBaseAddress = "http://localhost:9012";

try

{

WebApp.Start <Startup>(webHookSenderBaseAddress);

}

catch (Exception exception)

{

// logs exception

}

}

#endregion

}


3. WebHookHandler.cs

/// <summary>

/// Class WebhookController for controlling calls to Sales Closer

/// </summary>

[RoutePrefix("api/webhook")]

public class WebhookController : ApiController

{

#region Methods



[HttpGet]

[Route("GetData")]

public string GetData(string ID)

{

return $"{ID}: Hello from Web-server";

}

}



Now start the server with your application, in WPF app go to App.xaml.cs and in constructor add the following code:


/// <summary>

/// Interaction logic for App.xaml

/// </summary>

public partial class App : Application

{


public App()

{

// create new sales closer web server and start it

var webserver = new MyWebServer();

webserver.Start();

}

}


Now your web server is up and running on http://localhost:9012.



If you want to enable the Swagger UI then uncomment the SwaggerConfig.Register(config); line from the startup.cs. And, add a new SwaggerConfig class in the code.


public class SwaggerConfig

{

public static void Register(HttpConfiguration config)

{

config.EnableSwagger(c => c.SingleApiVersion("v1", "My Wrapper APIs"))

.EnableSwaggerUi();

}

}




If you want to minimize application to system tray you can use the following code:

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();


System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();

ni.Icon = new System.Drawing.Icon("App_Icon.ico");

ni.Text = "My Local server";

ni.Visible = true;


ni.DoubleClick +=

delegate (object sender, EventArgs args)

{

this.Show();

this.WindowState = WindowState.Normal;

};

}



protected override void OnStateChanged(EventArgs e)

{

if (WindowState == System.Windows.WindowState.Minimized)

this.Hide();


base.OnStateChanged(e);

}

}


16 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page