[英]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:......
我嘗試過一些東西:
這是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服務)。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory如果有任何我可以提供的幫助,請隨時告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.