You are currently browsing the category archive for the ‘Microsoft’ category.

This article is in continuation of the previous one in which we have created a simple dot net web service.  https://mirnauman.wordpress.com/2012/08/08/creating-a-simple-dot-net-web-service/

First of all click on Build and than on Publish.

Publishing Our Web Service

On the next screen provide setting for the publishing. Like enter a publish profile name. Keep publish method to File System. Specify a Target Location where the published files will be saved.

Publishing Settings

 

Now open Internet Information Services Manager. We can do this by typing inetmgr in RUN dialog or Administrative Tools from Control Pannel. When the IIS Manager is open. Right Click the server name and click add Website.

IIS Configuration for hosting our web service. Add a new site to the application pool.

This will open the add website dialog. Enter a site name, application pool name, provide the Physical path of the published files of our web service and provide a port number like 8082 etc.

Add website settings

Now check the application pool.

Website created in IIS application pool

Right click on the website in IIS Manager and click Browse Website. you will see your web service up and running.

Note:- this tutorial can  be used to publish any website or web service. The steps are the same

Motive

The reason for writing this article is that a lot of people have published too many articles on creating web services and making it simple but non is actually simple.  Almost all of those article focus on the boring theory and functionality  of the web service and not on actually what we least require to create a simple web service. Beginners to web services like me get confused in the functionality and un necessary theory. With this confusion the actual cause of creating a very simple web service dies. I have made it very simple to create a .net web service and my readers can extend this article like instead of addition they can put in what ever functionality they want in the WebMethod to get this web service work for them according to their needs.

Some Boring Theory That We Can’t Get Rid Off

  • When ever we create a web service we need some part of it to be accessed from outside. So that we can call it and make it do something for us. to make a method accessible to the outside world we need to add [WebMethod] Attribute on top of that method.
  • The method that we need to expose to the outside world must be public.
  • The file that we need to call is the one with “.asmx” extension to execute our web service.
  • We can have a private method in our web service that can be called from with in our public method. Like we can have some classes that will have some method that will do some stuff for us. To get access to that functionality of those method. We need to call those methods from our public method by creating objects of those classes in the Service1.asmx file and calling those methods through those objects.

Now Coming To The Real Stuff.

My development environment

  • Visual Studio 2010
  • Windows 7 professional 64 bit
  • Dot net framework 3.5

Creating the Web Service

Open your Visual Studio and click New Project (Section 1 in image).  From the left side panel choose C#(Section 2 in image) , Web(Section 3 in image). From the Framework selection dropdown at the top select .Net Framework 3.5 (Section 4 in image) and from the list below that select ASP.Net Web Service Application (Section 5 in image). Give the name of the web service application (Section 6 in image)  and the location (Section 7 in image) where it will be saved.  Click OK  (Section 8 in image)to create the project.

Creating a simple .net 3.5 web service application


namespace WsMirTestWebService
{
 /// <summary>
 /// Summary description for Service1
 /// </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 Service1 : System.Web.Services.WebService
 {
 [WebMethod]
 public string HelloWorld()
 {
  return "Hello World";
 }
 }
}

This is what is created for us by default. now replace the method

[WebMethod]
public string HelloWorld()
 {
  return "Hello World";
 }

with your own method that you want to call from outside. Lets say we want to send two numbers to the web service and get the sum

[WebMethod]
 public int add(int a,int b)
 {
  int c = 0;
  c = a+b;
  return c;
 }

Testing Created Web Service

Now when we run our web service, we will see Service1.asmx in brower with all our WebMethods. Currently we have only one method i.e, add.

Running our web service. Service1.asmx is displayed in browser with our WebMethod “add” on display

Clicking on the add link will take us to the second screen where we will supply two values to our WebMethod and will click the Invoke button to execute the add method.

WebMethod add executed. The method expects two values, a and b.

We provide two values 7,3 and click Invoke button. This will execute our WebMethod add and if our method is returning some value it will show us the returned value.

Returned Value of our WebMethod add.

 

https://mirnauman.wordpress.com/2012/08/15/publishing-a-dot-net-web-service-or-website/

The above link can be used to publish the created web service.

Error = The web server is configured to not list the content of this directory

I assume the environment to be  Windows 7 Professional, IIS 7. something. This error is caused when the directory browsing feature is disabled and or the default page is not properly set.  To sort out this issue go to IIS Manager. Open Directory Browsing.

Directory Browsing in IIS 7

Now click Enable from the options available on the right side. Restart IIS. Right Click on the App in the Default Websites tree. Click Manage Application , than Browse and Wala. The site will automatically start working. If there is still problems, check the default pages. if you have a default page other than the pages listed in the default page list. Add ur default page and check again. Site will be working.

I encountered this error while i was creating charts in asp.net using C#. The charts used to work fine in visual studio but when i published the website and tried to access the site through localhost (http://localhost/mysite/charts.aspx) it gave me this error. I’ll cut the crap and come straight to the point. The error was caused because ASP.NET machine user have no write permissions where my charts control wanted to create images.  I am using windows xp. I was looking for a way to give write permission to asp.net machine user on the charts folder in my website but there were no such options available in xp. To enable these option go to “tools” than to “folder options”  than click on the “view” tab. Now scroll down and uncheck “use simple file sharing”.

Uncheck "Use simple file sharing"

