Creating Web Service in ASP.NET from existing API - c#

I am looking to create a web service from an existing API. The existing API is from a separate Windows program that allows the calculation of mileages between 2 more more locations. The API is in C#. There are also different types of calculations (Shortest Distance, Lowest-Cost, etc).
I am confused on how to create a RESTFUL web service that will allow multiple locations. Example in RESTFUL API tutorials show for an example api/books/1/To Kill A Mockingbird/ would get the record for a book. But how would this be for multiple locations? Would it be api/sd/Kansas City,MO/Chicago,IL/Miluwake,WI? How would it know if I'm looking to get the calculations of 2 vs. 10 locations? Could I program it to have 10 locations entered in the URL?

You don't have to use url segments to accept parameters. You could use a query string parameter with place names, say, semicolon-delimited, then split them out in your code.
/api/sd?cities=Kansas City,MO;Chicago,IL;Miluwake,WI
Then in your code just use Request.QueryString["cities"].Split(';');

I think that as your route grows, you may run into other problems such as a url that is too long (there are limits to URL size, from what I understand -- https://stackoverflow.com/a/1051565/507025). If you're going to have and API that can accept an endless number of parameters, I think you would want to do something like sending a JSON object with the request so the API controller can process it.
Additionally, a JSON object might be simpler to process, code wise.
Try here: https://stackoverflow.com/a/20226220/507025

Related

MyAnimeList REST API limitations

I'm currently developing a C# API that connects to MAL for developing clients applications.
My problem is that currently I'm using the REST API of MAL documented in this page: http://myanimelist.net/modules.php?go=api#animevalues
Everything there function wonderfully, but my problem is that with only that services I cannot query about a user anime list or manga list. Neither for their score, progression, etc of their anime/manga.
My question is that I'm wrong and that info can be obtain with that API?
I know that I can access a user animelist (or mangalist) like: http://myanimelist.net/animelist/ and parse the content to obtain the info. But I was wondering if I can get that info with the REST API that is more robust (not as weak to changes in the page as parsing the html).
Thanks
It seems odd that the API does not appear to have this capability, but I also couldn’t find any information on it.
What you can use however is the following URL:
http://myanimelist.net/malappinfo.php?u=<username>
Apart from the username, it accepts a type parameter to query either the anime or manga list. There appears to be also a status parameter, but I have no idea how that works.
I also just found this thread on the topic.

Best practice to pass a very long string trough WebAPI to another WebAPI Service

im fairly new to ASP.Net programming.
I have the following question.
I have two WebAPI Projects on two different servers. One API collects some data from different XML files. The other API has the only task to look if a file exists on the server. So the first API collects the data, sends a string with the file and path name to the other API, wait for an answer and put this answer to the collected data.
[
{"Id":12858,"Name":"Foo","Depth":5,"ParentId":12818,"ClipId":-1,"Clips":[here comes the result]},
{"HierarchyId":12929,"Name":"Bar","Depth":5,"ParentId":12818,"ClipId":-1,"Clips":[here comes the result]}, ...
]
My first approach was to make an request for every file. So it can be normal that 50 requests had to be taken before the data collection was finished. That works really fine, but very slow too. It takes much time waiting for the response for thes 50 linear requests.
The idea is to send an long string, an array or a list that contains all strings with the path and the filename. E.g. C:\foo.txt;C:\bar.txt;....
With a GET Request the url ist far too long.
Which is a good practice to solve this? I try several hours different solutions with POST requests. But i don't think that this is THE solution. A deep research on some search machines given no good answer too. I'm sure there must be an easy solution for this.

Designing code for accessing a WebService that accepts xml requests

I'd like to create C# code that accesses a WebService that has only 1 method:
public string HandleRequest(string xml).
The request itself is sent in xml, where the xml content specifies which type of action to perform and required/optional parameters as well.
The response from the service is also returned in xml and may be different per each request type that is sent.
I'd like to design a solution that will facilitate interacting with this service, and that will allow me to:
Dynamically generate an xml from given parameters (action type, other optional args, etc). Currently the xml is loaded from a file that was already created
Parse the response in an easy way (creating a strongly typed object from it?)
What's a good solution for doing this? I find it hard to come up with one, as the request/response xml is dynamic and may change from call to call.
Some additional info:
The service is Java based and is hosted under Tomcat (Axis 1.2)
There's no wsdl document for the service (even if there was, i wouldn't be able to automatically generate some strongly typed request/response classes, as the service itself receives and outputs only XML and not some complex type).
That sounds like an XML-RPC implementation could be what you are after. From Wikipedia;
XML-RPC works by sending a HTTP request to a server implementing the
protocol. The client in that case is typically software wanting to
call a single method of a remote system. Multiple input parameters can
be passed to the remote method, one return value is returned. The
parameter types allow nesting of parameters into maps and lists, thus
larger structures can be transported. Therefore XML-RPC can be used to
transport objects or structures both as input and as output
parameters.
Wikipedia also lists some Java Implementations of this protocol.
While, I've not used this specifically, I've worked with a service designed around a bastardised version of JSON-RPC. As it didn't follow the spec truely, we couldn't utilise any pre-existing implementations.
Personally, I didn't see the benefit of using such a protocol as we still needed to have clear definitions of the operations exposed by the service along with their associated constraints such as mandatory parameters etc. In addition to that, we had to handle the serialisation/deserialisation of JSON (XML in your case) to the associated object model. This was largely due to the vendor we were interacting with and their lack of conformance to the spec. If yours is conformant, then you may find that the existing implementations provided might give you a neat way of handling this.
Note the critisims regarding bloat of XML-RPC on Wikipedia too. It might pay to look into JSON-RPC as an alternative. There are certainly a few implementations listed that you can check out.
Edit: I didn't read your question properly. Sorry. I thought you were looking at providing a service. I'd still look at the links around XML-RPC/JSON-RPC as it may give you an idea as to how to knock up a test client. As far as .NET goes, I looked at the Jayrock codebase to get an idea of how the JSON-RPC protocol was implemented and if we could have used that in our scenario. You can get a rough idea as to how they handle the requests and responses. From memory, they may even have a test harness or sample code showing how to call the service. That could give you some ideas.

