Showing posts with label Web service. Show all posts
Showing posts with label Web service. Show all posts

Friday, August 22, 2014

Consuming web service in console application

Below took me a while to find, so just copy over for future reference:

http://www.c-sharpcorner.com/UploadFile/718fc8/consuming-web-service-in-console-application/


IntroductionHere I am creating a simple web service and using it in a console application. Using a web service in a console application is similar to using a web service in window application. At first, we create a web service. Follow the given steps.
  • Create new project
  • Select ASP.NET Empty Web Application
  • Give name to your project and click ok button
  • Go to Solution Explorer and right click at your project
  • Select Add New Item and select Web Service application
  • Give it name and click ok button 
Now replace the code in the .asmx.cs page with the following code. 
using System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Web;
using
 System.Web.Services;
 

namespace
 myproject
{
    /// <summary>    /// Summary description for MyService    /// </summary>    [WebService(Namespace = "example.abc/")]
    [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 MyService : System.Web.Services.WebService    { 
        [WebMethod(Description="show simple message")]
        public string show()
        {
            return "My Web Service Application";
        }
        [WebMethod(Description = "show datetime")]
        public string showdate()
        {
            return DateTime.Now.ToLongDateString();
        }
        [WebMethod(Description = "addition of two int")]
        public int add(int a,int b)
        {
            return a + b;
        }   
    }
}

Run the application

Output window:

 
Now, create a new project and take Console Application.  Add the service reference. For doing this, follow the given steps.
  • Go to Solution Explorer and right click at your application. 
 
  • Click Add Service Reference. A new window will be open.
 
  • Click the "Advanced" button. A new window will be open.

      
  • Again click the "Add Web Reference" button. Again a new window will be open.
 
  • Copy the URL of your running service application and paste to it at URL( As given in above figure) and click the "Go" button.

   
  • Set the service reference name. The default name is "localhost". Now click the "Add Reference" button.
Now the service has been added in your console application. Write the following code.

using
 System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
namespace MyApplication
{
    class 
Program
    {
        static void Main(string[] args)
        {
            myservicereference.MyService obj = new myservicereference.MyService();
            Console.WriteLine(" Calling Web Methods");
            Console.WriteLine("---------------------");
            Console.WriteLine("\n Calling show Method");
            Console.WriteLine("  "+obj.show());
            Console.WriteLine("\n\n"+" Calling showdate Method ");
            Console.WriteLine("  "+obj.showdate());
            Console.WriteLine("\n Calling add Method ");
            Console.Write("  Enter First Number:");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("  Enter Second Number:");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("\n  Addition is:" + obj.add(a, b).ToString());
            Console.ReadLine();
        }
    }
}

 
Press Ctrl+f5 to run the code.

Output:

 
 Enter first and second number. Then press "enter".

Output:

web service get file and put file

I converted the return of get file to a string.

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

namespace TestWebService
{
   /// <summary>
   /// Summary description for WebService1
   /// </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 WebService1 : System.Web.Services.WebService
   {

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

      [WebMethod]
      public string GetFile(string filename)
     // public byte[] GetFile(string filename)
      {
         BinaryReader binReader = new
  BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open,
  FileAccess.Read));
         binReader.BaseStream.Position = 0;
         byte[] binFile =
  binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
         binReader.Close();
         var str = System.Text.Encoding.Default.GetString(binFile);
         return str;
      }

      [WebMethod]
      public void PutFile(byte[] buffer, string filename)
      {
         BinaryWriter binWriter = new
  BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew,
  FileAccess.ReadWrite));
         binWriter.Write(buffer);
         binWriter.Close();


      }
   }
}

below is code to run the web methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestAccessWebService
{
   class Program
   {
      static void Main(string[] args)
      {


       //  ServiceReference1.RolesServicePortClient cli = new ServiceReference1.RolesServicePortClient();
        // ServiceReference1.Role[] r ;
       
         //r = cli.getGlobalRolesByEmployee("0001445");
        // ServiceReference1.Role r1;
         //r1 = r[0];

         net.azurewebsites.smallgroup.WebService1 obj = new net.azurewebsites.smallgroup.WebService1();

         //myservicereference.WebService1 obj = new myservicereference.WebService1();
         //Console.WriteLine(r1.ToString());
         //Console.WriteLine(r1.OrgEntityID);
         Console.WriteLine("---------------------");
         //Console.WriteLine("\n Calling show Method");
         Console.WriteLine("  " + obj.HelloWorld());
         Console.WriteLine("\n\n" + " Calling GetFile Method ");
         byte[] bytearray = obj.GetFile("storelist.txt");
         //byte[] bytearray = obj.GetFile("StopSQL.bat");
         //Console.WriteLine("---------------------");
         var str = System.Text.Encoding.Default.GetString(bytearray);
         Console.WriteLine(str.ToString());
         Console.WriteLine("---------------------");
         //obj.PutFile(bytearray, "storelist.txt");
         //bytearray = obj.GetFile("storelist.txt");
         Console.WriteLine("---------------------");
         //str = System.Text.Encoding.Default.GetString(bytearray);
         //Console.WriteLine(str.ToString());
      }
   }
}

Tuesday, August 5, 2014

IIS 7 deploy web service

This article hopes to make it clearer on how to publish a web service in an IIS 7 environment.  Its describes creating a new web site to host the web service, as well and adding the web service as an application to an existing web site.
http://krismanohar.com/blog/?p=34


http://www.codeproject.com/Articles/28693/Deploying-ASP-NET-Websites-on-IIS-7-0

http://beyondrelational.com/modules/2/blogs/48/posts/10055/walkthrough-on-creating-wcf-40-service-and-hosting-in-iis-75.aspx