簡體   English   中英

我的wcf客戶端顯示異常,但WcfTestClient Tool工作正常

[英]My wcf client side shows exception but WcfTestClient Tool works fine

我用netTcpBinding編寫了一個WCF服務器端。 然后我編寫了一個客戶端。 但它在執行“var sc = new CommondServiceClient();”時顯示異常。 在運行時。 我該怎么辦?

以下是異常消息:

System.InvalidOperationException HResult = 0x80131509 Message =無法在ServiceModel客戶端配置部分中找到引用合同“ICommondService”的默認端點元素。 這可能是因為沒有為您的應用程序找到配置文件,或者因為在客戶端元素中找不到與此合同匹配的端點元素。 Source = System.ServiceModel StackTrace:......

我嘗試過一些東西:

  1. 我可以使用WcfTestClient來使用服務。
  2. 服務引用由visual studio“添加服務引用...”添加。 我想我得到服務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();
            }

我不知道出了什么問題。 我應該要求什么文件? 你能幫助我嗎?

但我現在有另一個問題。 是否有可能擺脫配置。 因此應用程序只需要DLL並且對WCF服務一無所知。

一般來說,有兩種方法可以在Web應用程序中調用WCF服務(Soap服務)。

  • 通過添加服務引用生成客戶端代理類,這種方式通常需要在配置文件(web.config)中配置服務設置,需要調用的大多數服務信息都存儲在配置文件中。 對於類庫項目,我們必須將配置遷移到實際項目的配置文件中。
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
  • 使用ChannelFactory創建通信通道,我們以編程方式設置服務配置。 從您的描述中,這種調用Web服務的方式可能是適當的解決方案。 在代碼中以編程方式自定義服務配置。 必須注意的是,所有服務配置都是硬編碼的,例如綁定類型和服務端點信息。 您可以參考以下文檔。

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory如果有任何我可以提供的幫助,請隨時告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM