尝试使C#客户端与localhost上的Web服务进行通信

时间:2012-08-08 14:38:22

标签: c# visual-studio-2010 web-services wsdl

我有一个未记录的Web服务,我需要使用Visual C#编写的客户端获取此Web服务的版本。问题是我太多了菜鸟。

我在Visual Studio中添加了服务引用,因此我获得了一个代理文件和output.config文件。

我从VS获取此行以启动该类的新实例:

DentalScannerServiceClient client = new DentalScannerServiceClient();

所以我把它放在我的控制台应用程序中:

DentalScannerServiceClient client = new DentalScannerServiceClient();
client.GetSoftwareVersion();

获取错误“方法没有重载'GetSoftwareVersion'需要0个参数”。 当我开始输入client.GetSoftwareVersion:

时,I​​ntelisens告诉我这一点

Status DentalScannerServiceClient.GetSoftwareVersion( out string version

所以我试试这段代码:

    DentalScannerServiceClient client = new DentalScannerServiceClient();
    string oo;
    client.GetSoftwareVersion(out oo);

然后打印字符串但是当我运行代码时出现此错误:

“InvalidOperationException未处理” “无法在ServiceModel客户端配置部分找到引用合同'IDentalScannerService'的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。 / EM>“

任何想法如何解决这个或从哪里开始寻找?我感谢任何帮助。也许这很简单。我对C#的经验也很少。

的app.config:

<?xml version="1.0"?>
<configuration>
<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="ThisIsTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><applicationSettings>
        <ThisIsTest.Properties.Settings>
            <setting name="ThisIsTest_localhost_DentalScannerService" serializeAs="String">
                <value>http://localhost:8731/DentalServiceLib/DentalScannerService/</value>
            </setting>
        </ThisIsTest.Properties.Settings>
    </applicationSettings>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicEndPoint" 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>
        </bindings>
        <client>
            <endpoint address="http://localhost:8731/DentalServiceLib/DentalScannerService/"
                binding="basicHttpBinding" bindingConfiguration="BasicEndPoint"
                contract="Scanner.IDentalScannerService" name="BasicEndPoint" />
        </client>
    </system.serviceModel>
</configuration>

output.config(从wsdl.exe获取,只是 项目 - &gt;添加现有项目添加它:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicEndPoint" 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>
        </bindings>
        <client>
            <endpoint address="http://localhost:8731/DentalServiceLib/DentalScannerService/"
                binding="basicHttpBinding" bindingConfiguration="BasicEndPoint"
                contract="IDentalScannerService" name="BasicEndPoint" />
        </client>
    </system.serviceModel>
</configuration>

2 个答案:

答案 0 :(得分:0)

这很可能是由于端点多次显示在配置文件中。找到<system.serviceModel><client></client></system.serviceModel>部分并检查重复项(基于名称)

如果您的配置文件中根本没有客户端部分,则需要添加它。 按照此模板

<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="<address here>" binding="basicHttpBinding" contract="<full qualifeid class name to client interface>" name="<some name here>" />
        </client>
    </system.serviceModel>
</configuration>

答案 1 :(得分:0)

由于我无法使用此方法,我采取了另一种方式:

  1. 使用SDK中的wsdl.exe生成的.wsdl文件
  2. 下载 soapUI 并以wsdl文件作为初始文件启动新项目
  3. soapUI为Web服务中的所有对象创建了SOAP请求
  4. 在soapUI中测试了不同的调用并获得了实时响应
  5. 进入C#VS并使我的控​​制台应用程序首先启动Web服务,然后使用HttpWebRequest发送简单的SOAP请求
  6. 宾果
  7. 这种方法运作得非常好!