实例化asmx web服务的对象时出现无效操作异常

本文关键字:无效 操作 异常 对象 asmx web 服务 实例化 | 更新日期: 2023-09-27 18:25:19

我正在尝试将.asmx web服务合并到我的应用程序中。

为此,我1.创建了一个类库项目。2.添加了具有给定wsdl的服务引用3.一个应用程序。配置已自动创建。作为的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="UploadWebServiceSoap" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
        <customBinding>
            <binding name="UploadWebServiceSoap12">
                <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                    messageVersion="Soap12" writeEncoding="utf-8">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                </textMessageEncoding>
                <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                    maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                    bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                    keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                    realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                    useDefaultWebProxy="true" />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:xxxx/xxx/UploadWebService.asmx"
            binding="basicHttpBinding" bindingConfiguration="UploadWebServiceSoap"
            contract="ServiceReference1.UploadWebServiceSoap" name="UploadWebServiceSoap" />
        <endpoint address="http://localhost:xxxx/xxx/UploadWebService.asmx"
            binding="customBinding" bindingConfiguration="UploadWebServiceSoap12"
            contract="ServiceReference1.UploadWebServiceSoap" name="UploadWebServiceSoap12" />
    </client>
</system.serviceModel>

设置完成后,我尝试在函数中调用web服务中的方法。

  UploadWebServiceSoapClient dpcClient = new UploadWebServiceSoapClient();

这是一个错误

Could not find default endpoint element that references contract 'ServiceReference1.UploadWebServiceSoap' in the ServiceModel client configuration     section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

谁能告诉我为什么我会犯这个错误吗。此外,我理解绑定创建了basicHttpBinding,但它是什么,并创建了相同的端点

实例化asmx web服务的对象时出现无效操作异常

看起来好像您已经多次创建服务引用。首先创建ServiceReference1,然后创建DPCServiceReference。您应该更改端点配置:

contract="ServiceReference1.UploadWebServiceSoap"

contract="DPCServiceReference.UploadWebServiceSoap"

您有2行出现此错误。

如果这没有帮助,请尝试使用Soap12删除customBinding和endpoint。