Access web service WSDL file in dynamically in client side using C#

No.of Views5558
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : Web Service , How to
Access web service WSDL file in dynamically in client side using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 


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.

WDSLImg01




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

{codecitation class="brush: csharp; gutter: true;" width="650px"}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace SampleWebservice
{
///
/// Summary description for FileCreateServices
///
[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;
}
}
}

{/codecitation}

ServerFileManager.cs

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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();
}
}
}
}
}
}
}

{/codecitation}

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.

WDSLImg02

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.

Image Loading..

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.

{codecitation class="brush: plain; gutter: true;" width="650px"}

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>

{/codecitation}

Note: I have installed Visual studio on D Drive.To learn passing parameter to WSDL.exe just visit here

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

Image Loading..

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.


lets write client code in within the client application this code look like followings

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGainService;
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();
}
}
}
{/codecitation}

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

Conclusion


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.

Sample code

Thank you
RRaveen

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
    Another article of our series that talks about accessing URL shortening services programmatically. This article is talking about is.gd shortening service, how you can use it, and how to access it via your C#/VB.NET application.
    Published Date : 30/Aug/2010
    This is the first article of our series that talks about accessing URL shortening services programmatically. Here we introduce new concepts like the REST API. We also have a brief discussion of URL shortening services APIs and how you can access them. In addition, we are going to talk about .NET support for the REST API and tools and techniques available that would help us during our journey through the API. A working example built using C# and WinForms is available at the end of this a
    Published Date : 29/Aug/2010
    10 Rules to justify a true Web service
    Published Date : 16/Feb/2010
    Flex Communication with ASP.NET WebService
    Published Date : 16/Feb/2010
    Using the OneWay Web Service Attribute
    Published Date : 15/Feb/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top