Ankush Band

Ankush Band

  • NA
  • 195
  • 7.4k

self hosting web api

Aug 29 2017 9:46 AM
How to access self hosting web api in windows application from another system through ajax call?
 
when I access self hosted webapi method of windos application from client application through ajax call from another system then it can't access. I explain my self hosted application as follows:
 
I create windows application and it derive from apicontroller it host self in system A with using System.Web.Http.SelfHost dll. following is my code :
 
In Program.cs :
  1. using System;  
  2. using System.Web.Http;  
  3. using System.Web.Http.SelfHost;  
  4. using System.Windows.Forms;  
  5. namespace SelfHost  
  6. {  
  7. static class Program  
  8. {  
  9. [STAThread]  
  10. static void Main(string[] args)  
  11. {  
  12. var config = new HttpSelfHostConfiguration("http://182.150.1.1:8080/api");  
  13. config.Routes.MapHttpRoute(  
  14. name: "API",  
  15. routeTemplate: "{controller}/{action}/{id}",  
  16. defaults: new { id = RouteParameter.Optional }  
  17. );  
  18. using(HttpSelfHostServer server = new HttpSelfHostServer(config))  
  19. {  
  20. server.OpenAsync().Wait();  
  21. Application.EnableVisualStyles();  
  22. Application.SetCompatibleTextRenderingDefault(false);  
  23. Application.Run(new MainForm());  
  24. }  
  25. }  
  26. }  
  27. }  
In ProductsController.cs :
  1. using System.Web.Http;  
  2. namespace SelfHost  
  3. {  
  4. public class ProductsController : ApiController  
  5. {  
  6. MainForm mainForm = new MainForm();  
  7. [HttpPost]  
  8. public IHttpActionResult GetResponse(string message)  
  9. {  
  10. mainForm.TextBoxRequestMsg = message;  
  11. return Json("Response from server successfully with message " + message);  
  12. }  
  13. }  
  14. }  
In MainForm.cs :
  1. using System;  
  2. using System.Windows.Forms;  
  3. namespace SelfHost  
  4. {  
  5. public partial class MainForm : Form  
  6. {  
  7. private string _TextBoxRequestMsg;  
  8. public string TextBoxRequestMsg  
  9. {  
  10. get { return txtReqMsg.Text; }  
  11. set  
  12. {  
  13. _TextBoxRequestMsg = value;  
  14. MessageBox.Show( _TextBoxRequestMsg);  
  15. }  
  16. }  
  17. public MainForm()  
  18. {  
  19. InitializeComponent();  
  20. }  
  21. }  
  22. }  
Following is client application this application and is hosted on another system using this application we can call of self hosted webapi method GetResponse() from another system using jquery Ajax call. Following is my client code:
  1. var message = "test request";  
  2. $.ajax({  
  3. url: 'http://182.150.1.1:8080/api/Products/GetResponse?message=' + message,  
  4. cache: false,  
  5. type: 'POST',  
  6. dataType: 'json',  
  7. contentType: 'application/json; charset=utf-8',  
  8. async: false,  
  9. processData: false,  
  10. success: function (data) {  
  11. alert("Successfully get response.");  
  12. },  
  13. error: function (err) {  
  14. alert("Call to web api failed.");  
  15. }  
  16. });  
Also I stop firewall of both system also I enable inbound rule of "World Wide Web Services (HTTPS Traffic-In)"
 
tell me any possible solutions.

Answers (2)