views:

459

answers:

2

I was curious as to how I would accomplish the following with webservices:

  1. Authenticate a user.
  2. Accept a CSV or XML file.
  3. 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?

+2  A: 

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.

David Burton
+1 Lots of good info. I'm really just unclear on the Webservice portion of it all. How does the end user with the data initiate a data push? Do they visit a page in a browser? Do it programmatically? Both? Either? And do you know of a good resource for constructing a webservice like this that might walk a newbie like me through it?
Blankasaurus
A web service would generally get called by a program. Most frameworks have a means for creating web services, where you enter the URL for the web service descriptor, and then just call functions in the standard way for that programming language. For example, in .NET, you'd add a Web Reference, enter the URL of the web service, and then just standard C#/VB.NET function calls, and what you'd see from your code would be .NET types - the web service implementation handles the glue and type serialisation and so on.A web service would not normally be accessed from a browser.
David Burton
In Visual Studio you have an ASP.NET Web Service Application project type. Create one, and it'll generate a web service with a HelloWorld function. Run that, check the .asmx URL you get, and create a new project in another instance of Visual Studio. Add a Service Reference, entering the .asmx URL, and you can then write code like: var svc1 = new ServiceReference1.Service1SoapClient(); MessageBox.Show(svc1.HelloWorld());Obviously those are using the default function and names. Then start hacking...
David Burton
+1  A: 

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.

Aaronaught
OK, that makes more sense. The purpose of this is for me to tell someone, I have a webservice, this is how you access it and send data. Then the ball is in their court to get data to me. In terms of authentication I would like to use the sites authentication which is the .NET SQL flavor.
Blankasaurus
Would a typical scenario be like this? I make a webservice and run it on my host machine. When a customer wants to send me data they...run a program that makes a request and sends data to my webservice for each new item they are adding(the program would loop through the items)? Then my webservice would have some type of handler for when it received an item that would put it into my SQL db?
Blankasaurus
@Casoninabox: Messages sent to/from a webservice should be atomic. That is, one message defines *all* the data that is relevant to a particular request or transaction, as opposed to invoking the service several times in a loop. Other than that - yes, as long as the data being sent conforms to a constant and specific format, this would be a typical scenario. In addition, the nice thing about a WCF service is that you don't really *have* to tell them how to send data, you just need to tell them where the service is (URL) and they can generate a proxy.
Aaronaught
Well, that clears a lot of stuff up for me. Thanks.
Blankasaurus