【问题标题】:Consuming a .net 2.0 web-service from a DLL without app.config从没有 app.config 的 DLL 使用 .net 2.0 Web 服务
【发布时间】:2013-04-13 01:36:02
【问题描述】:

使用没有 app.config 的 .net 2.0 Web 服务的一般问题

请原谅我的英语不好:

我已经做了什么:

我编写了一个测试控制台应用程序,该应用程序与存储在 app.config 中的预配置 Web 连接连接,这工作正常:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="XpageServiceSoap">
                    <security mode="Transport" />
                </binding>
                <binding name="XpageServiceSoap1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://www.anydomain.com/XpageService.asmx"
                binding="basicHttpBinding" bindingConfiguration="XpageServiceSoap"
                contract="XsysService.XpageServiceSoap" name="XpageServiceSoap" />
        </client>
    </system.serviceModel>
</configuration>

现在我想构建一个也可以与 COM 一起使用的程序集。尝试调用 DLL 函数会导致错误,因为无法为 DLL 提供 app.config。

我想在我的 DLL 中包含连接信息。所以我尝试了以下方法:

[ServiceContract]
interface IXsysService
{
    [OperationContract]
    string userpswd(string user, string passwort);
    [OperationContract]
    string pagesource(string page, string tokenxml, string ip);
}

并从 main 调用:

EndpointAddress address = new EndpointAddress("https://www.anydomain.com/XpageService.asmx");
WSHttpBinding bind = new WSHttpBinding(SecurityMode.Transport);
ChannelFactory<IXsysService> factory = new ChannelFactory<IXsysService>(bind, address);
IXsysService proxy = factory.CreateChannel();

using (proxy as IDisposable)
{
    Console.WriteLine("Abruf: {0}", proxy.userpswd("user",GetPasswortFromConsole())); // exception!
    Console.ReadLine();
}

代理似乎通过 SSL 成功连接。但是在调用 userpswd() 时,我收到以下异常(从德语翻译):

“System.Web.Services.Protocols.SoapException:如果没有有效的操作参数,则无法处理请求。请指定有效的 SOAPAction 对象。”

当我尝试这个时:

EndpointAddress address = new EndpointAddress("https://www.anydomain.com/XpageService.asmx?wsdl");

我遇到了同样的异常。

也许我的尝试完全错了。

有人对我如何解决这个问题有任何建议吗?我需要一个无需任何 app.config 即可连接到 Web 服务的程序集。

【问题讨论】:

    标签: dll .net-2.0 asmx app-config


    【解决方案1】:

    一个应用程序只使用一个配置文件源。那是应用程序本身的配置文件。在您的情况下,它是控制台应用程序的配置。

    所有设置都必须在该文件中。如果它们来自 DLL 的 app.config,则需要将它们复制到控制台应用程序的 app.config 中。

    这是因为单个库 (DLL) 可能被不同的消费项目(控制台应用程序、Windows 服务、桌面应用程序)使用。这些中的每一个都可能需要不同的设置。只为库设置一组设置,而忽略库的不同用户是行不通的。

    【讨论】:

    • 好的,但最后我需要一个可供 COM(没有 app.config)和 .net 应用程序使用的 COM 程序集。
    • 那你就得在没有设置的情况下生活
    • 这让我们回到了这个问题:所示的通过 SSL 使用服务的方式可以吗?如果是,为什么 userpswd() 的调用不起作用?我没有找到任何关于这个问题的例子——这就是为什么我认为,我的方法是完全错误的。
    猜你喜欢
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多