提供两个接口的 Web 服务在 WPF 应用程序中引发错误。

4

我有一个提供两个接口的Web服务,一个是“MyAppNameData”,另一个是“MyAppNameSync”。我正在向WPF应用程序添加两个服务引用。在代码中,当我使用“MyAppNameData”引用时,没有出现错误。但是当我使用“MyAppNameSync”时,会生成以下错误:

Could not find default endpoint element that references contract 'MyAppNameSync.IMyAppNameSync' 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.

我以完全相同的方式添加了这两个引用,但是MyAppNameData是使用BasicHttpBinding添加的,而MyAppNameSync是使用WSHttpBinding添加的。我不知道为什么会出现这种情况。

以下是客户端app.config文件中的serviceModel元素。正如您所看到的,有一个endpoint元素引用了合同'MyAppNameSync.IMyAppNameSync',这与错误消息所说的相反:

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IMyAppNameData" 
                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>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IMyAppNameSync" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" 
                hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" 
                 maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" 
                     proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" 
                    negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://computername.domainname.home/MyAppNameSyncService/MyAppNameData.svc"
            binding="basicHttpBinding" 
            bindingConfiguration="BasicHttpBinding_IMyAppNameData"
            contract="MyAppNameData.IMyAppNameData" 
            name="BasicHttpBinding_IMyAppNameData" />
        <endpoint address="http://computername.domainname.home/MyAppNameSyncService/MyAppNameSync.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyAppNameSync"
            contract="MyAppNameSync.IMyAppNameSync" 
            name="WSHttpBinding_IMyAppNameSync">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>       
</system.serviceModel>

任何建议都将不胜感激。
谢谢。

你所说的“以相同方式添加两个引用”是什么意思?你只需要添加一个引用,它就会创建两个代理。 - Ladislav Mrnka
感谢您的回复。我发布的Web服务中有两个.svc文件,分别是MyAppNameData.svc和MyAppNameSync.svc。为了添加服务引用,我右键单击“服务引用”节点,选择添加一个svc文件,给它命名空间名称,然后点击“确定”。我再次为另一个svc文件执行相同的操作。 - rogdawg
好的,抱歉我误以为您有一个具有两个端点的单一服务,但实际上您有两个不相关的服务。 - Ladislav Mrnka
1
它是否只适用于单个 wsHttpBinding?如果将 wsHttpBinding 切换为 basicHttpBinding,它是否能正常工作?基本上,您能否确定是 wsHttpBinding 语法导致问题,还是多个绑定导致问题? - Rachel
2
看起来你并没有尝试 Rachel 的完整建议。你能否仅引用 MyAppNameSync?在此之前,你不会知道问题是否出现在绑定上,还是双重绑定上。 - sellmeadog
显示剩余2条评论
1个回答

2
好的。现在它可以正常工作了。显然,我同时出现了几个错误。正如Rachel在她上面的评论中建议的那样,我尝试让最简单的情况起作用。但是一开始我无法做到。从主机中删除一个服务,因此我只有MyAppNameData.svc一开始没有起作用。我知道过去Web服务的简单版本已经起作用了,所以我尝试将所有内容恢复到那个工作点。
我使用我的现有ASP.NET Web应用程序之一指向我一直用于测试的Web服务实例,并且我能够使其工作(即使同时具有两个服务;MyAppNameData.svc和MyAppNameSync.svc在主机MyAppNameWebService中)。所以,我知道问题出在我的WPF应用程序和Web服务之间。
我看到了几个讨论线程,其中提到“如果您正在调用类库中的服务并从另一个项目调用类库,则可能会出现此错误”。但是,我认为在解决问题时已经考虑了这个问题。但是,当我将所有内容都从我的WPF项目中剥离出来,并重新构建了它们(第100次),确保“将WS配置设置包含到主项目的app.config(如果是winapp)或web.config(如果是Web应用程序)中”如此线程所述,我能够使它工作!
我之前尝试过这个解决方案,但显然我仍然没有解决其他问题。我不再收到错误,并且可以从同一主机连接并使用两个单独的服务。所以,我在业务上了。
感谢大家的回复和建议。现在,面对其他挑战!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接