How to find optimal road path of multiple locations within a map by giving post codes as input?

I've a requirement in which i need to show optimal road route related to a fixed location within a map by giving few postcodes, fetched from database on the basis of some conditions, as input.
The route should be shown as the least distanced postcode first order from the fixed location.
How can i implement using asp.net?
Can i use Google-map api to implement this?
If you want to use Google's database of roads, you have their API: http://code.google.com/apis/maps/documentation/directions/
https://maps.googleapis.com/maps/api/directions/output?parameters
Their API is very well documented, so checking that out would be a great start. The output is available in XML and JSON, and it includes the waypoints which you are looking for.

Accept Data With A Webservice C# .NET 3.5

I was curious as to how I would accomplish the following with webservices:
Authenticate a user.
Accept a CSV or XML file.
Process the file and put it into an SQL database.
Someone mentioned in a previous post that I should use a webservice. I can't seem to find any resources that explain how to begin something like this. All the simple examples seem to just show how you can serve XML given a query.
I want to know how to accept stuff and also, how this would differ from an upload control on an authenticated webpage. I don't think I really understand webservices and their benefits.
How would the user sending the XML file interface with my webservice?
If you want to do large file uploads, then a web service may cause some issues, because some web service platforms (including .NET) have default settings limiting the size of the data.
The advantage of a web service is that it does all the mapping of the request to/from XML, so you can return a .NET type, and don't need to muck around with processing request parameters.
However, you may have to put more effort into maintaining state, etc.
For logins, what you can do is have a login function that returns some kind of identifier which can be used to verify the user as valid for that session - one way of doing this being to have columns in your user table for lastActive and sessionGUID, and when they log in you generate a new sessionGUID and return that, and on that and any other valid request they make you update the lastActive, and if there is a request too long after the lastActive time, then you refuse the request... there's any number of similar ways of doing that, but hopefully you get the general idea - you don't want to require the login details each time, but you can generate a temporary identifier and use that.
For accepting an XML file, you'd want to use something like XDocument or XMLReader to read the data that you receive. Assuming you're not talking about the parsing of the XML format that the web service itself uses, you're most likely to be receiving a string and then pushing that into an XDocument and then using the standard XDocument functions to process the data. If the document would be large, then XMLReader should be more efficient.
For reading a CSV file, there are some (free and non-free) CSV readers which help avoid some of the issues you can have, giving you a nice API for processing a string or strings of CSV data. If you know that the source data doesn't have non-structural commas, though, you can just take the string and split it by commas, and then strip any quotes around the values. That tends to get flaky quite fast if there might be addresses or other data that could have commas in, though.
The XML should be able to be passed via the web service just fine - it should be encoded and decoded, so it's then compliant strings being passed out.
As for storing it in a database, there's any number of ways to do that - you can use ADO.NET to store things in a database without further libraries, you can create a database structure in Visual Studio or SQL Server Management Studio and then use SQLMetal or Linq to SQL to generate classes for saving the data, you can use a 3rd party database mapping tool (such as Castle ActiveRecord), or whatever. It depends what you know and how much you're willing to learn. That's really separate to the web service. When you define a web service in .NET you effectively define standard functions with attributes marking them as web services, so the database side is standard .NET database stuff that's not necessarily any different to what you'd do for an ASP.NET website, or even a desktop program.
A web service is not really appropriate for sending an arbitrary file. It can be done, but if that's your only reason for creating the web service, you might as well just stick to HTTP.
If the file has a specific format or specific contents then you might want to create a web service for that. The purpose of an ASMX or WCF web service is to provide discoverability and strong typing to the data (among other things, but I'm sticking to the basics for the moment). From the perspective of the client, instead of trying to create some ugly XML or CSV blob and chuck it over HTTP, you use an actual service proxy with POCO classes:
MyService service = new MyService();
MyData data = new MyData() { ID = 3, Name = "Test", Date = DateTime.Now };
service.Save(data);
Visual Studio (and equivalent tools in Java and some other platforms) will take care of generating the proxy for you, so really all you have to do is write the above code.
But if you're just trying to send any data, this won't get you anywhere, because you can't generate a proxy for raw XML. Well, you can, but it would just be an XmlDocument and that accomplishes nothing in terms of usability, type safety or discoverability.
Don't get confused by the "XML" in "XML Web Service". It's not a tool for sending around vanilla XML. Rather, XML refers to the format of the message, as it is transmitted over the wire, as opposed to a POST string (id=3&name=Test&date=2010-01-24) or a binary RPC call as used in .NET Remoting.
In terms of authentication, if you do decide to use WCF, you just have to use the right binding. A WCF proxy is normally configured by default to use wsHttpBinding, which uses integrated Windows authentication to secure the messages. Again, assuming you use Visual Studio, this is all done pretty much automatically for you unless you decide to change the defaults.

Categories