0

I have a WCF Service and i want to invoke this at run time ...

if i do the same code and instead of WCF(.svc) if i call (.asmx) i can able to get through and getting results... but here WCF gets failed

 private void button1_Click(object sender, EventArgs e)
    {
        string WebserviceUrl = "http://localhost:90/service1/service1.svc";
        string serviceName = Service1;
        string methodName = GetData;
        string[] arArguments = new string[2];
        arArguments[0] = 2 ;
        string sData =CallWebService(WebserviceUrl, serviceName, methodName, arArguments);
        if (!string.IsNullOrEmpty(sData))
            MessageBox.Show(sData + "\tData Available");
        else
            MessageBox.Show(sData + "\tData not Available");
    }
    [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
    private string CallWebService(string WebserviceUrl, string serviceName, string methodName, string[] arArguments)
    {
        System.Net.WebClient client = new System.Net.WebClient();
        System.IO.Stream stream = client.OpenRead(WebserviceUrl + "?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) // If Successfull
        {
            CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");
            string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" };
            CompilerParameters parms = new CompilerParameters(assemblyReferences);
            CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);
            if (results.Errors.Count > 0) // Error Part
            {
                foreach (CompilerError oops in results.Errors)
                {
                    System.Diagnostics.Debug.WriteLine("========Compiler error============");
                    System.Diagnostics.Debug.WriteLine(oops.ErrorText);

                }
                throw new System.Exception("Compile Error Occured calling webservice. Check Debug ouput window.");
            }
            object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
            MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);
            return mi.Name;
        }
        else
        {
            return null;
        }
    }

how to achieve this .....

Aravind
  • 1,521
  • 2
  • 12
  • 23
  • What is the error you are getting – BlackICE Jul 09 '13 at 05:10
  • if (warning == 0) here warning comes as NoCodeGenerated ,... !!! but in case of .Asmx service it works fine ..!!!! – Aravind Jul 09 '13 at 05:19
  • What you are doing is ok for .asmx services but not for WCF services. Like ServiceDescriptionImporter works for .asmx (legacy web services). ServiceContractGenerator works with WCF services (.svc). Similarly, the ServiceDescription Class use have used in part of System.Web.Services dll. While, for WCF services, this class is defined in System.ServiceModel dll. – vibhu Jul 09 '13 at 19:52
  • check this post - http://stackoverflow.com/questions/7360975/dynamically-invoke-wcf-service-from-its-uri-http-ipport-service1-svcwsdl – vibhu Jul 09 '13 at 20:00

0 Answers0