Access Web service in client using WSDL file

The purpose of this article will explore how to access a web service without add Web Reference into the client application.That means using WSDL File and generate proxy class and then finally use within client Application.

Introduction

 Last week i noticed in forums a few developers ask question how to access web service method without add Web References to the client project using WSDL file.

 Actually this is common way most of the CRM web service we need access web service method using only WDSL File, they don't give the Reference to add with our project.

It's pretty easy, lets start our demonstration, to this we need a web service server side and a windows client.First you need create a web service using Visual Studio 2008, look following image to clear figure.




To create web service in Visual studio 2008, just click on File menu then  click new Project, and then select C# language under the C# language you need select ASP.NET web service application finally give a name as  SampleWebService then click OK button to process.

Now we have a web service class file, we need write few lines within that.just take a simple example you need a create a file in server which has client data.

Web Service class File code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace SampleWebservice
{
/// <summary>
/// Summary description for FileCreateServices
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class FileCreateServices : System.Web.Services.WebService
{

[WebMethod]
public bool CreateFile(string[] collectionUsers)
{
bool result = false;
ServerFileManager manager = new ServerFileManager();
manager.CollectionofUsers = collectionUsers;
manager.CreateFile();
return result = true;
}
}
}

ServerFileManager class File Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace SampleWebservice
{
public class ServerFileManager
{

public ServerFileManager()
{

}

string[] collectionofUsers = null;

public string[] CollectionofUsers
{
get { return collectionofUsers; }
set { collectionofUsers = value; }
}
public void CreateFile()
{            
if (collectionofUsers != null)
{
using (FileStream stream=new FileStream(@"c:\UserDetails.txt",FileMode.Append))
{
using (StreamWriter writer=new StreamWriter(stream))
{
foreach (string username in collectionofUsers)
{
writer.WriteLine(username);
writer.Flush();
}
}
}
}
}
}
}

Both class are simple and basic code lines.Again say we are going to create user name list file in the server , which file supply from

client application.But our demonstration is use the WSDL file and access web service method in client Application.

Let's compile web service project and run ,when you will see its look like this way in IE.


But we need WSDL file to access in out side. how you can generate WSDL file from the .asmx file it's pretty easy just type like this

on your IE http://localhost:14057/FileCreateServices.asmx?WSDL then you will see like this xml file structure on your IE.


Fine we have WSDL file structure ,just save in your local directory with extension .wsdl  using IE save as Menu.

Lets start i important step here,We need generate proxy class to access Web service method from using WSDl file, then only you access the web service method in your client application.

Note:if you are add web reference using Visual studio , we don't do those steps because of Visual studio generate proxy class for us.

To generate proxy class we need access WSDL.exe application which come with .NET 2.0. just look following figure to learn how to generate proxy class using wsdl.exe with command prompt.

Let's open command prompt and type following commands in command prompt.

Setting environment for using Microsoft Visual Studio 2008 x86 tools.

d:\Program Files\Microsoft Visual Studio 9.0\VC>cd..

D:\Program Files\Microsoft Visual Studio 9.0>cd..

D:\Program Files>cd "Microsoft Visual Studio 8"

D:\Program Files\Microsoft Visual Studio 8>cd SDK

D:\Program Files\Microsoft Visual Studio 8\SDK>cd v2.0

D:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>cd Bin

D:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>wsdl.exe /n:EggHeadCafeS
ervice /o:FileCreateService.cs D:\FileCreateServices.wsdl
Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'FileCreateService.cs'.

D:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>

Note: I have installed Visual studio on D Drive.

To learn passing parameter to WSDL.exe just visit here http://msdn.microsoft.com/en-us/library/7h3ystb6%28VS.71%29.aspx

Now we have cs file i n our bin directory just copy and paste in your client project. when your add with your client appliction it like be followings.

This is your client application



Now access Web method in our client application.

Before write code with client you need ass one reference to client project access web service methods and property.

Note:Right click on your project then select add reference menu then under teh .NET tab select the System.Web.Services libaray.

Now  write client code like followings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EggHeadCafeService;
namespace WDSLDemo
{
class Program
{
static void Main(string[] args)
{
FileCreateServices services = new FileCreateServices();
services.Url = "http://localhost:14057/FileCreateServices.asmx";
string [] collection=new string[]{"Testor1",
"Testor2",
"Testor3",
"Testor4",
"Testor5",
"Testor5",
"Testor6"
};
bool result =services.CreateFile(collection);
if (result)
{
Console.WriteLine("Done");
}
else
{
Console.WriteLine("failed");
}
Console.ReadLine();
}
}
}


Output: File create in server side and "Done " message come in the Console.


I hope this article will helpful to most of the developers who are need access web service methods using WSDL file without adding Web References .Please make comment about the article.


 



By Ravenet Rasaiyah   Popularity  (22132 Views)
Biography - Ravenet Rasaiyah
My name is RRaveen, and my profession is software developing since 2006 (having more than 4 years experience) and holding MCTS,MCPD certifications and most of developments are based on MS technologies .Specifically ASP.NET ,Sharepoint and Widows Mobiles. I have worked on ERP systems also.