Solved

How to send an XML file/data from a windows service to a web service?

Posted on 2004-09-09
7
235 Views
Last Modified: 2008-02-01
Hi,
 I am setting up a windows service that will send xml data to a web service on a timed interval.  The xml data is a dataset from the sql server.  What is the best way to transport this xml data to the web service?  Will I be using SOAP? Or just use WebMethods?

Lastly, how can I stream the xml data from windows service to the web service securely?  

I am new at this, so any help would be appreciated.  Furthe more, the transport method must be secured.  

Any suggestions, links, info, code,  would help.

Thanks,
 
K
0
Comment
Question by:Khaki
  • 5
  • 2
7 Comments
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
The security issue should be handled by the web service.  It should accept SOAP via the https protocol.  If you have control over the web service (meaning you are programming it), you can set that up or you can write up your own encryption before you send off the XML and then decrypt it in the web service.  But again, that is if you have control over the web service.

By your question about web methods, I am thinking you will be developing the web service.  If this is true, then yes, you would define a web method that would accept your dataset as its argument.  In the consuming application, you set a Web Reference with the URL for the web service and then call its web methods just like calling the methods of any other class.

If you are going to create the web service and need some code samples, post a comment accordingly and I'll try to gin something up for you.

John
0
 

Author Comment

by:Khaki
Comment Utility
Hi John, Thanks for insight.  I think you got were I am going.  I will be writing both the client and the web service, so if you have any sample code, I would greatly appreciated it.  
 One more thing, do you have any sample SOAP working  https links or samples?  I just need something small, then I can get started on my own.

Thanks.

K
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
I don't, but you don't really need that.  ASP.NET abstracts the complexity of SOAP away from the developer quite nicely.  You create the webservice and ASP.NET takes care of creating the SOAP envelope.  On the consuming side, you set w "Web Reference" and ASP.NET creates the SOAP to call the web method and pass the dataset.

I'll gin up a real simple web service and explain how to do the same.  I'll post this (hopefully) by the end of the evening.

John
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Accepted Solution

by:
jnhorst earned 160 total points
Comment Utility
Khaki:

OK:  Start by creating a new project.  Select the ASP.NET Web Service icon in the dialog and name it something like WebServicesTest.  The location of the project should be http://localhost/WebServicesTest.

VS will create a default file named Service1.asmx.  Change the file name to XtremeData.asmx.  Then right click the file and select View Code.  You will see class defined as Service1.  Change it to XtremeData.  If you are using C#, make sure to change the constructor method name to XtremeData as well.  Build the project.

Now go to the Server Explorer in the VS IDE.  There is a toolbar button on the upper right of the IDE that drops down.  You will find the Server Explorer there.  In the server explorer right click on the Database Connections node and select Add Connection.  If the Provider tab is not selected, select it and pick the "Microsoft OLE DB Provider for ODBC Drivers" and click Next.  You will be asked to select the ODBC datasource.  There should be an ODBC data source for the Xtreme database already on your system.  Choose it.  There will also be a dropdown for Initial Catalog.  This should have the file path.  Select it and test the connection.  It should be fine.

This is a database used for Crystal Report samples, but it will suffice for our purposes.  You'll see nodes for Tables and Views.  Expand the Tables node.  Then in the VS menu click Project >> Add New Item.  Select the dataset icon and change the file name to Xtreme.xsd.  You will see a blank yellowish designer.  Drag the Employee table node onto this designer.  You will see a box with the Employee table's columns and their data type.  Technically, in XML this is called an element, and the columns are elements within the Employee element.  But ADO.NET will use this to create a class named Xtreme that inherits from System.Dataset with a DataTable named Employee created from the XML elements defined in the Xtreme.xsd file.  This is the dataset you will populate and return to your consuming app.  Build the app to make sure everything is set up right.

Now make sure the XtremeData.asmx file is showing in design view (should be an empty light bluesh pane).  Open your toolbox and click the Data tab.  Doublic click the Dataset tool.  You'll get a dialog that will have a "Typed Dataset" radio button selected and the Xtreme dataset in a dropdown.  Just click OK.  An icon will appear in the designer with the name xtreme1.  Right click it, show its properties and change its name to dsXtreme.  Save your project.  Again in the Data tab of the toolbox, double click OdbcDataAdapter.  You will get a Data Adapter Configuration Wizard (this is one very cool IDE feature).  Click next.  Unless you have other connections in the Server Explorer, your Access connection will appear in the dropdown.  It it isn't, select it and click next.  You will only be able to use SQL statements so click Next again.  Enter the following:

Select * From Employee

(It is generally bad form to use "Select *" like this, but for demo purposes, it'll do.)  CLick the Advanced Options button and deselect the "Generate Insert, Update and Delete Statements" checkbox and click OK.  Click Next.  You should see "Generated Select Statement" and "Generated Table Mappings" with checks next to them.  Click Finish.  Right click the icon in the designer and change the name to daXtreme.  Now we'll do the web method.

In the code for the XtremeData.asmx file, enter the following (within the class):

[WebMethod]
public WebServicesTest.Xtreme GetEmployees()
{
      this.daXtreme.Fill(this.dsXtreme.Employee);
      return this.dsXtreme;
}

Here you are simply filling the datatable and returning it within the dataset.

Build the project.  Your webservice is basically done.

Now, in the same solution, create a ASP.NET Web Application project and name it WebServiceConsumer.  Right click the project node and set the project as the startup project.  It will create a WebForm1.aspx file.  We'll use that to call to the web service.  But you will first need to set a Web Reference.  In the Web References node in this new project, right click References and select Add Web Reference.  You will get a dialog asking for the URL to your web service.  This will be:

http://localhost/WebServicesTest/XtremeData.asmx.  The dialog will tell you that it found the service and will show you what methods it exposes, which will be GetEmployees().  Change the Web Reference Name to WebServicesTest and click Add Reference.  Bulid your consuming application.  Open the WebForm1.aspx page in design view.  Doublie click the Dataset tool in the Data toolbox tab like before.  It will recognize that the web service exposes the Xtreme dataset.  Leave this selected and click OK.  An icon will appear the bottom of the page in design mode in a tray.  Right click it, show proprties and change the name to dsXtreme.

Now put a datagrid on the page and set the DataSource to dsXtreme and the DataMember to Employee.  Rename the grid to grdXtreme. You'll see all of the columns in the grid, even in design time.  Now in the page's codebehind, in Page_Load, do this:

private void Page_Load(object sender, System.EventArgs e)
{
      // get dataset.
      WebServicesTest.XtremeData ws = new WebServicesTest.XtremeData();
      this.dsXtreme = ws.GetEmployees();

      if (!this.IsPostBack)
      {
            this.grdXtreme.DataBind();
      }
}

Now comes the cheesy part.  When you opened your connection in Server Explorer, Access locked the mdb file so if you run the app now, you'll get an error.  Close down VS and restart it.  This will release the lock.  Start the app.  Your WebForm1.aspx should load and show the data from the Xtreme database in your grid.

This is the basics of creating and consuming a web service.  You can have multiple services in one project.  You'll have to figure out the security part, but if your server supports https, that should do what you need by way of encryption.

John
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
Sorry, there is no "Web References" node.  Just References.  Right click and add a Web Reference.

John
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
Also, if you could bump up the points a little, that would be cool.  But that's up to you.

John
0
 

Author Comment

by:Khaki
Comment Utility
good job. That will get me started.
Thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now