After this right click on ur charts folder where the images are supposed to be saved. Click on properties. Click on security tab. Add ASP.NET machine account user and give full rights.

Add ASP.NET mahine account user and give full rights

Click ok and check your published website charts. It will work fine. The next problem that I encountered was when I was trying to publish the same website on our Server machine running Windows Server 2003 to make it accessable through live ip. I repeated the above process and gave “ASP.NET machine account” user full rights but still the charts didn’t worked. The problem was solved by giving “Users” account write permission in addition to the ASP.NET machine user account on the charts folder on the server machine.

Sending emails through ASP.Net2.0  is very easy. Just a few lines of code. But when u get an error you will be surprised.  So before you write up the few lines of code you have to do the following.

Configure IIS for sending emails. Its simple not a big deal. Check if SMPT is already installed. If not installed than go to Add Remove Windows Components and double click IIS. Click SMTP. Install it and restart your system.  Now your local web server is ready for sending emails. The next thing is the coding part.

Add this line of code to the top of the page.

using System.Net.Mail;

To send an email you need the following parts of an email.

From (address who is sending the email)

To (to whom the emails is being sent)

Subject  (subject of the email)

Body (body text of the email)

Write the following code


string strFrom, strTo, strSubject, strBody;

strFrom = "sender@gmail.com";

strTo = "rece<a href="mailto:receiver@gmail.com">iver@gmail.com</a>";

strSubject = "Test Email";

strBody = "You have a test email.";

MailMessage eProEmail = new MailMessage(strFrom, strTo, strSubject, strBody);

SmtpClient emailClient = new SmtpClient("localhost");

emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

emailClient.Send(eProEmail);

emailClient.Timeout = 500;

Run the code and check your email. I hope you will not encounter those errors saying “Mailbox not found” etc etc.

Before I can proceed with this tutorial I assume that the reader has already developed a report in crystal and has loaded it in crystal report viewer control. Running the website and the report page prompts the user with something like this.

Crystalreports Login Prompt

Crystalreports Login Prompt

Now we don’t want to give login information every time we try to run this report.  Its annoying . so what we need to do is to do something with the help of which we can get our report perfectly without this stupid prompt. To solve this problem we will provide the login information programmatically so that our report page don’t prompt us for it. To solve the problem I have done the following. Add the following code to the top of the report page.


using CrystalDecisions;

using CrystalDecisions.CrystalReports;

usingCrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

Now in the Page_Load()  event write the following code. Or we can write this code in any other function and call it when we want to provide the login information.

 

Now in the Page_Load()  event write the following code. Or we can write this code in any other function and call it when we want to provide the login information.


ReportDocument rpt = new ReportDocument();

rpt.Load(Server.MapPath("myReport.rpt"));

rpt.SetDatabaseLogon("myUserLogin", "mypassword", @"Server", "Database");

CrystalReportViewer1.ReportSource = rpt;

Save the page and run the report. This time it will not prompt for the stupid logon information. If some one wants a very basic article on how to create a report and how to call it in asp.net. plz leave your comments . I will write a basic tutorial for creating a crystal report and calling it from asp.net.

Note:- Plz  leave ur comments if this article was helpful.

Thanks.

 

Before I start I would like to tell all that I am using  visual studio 2005, dotnet framework 2.0 , windows xp sp 2 and IIS 5.1 with sqlserver 2008. I developed a web application and I wanted to make it available online on our server. The first thing that I did was to publish the web site, the path for the published files was c:\eprocure.  That went pretty well no error, and visual studio message was “published successfully”. The next thing was to make the published website accessible through the local server.  I created a virtual folder named “epro” for my published web application.  Than I tried to access the login.aspx page as  http://localhost/epro/login.aspx  and BANG the following error came out of no where

A name was started with an invalid character. Error processing resource ‘http://localhost/epro/&#8217;. Line 1, Posi…

<%@ Page Language=”C#” MasterPageFile=”~/Master.master” AutoEventWireup…

I tried a lot of tweaking , messed up a lot of thing but nothing worked. Finally the problem was solved as follows Type “cmd” in RUN. Go the following location.

D:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\

Look for a file

aspnet_regiis.exe.

When multiple versions of the .NET Framework are executing side-by-side on a single computer, the ASP.NET ISAPI version mapped to an ASP.NET application determines which version of the common language runtime (CLR) is used for the application. The ASP.NET IIS Registration Tool (Aspnet_regiis.exe) allows an administrator or installation program to easily update the script maps for an ASP.NET application to point to the ASP.NET ISAPI version that is associated with the tool. The tool can also be used to display the status of all installed versions of ASP. NET, register the ASP.NET version that is coupled with the tool, create client-script directories, and perform other configuration operations.

Execute the following command

 aspnet_regiis.exe-i.

this command installs the version of ASP.NET that is associated with Aspnet_regiis.exe and updates the script maps at the IIS metabase root and below. Only the script maps for applications that use an earlier version of ASP.NET are updated. Applications that use a later version are not affected.

Than execute the following command

aspnet_regiis.exe-e

this command removes the client-side scripts for ASP.NET from the aspnet_client subdirectory of each IIS site directory. Only the client-side scripts for the ASP.NET version associated with Aspnet_regiis.exe are removed.

Restart the system and check the website as http://localhost/epro/login.aspx and  the site is functional.

Please leave your comments if this helps.

Blog Stats

  • 328,022 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 233 other followers

%d bloggers like this: