【问题标题】:Cannot call WCF WebService from DLL无法从 DLL 调用 WCF WebService
【发布时间】:2011-12-23 14:32:52
【问题描述】:

我正在尝试从我制作的 dll 中调用 WCF Web 服务,该 dll 在我们的 CAD 软件中运行。 但我无法让它工作。

当我尝试建立代理时,我收到以下错误:

在 ServiceModel 客户端配置部分找不到名为“BasicHttpBinding_IAxaptaService”和合同“AxaptaProxy.IAxaptaService”的端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素。

我搜索了一下升技,我认为问题是由于我的 DLL 在另一个程序中运行。 有一些关于将 EndPoint 配置从应用程序复制到服务的文章,但我不太明白我应该做什么。

任何人有想法,我怎样才能做到这一点?

我的客户创建的 App.Config 是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IAxaptaService" 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:4726/LM/AxaptaService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAxaptaService"
                contract="AxaptaProxy.IAxaptaService" name="BasicHttpBinding_IAxaptaService" />
        </client>
    </system.serviceModel>
</configuration>

我已尝试将其合并到我的 web.config 中,在托管 Web 服务的站点上,如下所示:

  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="GetStream.customBinding0">
          <binaryMessageEncoding/>
          <httpTransport/>
        </binding>
      </customBinding>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IAxaptaService" 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>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AutoCompletionAspNetAjaxBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="AutoCompletion">
        <endpoint address="" behaviorConfiguration="AutoCompletionAspNetAjaxBehavior" binding="webHttpBinding" contract="AutoCompletion"/>
      </service>
      <service name="GetStream">
        <endpoint address="" binding="customBinding" bindingConfiguration="GetStream.customBinding0" contract="GetStream"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <client>
      <endpoint address="http://localhost:4726/LM/AxaptaService.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAxaptaService"
          contract="AxaptaProxy.IAxaptaService" name="BasicHttpBinding_IAxaptaService" />
    </client>
  </system.serviceModel>

里面已经有一些其他的东西了。我可以删除它们,如果这样更容易的话。我把它们留在里面,以防它们对它有一些影响。

所以我从一个独立的 winform 应用程序测试了该服务,它运行良好。 可能是因为 App.config 吗? .dll 的配置是否已加载?

【问题讨论】:

  • 如果您的应用程序名为 myapp.exe,则需要将其复制到 myapp.exe.config
  • @Nicolai:基本上看起来是对的。您尝试连接的服务在哪里?同一个盒子?还是不同的盒子?如果您在复制该配置后遇到相同的错误,我会感到惊讶,并且预计您现在会遇到不同的异常 - 可能是找不到服务。
  • Merlyn:是的,现在一切都在我的本地主机上,使用 VisualStudio 的内部网络服务器。
  • 我尝试创建一个 winform 应用程序,并将服务引用添加到该应用程序,并且效果很好。我猜是我的 .dll 有问题。

标签: c# wcf web-services dll


【解决方案1】:

您需要将连接信息从 MyDll.dll.config 复制到 Web.config。

小心合并配置部分,而不是简单地并排添加新数据或替换它。如果已经有同名的部分,您可能需要将它们合并。

这是一篇描述 app.config 的 WCF 部分内容的文章:

http://msdn.microsoft.com/en-us/library/ms734663.aspx

主要部分有:

<system.serviceModel>
   <bindings>
     <!-- various bindings go here... -->
   </bindings>
   <client>
     <!-- endpoints go here... -->
   </client>
</system.serviceModel>

您需要组合这些节点中的所有内容 - 将各种类型的 endpoint 元素和 binding 元素添加到服务的 web.config。

所以,如果你有一个如下所示的配置:

<system.serviceModel>
   <bindings>
     <someBindingType name="someBinding" />
   </bindings>
   <client>
     <endPoint name="someEndpoint />
   </client>
</system.serviceModel>

您需要复制 someBindingTypeendPoint 元素。整个元素,包括结束标签(如果有的话)和子元素。

确保不要重复 system.serviceModelbindingsclient 元素。如果它们已经存在,请合并到它们中,而不是创建新元素/复制。

【讨论】:

  • 我不完全确定如何做到这一点。它是绑定还是端点(或两者)?我应该将它合并到 web.config 中吗?我的 Web 服务正在现有的 ASP.NET 站点中运行。
  • @Nicolai:哦,这是一个网络服务。出于某种原因,我认为您的意思是 Windows 服务。是的,然后将数据合并到web.config。我已经编辑了答案来解决这个问题。
  • 我希望我明白这一点,但我显然不明白。我已将 App.config 中的绑定复制/粘贴到我的 web.config 中。我该如何处理客户端部分?我是否只是将整个客户端节点复制到我的 web.config 中?我尝试将内容粘贴到 web.config 中的新服务节点中,但这似乎没有任何好处。
  • @Nicolai:对于绑定下的每个元素,复制。对于客户端下的每个元素,复制。这意味着您将复制 wsHttpBindingendpoint 之类的内容。您需要复制每个绑定和端点元素的全部内容,而不是每个元素的一部分。但是,如果事实证明有多个名称相同,那么您将发生冲突。如果最终出现这种情况,请将您的一些配置文件粘贴到您的问题中。
  • @Nicolai: "新服务节点" - 确保您没有两个 system.serviceModel 节点、两个 bindings 节点或两个 client 节点。如果这些节点已经存在,则必须合并它们。
【解决方案2】:

我终于让它工作了!

问题是,我的 .dll 项目中没有加载 app.config。 为了解决这个问题,我在代码中创建了绑定,而不是通过 app.config,如该线程中所述: WCF Configuration without a config file

感谢您的所有帮助。 Merlyn,如果没有你的帮助,我什至不会走到这一步。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多