使用编程WSDL和WSDL解析创建代理 [英] creating proxy using wsdl programmatically and wsdl parsing

查看:126
本文介绍了使用编程WSDL和WSDL解析创建代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的XML Web服务。我的客户端Web Service客户有服务器Web服务的服务的WSDL在运行时的URL。为了让客户使用服务我需要做以下的事情编程:



1)获取动态WSDL文件服务或从磁盘上的位置。
2)创建一个代理编程即不使用Wsdl.exe用或添加Web引用。
3)上调用创建的代理方法。



是否有可能呢?如果有些人做过这将不胜感激采取任何建议,如何做到的。


解决方案

 使用系统; 
使用System.Collections.Generic;
使用System.Text;
使用的System.Reflection;
使用System.CodeDom;
使用System.CodeDom.Compiler;使用System.Security.Permissions
;
使用System.Web.Services.Description;

命名空间ConnectionLib
{
公共类WSProxy
{
[SecurityPermissionAttribute(SecurityAction.Demand,无限制= TRUE)]
公共静态对象CallWebService(字符串webServiceAsmxUrl,弦乐服务名,字符串methodName中,对象[]参数)
{
System.Net.WebClient客户端=新System.Net.WebClient();

//连接到Web服务
的System.IO.Stream流= client.OpenRead(webServiceAsmxUrl +WSDL?);

//现在读描述服务的WSDL文件。
ServiceDescription描述= ServiceDescription.Read(流);

/////加载DOM /////////

//初始化服务描述进口国。

ServiceDescriptionImporter进口商=新ServiceDescriptionImporter();
importer.ProtocolName =SOAP12; //使用SOAP 1.2。
importer.AddServiceDescription(描述,NULL,NULL);

//生成代理客户端。
importer.Style = ServiceDescriptionImportStyle.Client;

//生成的属性来表示的原始值。
importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

//初始化代码DOM树成,我们将导入服务。
的CodeNamespace nmspace =新的CodeNamespace();
CodeCompileUnit 1单元=新CodeCompileUnit();
unit1.Namespaces.Add(nmspace);

//将服务导入到代码DOM树。这将创建一个使用该服务代理代码。
ServiceDescriptionImportWarnings警告= importer.Import(nmspace,1单元);

如果(警告== 0)//如果为零,那么,我们是好去
{

//生成代理代码
CodeDomProvider provider1 = CodeDomProvider.CreateProvider(CSHARP);

//编译与适当的引用
串组装代理[] = assemblyReferences新的字符串[5] {System.dll中,System.Web.Services.dll, System.Web.dll程序,system.xml.dll的,System.Data.dll中};

CompilerParameters PARMS =新CompilerParameters(assemblyReferences);

CompilerResults结果= provider1.CompileAssemblyFromDom(PARMS,1单元);

//检查错误
如果(results.Errors.Count大于0)
{
的foreach(CompilerError哎呀在results.Errors)
{
System.Diagnostics.Debug.WriteLine(========编译器错误============);
System.Diagnostics.Debug.WriteLine(oops.ErrorText);
}
抛出新的System.Exception(编译出错调用webservice的检查调试输出中的窗口。);
}

//最后,调用Web服务方法

对象wsvcClass = results.CompiledAssembly.CreateInstance(服务名);

MethodInfo的MI = wsvcClass.GetType()实现getMethod(方法名)。

返回mi.Invoke(wsvcClass,参数);

}

,否则
{
返回NULL;
}
}
}
}


I am working on XML web services. My client web service "Client" has the url of the wsdl of server web service "Service" at run time. In order for the "Client" to use "Service" i need to do the following thing "programmatically":

1) Get the wsdl file on the fly from "Service" or from a location on the disk. 2) Create a proxy programmatically i.e not using wsdl.exe or Add web reference. 3)Invoke methods on the created proxy.

Is it possible to do it? If some one has done it would be greatful to take any suggestions how to accomplish.

解决方案

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Security.Permissions;
using System.Web.Services.Description;

namespace ConnectionLib
{
    public class WSProxy
    {
        [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
        public static object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args)
        {
            System.Net.WebClient client = new System.Net.WebClient();

            // Connect To the web service
            System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");

            // Now read the WSDL file describing a service.
            ServiceDescription description = ServiceDescription.Read(stream);

            ///// LOAD THE DOM /////////

            // Initialize a service description importer.

            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
            importer.ProtocolName = "Soap12"; // Use SOAP 1.2.
            importer.AddServiceDescription(description, null, null);

            // Generate a proxy client.
            importer.Style = ServiceDescriptionImportStyle.Client;

            // Generate properties to represent primitive values.
            importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

            // Initialize a Code-DOM tree into which we will import the service.
            CodeNamespace nmspace = new CodeNamespace();
            CodeCompileUnit unit1 = new CodeCompileUnit();
            unit1.Namespaces.Add(nmspace);

            // Import the service into the Code-DOM tree. This creates proxy code that uses the service.
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);

            if (warning == 0) // If zero then we are good to go
            {

                // Generate the proxy code
                CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");

                // Compile the assembly proxy with the appropriate references
                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);

                // Check For Errors
                if (results.Errors.Count > 0)
                {
                    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.");
                }

                // Finally, Invoke the web service method

                object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);

                MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);

                return mi.Invoke(wsvcClass, args);

            }

            else
            {
                return null;
            }
        }
    }
}

这篇关于使用编程WSDL和WSDL解析创建代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