This is a discussion on WebServices in DotNet within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi Friends, Here I'm starting a thread with web services in dot net and give an article about how ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi Friends, Here I'm starting a thread with web services in dot net and give an article about how to create web service in dot net and how to implement it in to Asp.Net. Learn with my article and share your knowledge.... ![]()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| Sponsored Links |
| |||
| What is WebService? Web Service is a software component for application to application communication. Web services use XML based communicating protocols.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Enterprises Services in Dot Net In Microsoft .Net there are some advanced concepts of enterprises services available, like Web Services and .Net Remoting. ASP.NET Web services and .NET remoting are two separate paradigms for building distributed applications using Internet-friendly protocols and the .NET framework. Web services typically use SOAP for the message format and require that you use IIS for the HTTP message transport. This makes Web services good for communication over the Internet, and for communication between non-Windows systems. Web services are a good choice for message-oriented services that must support a wide range of client platforms and a potentially heavy load. Remoting can be configured to use either SOAP or Microsoft's proprietary binary protocol for communication. The binary protocol yields higher performance, and is great for .NET to .NET communication, but cannot be used to communicate with non-Windows platforms. Remoting does not require an IIS Web server, making it a good choice for peer-to-peer development, but this also means that it cannot leverage the scalability and performance of IIS to support a high number of connections or requests per second.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| WebServices in DotNet Let we start our first web service… Here I’m going to start my first web service with the name of MyFirstService. There is a .cs file in App_Code will be created. This is our class file for writing program. Then a file Service.asmx will be created. This is our web service url. Let me explain the simple web service method that returns an integer value with adding two integer values. Code: using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service ()
{
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int add(int a, int b)
{
return a + b;
}
} Here the HelloWorld function is default function when we create the web service project. The add function which is have two parameters (a & b) and return an integer value. Run the program and give the parameter values for a and b both of 10 and click the Invoke button. After you will get the xml file with the value of 20. Hence we could create the service for adding two integer values. That's it....
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| CacheDuration attribute in Web Service This attribute specifies the length of time in seconds that the method should cache the results. For example, Code: [WebMethod(CacheDuration=30)] //Here 30 is seconds
public int add(int a, int b)
{
return a + b;
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Description in WebMethod We can add description in our web service’s WebMethod as follows, Code: [WebMethod(CacheDuration=30,Description="Returns added value.")]
public int add(int a, int b)
{
return a + b;
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| How to use webService in web application using C# Friends, From this post I'm going to explain how can we use web services in our web application. Yes..its a main thing how to use webservice in my application. Its very easy thing to use web service in our application by using AddWebReference. In the solution explorer we can find AddWebReference when we right click that solution explorer. Click that… There is 3 options for adding our web reference. • Web Services in this solution Finding web service in your solution. • Web services on the local machine Finding web service in local machine or localhost. • Browse UDDI Servers on the local networkGive your webservice url in that url column. And click Go…and give the web reference name. Then click the Add Reference button. Now see your solution explorer, there will be some new files with extensions .disco and .wsdl. Then put the namespace in your .cs file as, Code: using myService; Code: Service service = new Service(); int result = service.add(10, 10); That’s it… ![]() :
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hae Geeks, WSDL is the Web Service Description Language, and it is implemented as a specific XML vocabulary. While it’s very much more complex than what can be described here, there are two important aspects to WSDL with which you should be aware. First, WSDL provides instructions to consumers of Web Services to describe the layout and contents of the SOAP packets the Web Service intends to issue. It’s an interface description document, of sorts. And second, it isn’t intended that you read and interpret the WSDL. Rather, WSDL should be processed by machine, typically to generate proxy source code (.NET) or create dynamic proxies on the fly (the SOAP Toolkit or Web Service Behavior).
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| Hi geek, This post shall describe an approach that may be used to upload any sort of a file through a web service from a Windows Forms application. The approach demonstrated does not rely on the ASP.NET file uploader control and allows the developer the opportunity to upload files programmatically and without user intervention. Such an approach may be useful for doing something like processing out the contents of a local message queue when internet service is available (if the user base were mobile and had only intermittent connectivity). The post also addresses the use of a file size check as a precursor to allowing a file to upload through the service. ![]() Figure 1: Test Application Shown Uploading a File. ![]() Figure 2: Mixed bag of different file types in transient storage folder. Getting Started The solution contains two projects; one is an ASP.NET Web Service project (Uploader) and the other is a Win Forms test application (TestUploader) used to demonstrate uploading files through the web method provided in the web service project. The web service project contains only a single web service (FileUploader) which in turn contains only a single Web Method (UploadFile). The Win Forms application contains only a single form which contains the controls (one textbox and two buttons used in conjunction with an OpenFileDialog control) and code necessary to select and upload files through the web service. ![]() Figure 3: Solution Explorer with the both Projects Visible.
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily Last edited by krishnakumar : 09-01-2007 at 01:08 AM. |
| |||
| Uploading files in Web Service Hi there, We can upload file easily in web service...Just create a method in your web service with the parameter of filename and byte array of that file. In that method you can write the file in your path using stream as follows, Sample code for upload a file in Web Service using C#. Code: [WebMethod]
public string Upload(string filePath, byte[] file)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.Create,
FileAccess.ReadWrite);
fs.Write(file, 0, file.Length);
fs.Close();
return "OK";
}
catch (Exception ex)
{
return ex.Message;
}
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hey Ramesh, We can upload any type of file with using the upload method which is I posted in last post. For that you have to send the image file as a byte array. You can use the following code for converting the image in to byte[] in C#. Here I'm using the UploadFile control for browse the image file. Code: using System.IO; Stream imgStream = UploadFile.PostedFile.InputStream; int imgLen = UploadFile.PostedFile.ContentLength; string imgContentType = UploadFile.PostedFile.ContentType; byte[] imgBinaryData = new byte[imgLen]; int n = imgStream.Read(imgBinaryData, 0, imgLen); ![]()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hi all, Here i am giving one sample code for file upload in webservice using C# Code: Uploader Web Service Project The Uploader web service project is an ASP.NET web service project containing a single web service called, "FileUploader"; this web service exposes a single web method called, "UploadFile". The code for this web service begins with the following: Code: using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;
namespace Uploader
{
/// <summary>
/// This web method will provide an web method to load any
/// file onto the server; the UploadFile web method
/// will accept the report and store it in the local file system.
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class FileUploader : System.Web.Services.WebService
{ The class starts out with the default imports; I added System.IO to the defaults to support the use of file and memory streams. The web service namespace is left as the default http://tempuri.org which of course will have to updated if the service were deployed. The remainder of the code supplied in this class is used to define the web method used to upload the file; the code is annotated. The essential process is that, files converted to byte arrays are passed along with the full name of the file (not the path) including the extention as arguments to the UploadFile web method. The byte array is passed to a memory stream, and a file stream is opened pointing to a newly created file (named the name of the original file) within the target folder used to store the files. Once the file stream has been created, the memory stream is written into the file stream and then the memory stream and file stream are disposed of. The web method is setup to return a string; if all goes well, the string returned will read, "OK", if not, the error message encountered will be returned to the caller. Code: [WebMethod]
public string UploadFile(byte[] f, string fileName)
{
// the byte array argument contains the content of the file
// the string argument contains the name and extension
// of the file passed in the byte array
try
{
// instance a memory stream and pass the
// byte array to its constructor
MemoryStream ms = new MemoryStream(f);
// instance a filestream pointing to the
// storage folder, use the original file name
// to name the resulting file
FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
("~/TransientStorage/") +fileName, FileMode.Create);
// write the memory stream containing the original
// file as a byte array to the filestream
ms.WriteTo(fs);
// clean up
ms.Close();
fs.Close();
fs.Dispose();
// return OK if we made it this far
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
} Code: Test Uploader Win Forms Application The test application contains a single Windows Form class; this form contains a text box used to display the name of the file selected for upload, a browse button used to launch an open file dialog box which is used to navigate to and select a file for upload, and an upload button which is used to pass the file to web service so that the selected file may be stored on the server. The code for this class begins with the following: Code: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TestUploader
{
/// <summary>
/// A test form used to upload a file from a windows application using
/// the Uploader Web Service
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// do nothing
} The next bit of code in the class is private method used to prepare the file for submittal to the web service and to actually make that submittal. The code below is annotated to describe the activity but the essential parts of the operation are to check the file size to see if the web service will accept the file (by default, the web server will accept uploads smaller than 4 MB in size, the web config file must be updated in order to support larger uploads), and to convert the file to a byte array. When everything is ready, the byte array and the name of the file including the extension is passed to an instance of the web service web method. Note that, when setting up the demo, you will have remove and add the web reference back into the project in order for it to work for you. Code: /// <summary>
/// Upload any file to the web service; this function may be
/// used in any application where it is necessary to upload
/// a file through a web service
/// </summary>
/// <param name="filename">Pass the file path to upload</param>
private void UploadFile(string filename)
{
try
{
// get the exact file name from the path
String strFile = System.IO.Path.GetFileName(filename);
// create an instance fo the web service
TestUploader.Uploader.FileUploader srv = new
TestUploader.Uploader.FileUploader();
// get the file information form the selected file
FileInfo fInfo = new FileInfo(filename);
// get the length of the file to see if it is possible
// to upload it (with the standard 4 MB limit)
long numBytes = fInfo.Length;
double dLen = Convert.ToDouble(fInfo.Length / 1000000);
// Default limit of 4 MB on web server
// have to change the web.config to if
// you want to allow larger uploads
if (dLen < 4)
{
// set up a file stream and binary reader for the
// selected file
FileStream fStream = new FileStream(filename,
FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
// convert the file to a byte array
byte[] data = br.ReadBytes((int)numBytes);
br.Close();
// pass the byte array (file) and file name to the web service
string sTmp = srv.UploadFile(data, strFile);
fStream.Close();
fStream.Dispose();
// this will always say OK unless an error occurs,
// if an error occurs, the service returns the error message
MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
}
else
{
// Display message if the file was too large to upload
MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
}
}
catch (Exception ex)
{
// display an error message to the user
MessageBox.Show(ex.Message.ToString(), "Upload Error");
}
} Following the UploadFile method, the next bit of code is used to handle the browse button's click event. This code is used merely to display an open file dialog to the user and to take the file selected through that dialog and display the file name in the form's file name text box. Code: /// <summary>
/// Allow the user to browse for a file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Open File";
openFileDialog1.Filter = "All Files|*.*";
openFileDialog1.FileName = "";
try
{
openFileDialog1.InitialDirectory = "C:\\Temp";
}
catch
{
// skip it
}
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName == "")
return;
else
txtFileName.Text = openFileDialog1.FileName;
} The class wraps up with the button click event handler for the Upload button. This handler merely checks for text in the file name text box and, if something is there, it sends the value to the Upload method. Code: /// <summary>
/// If the user has selected a file, send it to the upload method,
/// the upload method will convert the file to a byte array and
/// send it through the web service
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpload_Click(object sender, EventArgs e)
{
if (txtFileName.Text != string.Empty)
UploadFile(txtFileName.Text);
else
MessageBox.Show("You must select a file first.", "No File Selected");
} That wraps up all of the client and server side code necessary to upload any sort of file to a server from a Win Forms application.
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| Dear Raja, After buying something over the Internet, you may have wondered about the delivery status. Calling the delivery company consumes your time, and it's also not a value-added activity for the delivery company. To eliminate this scenario the delivery company needs to expose the delivery information without compromising its security. Enterprise security architecture can be very sophisticated. What if we can just use port 80 (the Web server port) and expose the information through the Web server? Still, we have to build a whole new Web application to extract data from the core business applications. This will cost the delivery company money. All the company wants is to expose the delivery status and concentrate on its core business. This is where Web Services come in. Web Services basically uses Hypertext Transfer Protocol (HTTP) and SOAP to make business data available on the Web. It exposes the business objects (COM objects, Java Beans, etc.) to SOAP calls over HTTP and executes remote function calls. The Web Service consumers are able to invoke method calls on remote objects by using SOAP and HTTP over the Web. Here is a figure attached for illustrate webservices raja....
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! Last edited by S.Vinothkumar : 09-03-2007 at 12:30 AM. |
| |||
| Hi, You can use HTTPWebRequest and HTTPWebResponse method for that. Using the POST method we can write(upload) file in to ur http path easily... for more just read about HTTPWebRequest and HTTPWebResponse in DotNet
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |