Buscar contenidos

lunes, 4 de febrero de 2019

Broken Backward Compatibility in System.Web.Http



https://www.c-sharpcorner.com/forums/self-hosting-web-api

https://stackoverflow.com/questions/37207571/broken-backward-compatibility-in-system-web-http


This turned out to be a lot simpler than it looked above. The client side code was the one using the System.Web.Http.SelfHost.HttpSelfHostServer so as a result it was pulling the assembly System.Web.Http.SelfHost.dll into the bin directory.
The unit tests assembly was pulling in System.Web.Http.dll as both it and the server side required it.
The selfhost dll was v4.0.0.0 and the system.web.http was v5.2.3. This is where the problem came from. The fix was to ensure v5.2.3 of System.Web.Http.SelfHost.dll was in the bin directory and add a redirect to the app.config of the unit test assembly.

---

Ankush Band

Ankush Band

  • 1.6k
  • 146
  • 1.6k

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)

0
Ankush Band

Ankush Band

  • 1.6k
  • 146
  • 1.6k
Aug 30 2017 7:41 AM
Finally I found solution for above question and I am successfully executed above scenario.
install Microsoft.AspNet.WebApi.Cors
package through NuGet Package Manager
after install this package you can see dll reference System.Web.Http.Cors.dll
and then add the following code in Program.cs for enabling EnableCorsAttribute
as
  
  1. var cors = new EnableCorsAttribute("*""*""*");  
  1. config.EnableCors(cors);  
look like this-
  1. static void Main(string[] args)  
  2. {  
  3. var cors = new EnableCorsAttribute("*""*""*");  
  4. var config = new HttpSelfHostConfiguration("http://192.168.3.81:8080/api");  
  5. config.EnableCors(cors);  
  6. config.Routes.MapHttpRoute(  
  7. name: "API",  
  8. routeTemplate: "{controller}/{action}/{id}",  
  9. defaults: new { id = RouteParameter.Optional }  
  10. );  
  11.   
  12. using(HttpSelfHostServer server = new HttpSelfHostServer(config))  
  13. {  
  14. server.OpenAsync().Wait();  
  15.   
  16. Application.EnableVisualStyles();  
  17. Application.SetCompatibleTextRenderingDefault(false);  
  18.   
  19. Application.Run(new MainForm());  
  20. }  
  21. }  
and then add following attribute in ProductsController.cs
  1. [EnableCors(origins: "*", headers: "*", methods: "*", SupportsCredentials = true)]  
look like this-
  1. namespace SelfHost  
  2. {  
  3. [EnableCors(origins: "*", headers: "*", methods: "*", SupportsCredentials = true)]  
  4. public class ProductsController : ApiController  
  5. {  
  6. MainForm mainForm = new MainForm();  
  7.   
  8. [HttpGet]  
  9. public string GetResponse()  
  10. {  
  11. mainForm.TextBoxRequestMsg = "Request get successfully from client.";  
  12. return "Response from server successfully.";  
  13. }  
  14. }  
  15. }  
in my above question I was told like i have done some settings but actually not required any setting for this.
just give cross domain rights through Microsoft.AspNet.WebApi.Cors package.

No hay comentarios:

Publicar un comentario