Call Windows Service from Controller - asp.net-mvc-3

I have a windows service created and installed. I want to call it from my MVC controller, as I am implementing MSMQ messaging service, so need to call Windows service.

Ideally the windows service should host a WCF service that your web application can contact. From there, it's a matter of creating a client, making the WCF call (to the service) and sending/receiving the information necessary.
Sorry, didn't see MSMQ until later. How about something like:
MessageQueueTransaction msgTx = new MessageQueueTransaction();
MessageQueue msgQ = new MessageQueue(#".\private$\Orders");
msgTx.Begin();
try
{
msgQ.Send("First Message",msgTx);
msgQ.Send("Second Message",msgTx);
msgTx.Commit();
}
catch
{
msgTx.Abort();
}
finally
{
msgQ.Close();
}
Then, of course, have a method of reading the messages in the service.

there is a .net library to do that as push notifications, it's name is signalr

Related

SignalR using Web API and Calling Service on other server

I have an application that makes calls from the client using JQuery to a Web API controllers, but then from such controller is makes a call to another server, where another controller picks up and does all the data logic (insertions, etc..) so basically (two different solutions and totally separate, no dependencies)
Client
Web API (this live in http://localhost:5020/)
-----------------
Some Server API here (this lives in http://localhost:4566)
Data Layer
SQL
So Web API makes calles to some server and saves or retrieves data.
I need to be able to add SinalR when something is saved to one of the databases on the other server. How can I design this so I get notifications something was saved in the client side since there are no dependencies?.
Do i add the HUBs and what not on the receiving server or on the client server, a bit confuse how that would work.
I would appreciate any clarification.
I usually use the method explained in this answer.
This will allow you to have hubs which can be invoked from frontend (JS) and backend (C#) as well.
Basically for the backend (C#) calls use a hubcontext. As explained by the SignalR team here too.
Simple code (in your hub class):
public class YourHubClass: Hub
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<YourHubClass>();
// ...
public static void Send(string message) {
hubContext.Clients.All.addMessage(message);
}
}
Now you can call the static method in your controller:
YourHubClass.Send("Hello!");
And the clients will receive the message by the event addMessage(message)

Persisting Web Requests in ASP.NET Web API

I would like to build some kind of JSON.API scheduler service to play web request later on my production server.
It should be possible to POST this to localhost/app/events on my development machine:
{
“schedule”: {
“start”:”2014-12-31”,
“repeat”:”annualy”
},
“requst”: {
“verb”:”POST” ,
“href”:“http://localhost/app/emails”,
“body”:{
“type”:”HappyNewYearWishes”
}
}
}
Given ASP.NET API as an implementation mechanism, how to parse and persist “request” part to database, so Production Server will be able to
POST /emails
{
“type”:”HappyNewYearWishes”
}
according to the schedule? The problem is that the deployment root is different for development machine and production server, so I cannot persist “href” as it is. What kind of mechanisms of ASP.NET Web API for route parsing, transformation, and persistence are useful here?
You can get the Route parameters using
RouteData.Values
Also you can get the eventual querystring parameters using
Request.Url.Query
With the results of these two objects stored, you can easily rebuild the URL.

multiple self-hosted webapi on same server

I want to host, mutiple self-hosted webapi's along with the web-site on the same machine / windows 2k8 server. For hosting the website I am using IIS. and for webapi's I would be using self-hosted webapi 2. How do I configure using self-hosted webapi, so that everything can work in sync on same server.
So lets say I will host the website at http://example.com/mysite and I will be hosting the webapi at http://example.com/apiws and http://example.com/apiui
I am using windows service for the configuration. This is how the web-api self hosting looks like as of now - for first webapi.
protected override void OnStart(string[] args)
{
_config = new HttpSelfHostConfiguration(ServiceAddress);
_config.MapHttpAttributeRoutes();
_config.Routes.MapHttpRoute("DefaultApi",
"apiws/{controller}/{id}",
new { id = RouteParameter.Optional });
_server = new HttpSelfHostServer(_config);
_server.OpenAsync().Wait();
}
the configuration is almost same for the second server as well.
My question is having all of them working on the same port, is it possible? are there any issues which might arise? etc?
you are confusing web-api with mvc.
MVC/IIS/websites needs hosting on domain on sub-domain.
webapi are just for listening to the request and providing the data response.

How to communicate with WEB API in Windows Phone?

I am working with push notifications in windows phone. Notification channel uri is changing while re installing the app in the windows mobile. so there is a problem to send notification to the channel url.
By using web service i am storing the url in database.
I want to update the uri if it is changed using device id.
To do that, i have a WEB API method [HTTPGET] to check the deviceid availability in the database. It is developed to return True/False.
Now my question is, How can we communicate with the service and how can we handle the web api bool return type using C# in windows phone?
Thank You.
To communicate with the service with httpget request you have to use HttpClient class
Here how I do:
HttpClient client = new HttpClient();
string result=await client.GetStringAsync(url);
note that the event which call the web service must be declared async, because the httpclient is purely Asynchronous

WCF and Javascript: Not Found

this occured when I called my WCF Service
as follows:
hosted WCF Service in IIS in name testWCF.
web application name is webWCF.
gave scriptreference as: http://localhost/testWCF/mywcf.svc
In JavaScript called the method GetSerivceCharge as
var x = new thetest.Backoffice.IBackofficeService();
var y = x.GetSerivceCharge(res);
function res(result) {
alert(result);
}
it results as follows:
Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'GetSerivceCharge' failed with the following error: Not Found
Server Error in '/webWCF' Application.
HTTP Error 404 - Not Found.
Isn't this a cross domain issue? I see that your WCF web service is hosted in IIS and accessible through http://localhost/testWCF/mywcf.svc while your web application is using the ASP.NET development server which means it is hosted on http://localhost:SOME_PORT/webWCF.
AFAIK, AJAX is used when you want to invoke a web service from javascript. Due to cross domain restrictions you need to have the web service and the web application hosted on the same domain, or create a some proxy/bridge that will delegate calls to the web service.
Try typing the web service URL directly into your browser's address bar and see what happens.
Maybe it's this dumb: GetSerivceCharge is misspelled. Try GetServiceCharge. And if that's not it, you should post your web method's signature.
I think you need to add a reference to your WCF in your project.
If you are using VS 2008 try adding a reference to your web service:
In solution explorer right click on your project. Then select Add Service Reference. Then click on discover in the dialog that pops up and you should be able to find your WCF service.

Resources