无法在类库应用程序中使用WCF服务

时间:2015-01-27 18:18:52

标签: wcf

我有一个在IIS 8.5上运行的WCF服务。我在Windows窗体项目和控制台应用程序项目中使用并测试了该服务并且工作正常!但是我无法从Visual Studio 2013中的类库项目中使用Web服务。

无法找到引用合同的默认端点元素' bindSignalR.bindSinalRService'在ServiceModel客户端配置部分中。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

我查看了Windows窗体项目(可以工作)和类库项目的配置文件,但我找不到区别。我做错了什么?

该服务的web.Config是:

<configuration>
    <system.web>
      <compilation targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
                   <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <customBinding>
                <binding name="bindSignalRService.Web.bindSinalRService.customBinding0">
                    <binaryMessageEncoding />
                    <httpTransport />
                </binding>
                               </customBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
        <services>
            <service name="bindSignalRService.Web.bindSinalRService">
                <endpoint address="" binding="customBinding" bindingConfiguration="bindSignalRService.Web.bindSinalRService.customBinding0"
                    contract="bindSignalRService.Web.bindSinalRService" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

Windows窗体中的app.config(这个工作正常!)app:

<configuration>
    <configSections>
    </configSections>
    <system.serviceModel>
        <bindings>
            <customBinding>
                                   <binding name="CustomBinding_bindSinalRService">
                    <binaryMessageEncoding />
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <client>
          <endpoint address="http://slascvm042:49904/bindSinalRService.svc"
                binding="customBinding" bindingConfiguration="CustomBinding_bindSinalRService"
                contract="bindNewEventService.bindSinalRService" name="CustomBinding_bindSinalRService" />

        </client>
    </system.serviceModel>
</configuration>

但是当我尝试在类库应用程序中使用WCF服务时,我得到了错误: 无法找到引用合同的默认端点元素&#39; bindSignalR.bindSinalRService&#39;在ServiceModel客户端配置部分中。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

1 个答案:

答案 0 :(得分:2)

解决:感谢您试图帮助我。我真的很难(投入很多很多小时)。

在库应用程序中使用Web服务时(无论您在何处使用库),都必须将app.config文件的端点和绑定部分从库复制到(web / app)。在项目中配置您想要使用 LIBRARY !!! (如果出现问题,请纠正我。)

在我的情况下,我复制了:

<system.serviceModel>
    <bindings>
        <customBinding>
            <binding name="bind">
                <binaryMessageEncoding />
                <httpTransport />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:49904/bindSinalRService.svc"
            binding="customBinding" bindingConfiguration="bind" contract="bindSignalRService.Web.bindSinalRService"
            name="myEnpointName" />
    </client>
</system.serviceModel>