2016-09-21 61 views
0

我是C#初学者,我试图建立一个简单的库项目 服务工作很好,但在客户端总是抛出异常的服务对象双工WCF项目在Visual Studio中出现InvalidOperationException

出现InvalidOperationException在 ServiceModel客户端配置部分找不到 引用合同'ServiceReference.IServiceLibrary'的默认端点元素。这可能是因为没有 配置文件中发现您的应用程序,或者是因为没有匹配这份合同 端点元素可以在客户端 元素中找到

它可能是问题?

客户端配置

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
    <supportedRuntime version="v4.0" 
         sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <system.serviceModel> 
    <bindings> 
     <wsDualHttpBinding> 
     <binding name="WSDualHttpBinding_IServiceLibrary" /> 
     </wsDualHttpBinding> 
    </bindings> 
    <client> 
     <endpoint addrres="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" 
       binding="wsDualHttpBinding" 
       bindingConfiguration="WSDualHttpBinding_IServiceLibrary" 
       contract="ServiceReference.IServiceLibrary" 
       name="WSDualHttpBinding_IServiceLibrary"> 
     <identity> 
      <dns value="localhost" /> 
     </identity> 
     </endpoint> 
    </client> 
    </system.serviceModel> 
</configuration> 

服务配置

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
    <add name="WCF.Properties.Settings.DemaLibraryConnectionString" 
     connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\databases\DemaLibrary.mdf;Integrated Security=True;Connect Timeout=30" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
     app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="WCF.ServiceLibrary"> 
     <endpoint address="" binding="wsDualHttpBinding" 
        contract="WCF.IServiceLibrary"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
       set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> 
      <!-- To receive exception details in faults for debugging purposes, 
       set the value below to true. Set to false before deployment 
       to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

主机

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
    <supportedRuntime version="v4.0" 
         sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
     app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="WCF.ServiceLibrary"> 
     <endpoint address="" binding="wsDualHttpBinding" 
        contract="WCF.IServiceLibrary"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    </system.serviceModel> 
</configuration> 
+0

难道你会莫名其妙托管服务库?控制台应用程序,IIS等?如果服务库没有托管,您将无法连接到它。至少,发布你的服务和客户端的''部分。否则,这只不过是一种猜测游戏。 – Tim

+0

是的..我把主机放在另一个解决方案 –

回答

0

您的客户端需要一个包含服务的端点的app.config文件。事情是这样的......

的app.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="wsHttpBinding"> 
      <security mode="Transport" /> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://yourdomain.com/YourService.svc" 
       binding="wsHttpBinding" bindingConfiguration="wsHttpBinding" 
       contract="YourDomain.Contracts.Service.IService" /> 
    </client> 
    </system.serviceModel> 
</configuration> 
0

我首先想到的是你没有复制的服务库的<system.serviceModel>部分的配置对于托管服务的应用程序,但它看起来像你一样。

但是,您的合同名称是错误的。您的客户有合同的ServiceReference.IServiceLibrary,但该服务有WCF.IServiceLibrary。即使代码完全相同,命名空间的差异也会使它们看起来不同。

在你的客户,合同名称更改为WCF.IServiceLibrary这样的:

<client> 
    <endpoint address="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" 
      binding="wsDualHttpBinding" 
      bindingConfiguration="WSDualHttpBinding_IServiceLibrary" 
      contract="WCF.IServiceLibrary" 
      name="WSDualHttpBinding_IServiceLibrary"> 
    <identity> 
     <dns value="localhost" /> 
    </identity> 
    </endpoint> 
</client> 
+0

它没有工作..我添加了服务引用的主机,而不是它自己的服务,我已经改名为ServiceReference –

+0

我真的很失望..我试图解决这个问题3天以来 –

+0

您是否已将配置中的名称更改为“WCF.IServiceLibrary”? – Tim

相关问题