【问题标题】:Duplex WCF Project In Visual Studio InvalidOperationExceptionVisual Studio InvalidOperationException 中的双工 WCF 项目
【发布时间】:2016-09-21 17:37:27
【问题描述】:

我是 C# 的初学者,我正在尝试构建一个简单的库项目 该服务运行良好,但在客户端总是在服务对象中抛出异常

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

问题出在哪里?

客户端配置

<?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>

【问题讨论】:

  • 您是否以某种方式托管服务库?控制台应用程序、IIS 等?如果服务库未托管,您将无法连接到它。至少,张贴您的服务和客户的&lt;system.serviceModel&gt; 部分。否则这不过是一场猜谜游戏。
  • 是的..我把主机放在另一个解决方案中

标签: c# wcf service invalidoperationexception clients


【解决方案1】:

您的客户端需要一个包含服务端点的 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>

【讨论】:

    【解决方案2】:

    我的第一个想法是您没有将服务库的 &lt;system.serviceModel&gt; 部分复制到托管服务的应用程序的配置中,但看起来您这样做了。

    但是,您的合同名称错误。您的客户端中的合同有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>
    

    【讨论】:

    • 它不起作用..我添加了主机的服务引用而不是它自己的服务,我已将名称更改为 ServiceReference
    • 我真的对此感到绝望..我从 3 天以来一直在努力解决这个问题
    • 你把配置里的名字改成WCF.IServiceLibrary了吗?
    • 是的..仍然抛出异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多