Not able to consume WCF service in Class Library Application
本问题已经有最佳答案,请猛点这里访问。
我有一个在IIs8.5上运行的WCF服务。我在Windows窗体项目和控制台应用程序项目中使用并测试了该服务,工作正常!但我无法使用Visual Studio 2013中类库项目中的Web服务。
在ServiceModel客户端配置节中找不到引用协定"bindsignarr.bindsinalrservice"的默认终结点元素。这可能是因为找不到应用程序的配置文件,或者是因为在客户端元素中找不到与此协定匹配的终结点元素。
我查看了Windows窗体项目(有效)和类库项目的配置文件,但找不到区别。我做错什么了?
服务的web.config是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <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(这个很好用!)应用程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <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服务时,我得到了错误:在ServiceModel客户端配置节中找不到引用协定"bindsignarr.bindsinalrservice"的默认终结点元素。这可能是因为找不到应用程序的配置文件,或者是因为在客户端元素中找不到与此协定匹配的终结点元素。
谢谢你帮我。我真的很难接受(投入了很多小时)。
当您在库应用程序中使用Web服务(无论您要在哪里使用库)时,必须将app.config文件的端点和绑定部分从库复制到您要使用库的项目中的(web/app.config)!!!!(如果有问题,请纠正我)。
在我的案例中,我复制了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <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> |