我用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如果有任何我可以提供的帮助,请随时告诉我。