I saw this translator webservice
http://api.microsofttranslator.com/V2/Soap.svc
but I am not allowed to add a new web service to our application, it must only be called inside a Controller and there to use its method "Translate" and also pass the parameter so I can get the return string. Is there a way in doing this?
Thank you!
Mentioning that you need to call the service from a controller suggests that you want to call it from inside code. You might call your service like shown here. Using the ChannelFactory gives you the ability to create your own service clients. You won't need to add a service reference to do that.
I also wrote a static wrapper making it easier to access services. Just ask if you need :-)
Thank you for all who response.
By the way here's the answer for my own question.
http://msdn.microsoft.com/en-us/library/ff512421.aspx
Related
I have an API project created in C#. There's a desire to "simplify" something that it does which would mean creating a new API endpoint, which in the background would call several existing endpoints within the same API.
I'm concerned that this sort of recursion, an API calling itself, is bad practice and it'd be a better solution to have applications that make use of the API call the existing endpoints individually and manage the returned data within their own separate application logic. Am I right to be concerned?
Thanks
There is a whole lot of "it depends" in the answer.
If you moved logic out of your controllers and into shared libraries would it make sense for these two libraries to call one another directly, or would they be in the same library?
If they're in the same library and the data they access should all be in that same service, then I would call within the library.
I have a web-service method that returns a
List<KeyValuePair<string,double>>
However when I use use that method in a service reference, the object it returns is
List<KeyValuePairOfStringDouble>
I cannot figure out how to use this.
Your service reference is trying to make the best out of the WSDL it gets. But your service and your application have been playing games with each other. They were never allowed to directly talk. So you are now stuck with something that looks like what you need and is called like what you need, but it is not exactly what you need.
Simple solution is to build a new list that is what you syntactically need:
List<KeyValuePair<string,double>> yourlist = serviceResult.Select(kvp => new KeyValuePair<string, double>(kvp.Item1, kvp.Item2)).ToList();
For a better solution in the future you should let both ends of your service communication know what the other end is up to syntactically. This is called a contract assembly.
This thread on StackOverflow and the links that it contains might be a good start if you want to build one for your service.
I am using a System.Web.Http.ApiController in my application. One of the functions I need is to have an admin screen with a button on it that will initiate some method on the controller to move data from one table to another. The action should complete within a few seconds. There need be no parameter sent to the action and I assume that I can do this with a normal HTTP call to a method on the WebAPI controller.
However what type of call should this be? GET, POST, PUT ?
None of these really seem to fit so I would appreciate some advice.
If you are "moving" data then the operation is "unsafe" and "non-idempotent" therefore POST correctly describes those semantics.
Given REST is based on resource access, neither GET, POST or PUT apply. You're retrieving nor sending resources. You're, frankly, calling a Remote Procedure Call.
You can however expose this RPC as a resource, though one that only allows one method. It's up to you to name and implement this. I'd say you POST to /Admin/BeginCopyingData/.
I would go for a POST or PUT - it doesn't really matter.
Wait, what?
Honestly, if it is to kick off some internal admin system function rather than to add an entity to a truly RESTful API then I would advise you to avoid getting caught up in HTTP semantics, as you aren't doing REST.
You don't even need to use Web Api, you could just have the form on the page post back to a standard action method on your MVC controller.
Given that POST in REST can be interpreted as "process" , I would go for POST.
Also, POST is not idempotent so you can do pretty much whatever you want with it.
I'm playing around with WCF and wondering about the following. I'd like to be able to get a list of all available service methods and parameters associated with these methods.
Now, I've tried to work with the ServiceDescription namespace, but it seems flawed. For one .svc it works, for another it doesn't.
What would be an okay way to approach this? Any tips?
You could just download the WSDL of the webservice and parse that (it is XML, see http://www.w3.org/TR/wsdl) since the WSDL contains all information like interfaces/methods/parameters etc.
Some helpful resources including source code for doing what you want:
http://webservicestudio.codeplex.com/
http://wizdl.codeplex.com/
http://soap-sec.sourceforge.net/
I have an asp.net MVC2 application that needs to call a web service from the controller. How do I do this? It is a RESTful service that returns Json data.
I cant seem to find a good example.
Thanks
You call it the same way you would do in any ASP.net application, it is not connected to MVC at all.
Either add a reference and use that (easiest) or go with the manual method: here is a guide, see at towards the end (fig. 14 in particular) for consuming such services:
http://msdn.microsoft.com/en-us/magazine/dd943053.aspx
I have written my own ActictiveResource client framework, which allows the consumer to specifiy the http provider and the serialisation provider. The generic activeResource class has the four main verbs (get,put,post,delete) as methods which it calls against a specified resource url, passed in at cunstruction. the fololwing is an example of getting a product item from teh service:
ActiveResource<Product> arProduct = new ActiveResource<Product>(jsoSerializer,liveHttpProv,"https://company/product/1452");
//Get verb
Product prod = arProduct.Get();
Of course you can also use the other verbs on the object to put, post and delete.
arProduct.Post(prod);
The code, basically, wraps up the underlying http post,put, get functions and takes care of the serialiasation of the payload to objects. It has been a very useful component which I have used over and over again. The code can be easily called from a controller, it may be worth using a IOC container (I am using Th eUnity block) to instatiate you providers
Hope this helps
I would put together a simple class that acts as a "client" that makes a web-request from the URL, and then return the response as a string.
From there you can deserialize the JSON data using either the JSON serialization that ships with WCF, or the most excellent JSON.Net library. You will need to create a simple data class that is structured in the same way as the JSON data your expecting back.
You could also combine the two and have your client class return the deserialized object directly.