Saturday, 26 October 2013

call webservice (.asmx) without adding service reference or making any proxy

WEBMETHOD writen in web service

[WebMethod]
        public int StoreImage(string filename, Byte[] imgbyte)
        {
            try
            {
                System.IO.File.WriteAllBytes(Server.MapPath("\\images\\") + filename, imgbyte);
                return 1;

             }
            catch
            {
                return 0;
            }
        }


Now put the following code in your page.

HTML

<div>
        <asp:FileUpload ID="fileuploader1" runat="server" />
        <asp:Button ID="btnIMage" runat="server" Text="upload"
            onclick="btnIMage_Click" />
            <br />
            <asp:Label ID="idmsg" runat="server"></asp:Label>
            <br />
    </div>

CODE in your page code behind

Add following reference to the page

using System.IO;
using System.Web.Services;
using System.Web.Services.Description;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;

Now write following code:

string filename = fileuploader1.FileName;
            Byte[] imgByte = null;
            if (fileuploader1.HasFile && fileuploader1.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = fileuploader1.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }

            System.Net.WebClient client = new System.Net.WebClient();
            System.IO.Stream stream =
                   client.OpenRead("http://xyz/SaveBannerImages.asmx?wsdl");
            ServiceDescription description = ServiceDescription.Read(stream);
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
            importer.ProtocolName = "Soap12";
            importer.AddServiceDescription(description, null, null);
            importer.Style = ServiceDescriptionImportStyle.Client;
            importer.CodeGenerationOptions =
                     System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
            CodeNamespace nmspace = new CodeNamespace();
            CodeCompileUnit unit1 = new CodeCompileUnit();
            unit1.Namespaces.Add(nmspace);
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);
            if (warning == 0)
            {
                CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");
                string[] assemblyReferences =
                  new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
                CompilerParameters parms = new CompilerParameters(assemblyReferences);
                CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);
                object[] args = new object[2];
                args[0] = filename;
                args[1] = imgByte;
                object wsvcClass = results.CompiledAssembly.CreateInstance("SaveBannerImages");
                MethodInfo mi = wsvcClass.GetType().GetMethod("StoreImage");
                string str = mi.Invoke(wsvcClass, args).ToString();
                if (Convert.ToInt16(str) == 1)
                {
                    idmsg.Text = filename + " successfully uploaded!!!";
                }
                else
                {
                    idmsg.Text = "Oops! Error in uploading " + filename;
                }
              
            }
            else
            {
                idmsg.Text = warning.ToString();
                Console.WriteLine("Warning: " + warning);
            }


Happy coading..................