我的 wcf 客户端显示异常但 WcfTestClient 工具工作正常

My wcf client side shows exception but WcfTestClient Tool works fine

我已经使用 netTcpBinding 编写了 WCF 服务器端代码。然后我编写了一个客户端。但是它在运行时执行 "var sc = new CommondServiceClient();" 时显示异常。我该怎么办?

以下是异常消息:

System.InvalidOperationException HResult=0x80131509 Message=Could not find default endpoint element that references contract 'ICommondService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. Source=System.ServiceModel StackTrace: ......

我试过:

  1. 我可以使用 WcfTestClient 使用服务。
  2. 服务引用由 visual studio "add service reference..." 添加。我想我得到了服务 mex 数据。但是我遇到了像上面那样的运行时异常
  3. 我也试过用 svcutil 工具生成代码,但还是不行

这是 wcf 配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <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="ServiceContractor.CommondService">
        <endpoint address="" binding="netTcpBinding" contract="ServiceContractor.ICommondService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
       </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
          <!-- 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>

这是一个自托管 wcf 服务:

            var baseAddress = new Uri($"net.tcp://localhost:{PORT}/Company/service");
            _host = new ServiceHost(typeof(CommondService), baseAddress);
            try
            {
                var smb = _host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null) _host.Description.Behaviors.Add(new ServiceMetadataBehavior());
                _host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
                _host.AddServiceEndpoint(typeof(ICommondService), new NetTcpBinding(), "");

                _host.Open();
            }
            catch (CommunicationException ce)
            {
                _host.Abort();
            }

我不知道哪里出了问题。我应该索取什么文件?你能帮帮我吗?

But I have another question now. Is it possible to get rid of the configuration. So that the application just needs the DLL and knows nothing about the WCF service.

一般来说,在Web应用中调用WCF服务(Soap服务)有两种方式。

  • 通过添加服务引用生成客户端代理class,这样 通常需要在配置中配置服务设置 file(web.config),大部分需要调用的服务信息是 保存在配置文件中。对于 class 库项目,我们有 将配置迁移到实际的配置文件中 项目。
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
  • 使用ChannelFactory创建通信通道,我们设置 以编程方式设置服务配置。从你的 描述,这种调用网络服务的方式可能是 适当的解决方案。自定义服务配置 以编程方式在代码中。必须注意一件事,所有 服务配置是硬编码的,比如绑定类型和服务 端点信息。您可以参考以下文件。

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory 如果有什么我可以帮忙的,请随时告诉我。