简体   繁体   English

我可以创建动态 web 服务客户端引用而不将其引用添加到项目的服务引用中吗?

[英]Can I create dynamic web service client reference without adding its reference to service references of the project?

I need to create dynamic references of multiple web services and sent some value with it.我需要创建多个 web 服务的动态引用并发送一些价值。

To be truly dynamic, you have to do three things:要真正充满活力,您必须做三件事:

1) Get the service description (wsdl) from the web service 2) Generate the proxy code dynamically from the service description 3) Compile the code and expose it in your application - usually through reflection or some sort of dynamic scripting interface. 1) 从 web 服务获取服务描述 (wsdl) 2) 从服务描述动态生成代理代码 3) 编译代码并将其公开在您的应用程序中 - 通常通过反射或某种动态脚本接口。

The code snippet below is from some experimenting I did a long time ago.下面的代码片段来自我很久以前做的一些实验。 It is not production code and won't compile, but should give you a head start if this is the direction you want to go.它不是生产代码,不会编译,但如果这是您想要 go 的方向,那么应该给您一个良好的开端。

It does not include step (3).它不包括步骤(3)。 The code generated can be compiled with classes provided in the System.CodeDom.Compiler namespace.生成的代码可以使用 System.CodeDom.Compiler 命名空间中提供的类进行编译。

Uri uri = new Uri(_Url + "?wsdl");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AllowAutoRedirect = true;
request.PreAuthenticate = false;
if (_User.Length > 0)
{
    request.UseDefaultCredentials = false;
    request.Credentials = new NetworkCredential(_User, _Password, _Domain);
}
WebResponse response = null;

try
{
    response = request.GetResponse();
}
catch (System.Net.WebException wex)
{
    response = wex.Response;
}
catch (Exception ex)
{
}

Stream requestStream = response.GetResponseStream();
ServiceDescription sd = ServiceDescription.Read(requestStream);
_ReferenceName = _Namespace + "." + sd.Services[0].Name;
ServiceDescriptionImporter Importer = new ServiceDescriptionImporter();
Importer.AddServiceDescription(sd, string.Empty, string.Empty);
Importer.ProtocolName = "Soap12";
Importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;

CodeNamespace nameSpace = new CodeNamespace(_Namespace);
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(nameSpace);

ServiceDescriptionImportWarnings warnings = Importer.Import(nameSpace, ccu);
if (warnings == 0)
{
    StringWriter sw = new StringWriter(System.Globalization.CultureInfo.CurrentCulture);
    Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
    CodeGeneratorOptions options = new CodeGeneratorOptions();
    options.BlankLinesBetweenMembers = false;
    options.BracingStyle = "C";

    prov.GenerateCodeFromNamespace(nameSpace, sw, options);

    _ProxySource = sw.ToString();
    sw.Close();
}

I don't have the code right now but I know this can be done, in fact in a former company I have worked we did have a generic web service client developed in house.我现在没有代码,但我知道这可以做到,事实上,在我工作过的一家前公司,我们确实有一个通用的 web 服务客户端在内部开发。

have a lock here:在这里有一个锁:

Generic Web Service Proxy 通用 Web 服务代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM