samedi 22 juin 2019

Can I deploy web service api from WPF App which is in Visual C# project?

I am very new to ASP.NET and stuck in this problem for a very long time. I have a WPF App(.NET Framework) that currently works.It's a program being used for controlling some external device(a Robot).I have the C# project of that WPF App as an editable project in my Visual studio 2017.

What I want to do is use that program( WPF App ) to deploy web api service and use fuction in WPF App from the web api , for example I have command "A.FORWARD" to move the robot forward in the button in the WPF App, I want to make an web api service calling "localhost:XXXX/api/moveforward" to call that command("A.FORWARD").

First,I tried creating web api service with .NET framrwork 4.6.1 and copying code and dependencies for controlling robot from WPF App to it.but After deploying web api,I found out that the external device(robot) is not reachable from web API.

then Secondly I tried copying all code for deploying Api service to the WPF App. But I found out the api service won't run (Application_Start() in Global.asax.cs) and I cant force it to run .

To forced the WPF App to run API, I copied all needed file especially "Global.asax.cs" to run API which contain this

public void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}

and I add it to call by clicking some button from "MainWindow.xaml.cs".

public void ButtonEmotion_Click(object sender, RoutedEventArgs e)
{
new Global().Application_Start();
}

those effort says

"Exception thrown: 'System.InvalidOperationException' in System.Web.dll An unhandled exception of type 'System.InvalidOperationException' occurred in System.Web.dll This method cannot be called during the application's pre-start initialization phase."




Aucun commentaire:

Enregistrer un commentaire