"No default endpoint found" 启动 WCF 客户端时

"No default endpoint found" when starting WCF client

我有一个代表 WCF 主机的 Windows 服务和一个代表 WCF 客户端的 WPF 客户端应用程序。 通信应该是双工的,所以我选择了 WSDualHttpBinding。 起初我安装并启动我的服务,它打开一个 WCF 连接,然后我启动我的 WPF 应用程序,我得到以下错误(我翻译了它): 没有找到合约的默认端点 \"WCFCloudManagerFolderWatcherService.Interfaces.IFilesDuplex\"服务模型客户端配置部分 指的是。这可能是由于: 没有找到配置文件 或者在客户端元素中没有找到端点元素,这对应于此合同。

合同: IFilesDuplex-合同:

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required,
            CallbackContract = typeof(IFilesDuplexCallback))]
public interface IFilesDuplex
{
    [OperationContract(IsOneWay = true)]
    void Update();
}

IFilesDuplexCallback:

interface IFilesDuplexCallback
{
    [OperationContract(IsOneWay = true)]
    void Equals(string[] result);
}

客户端 回调处理程序:

class CallbackHandler : IFilesDuplexCallback
{
    public event Action<string[]> ReceivedList = delegate { };

    public void Equals(string[] result)
    {
        this.ReceivedList(result);
    }
}

客户本身:

class FilesDuplexClient : DuplexClientBase<IFilesDuplex>, IFilesDuplex
{
    public FilesDuplexClient(InstanceContext callbackCntx)
        : base(callbackCntx)
    {            
    }

    public void Update()
    {
        base.Channel.Update();
    }
}

以及来自 Main Window 的代码,其中抛出错误:

CallbackHandler ch = new CallbackHandler();
        ch.ReceivedList += ch_ReceivedList;

        // Construct InstanceContext to handle messages on callback interface
        InstanceContext instanceContext = new InstanceContext(ch);

        // Create a client
        FilesDuplexClient client = new FilesDuplexClient(instanceContext);
        client.Update();

服务器端(Windows服务) FileProtocoll-Class(服务器代码)

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class FileProtocoll : IFilesDuplex
{
    IFilesDuplexCallback Callback
    { get { return OperationContext.Current.GetCallbackChannel<IFilesDuplexCallback>(); } }

    void IFilesDuplex.Update()
    {
        //....
        Callback.Equals(null);// just a dummy
        //...
    }

}

OnStart-Method 中的代码(在线程中):

// Step 1 Create a URI to serve as the base address.
        Uri baseAddress = new Uri("http://localhost:8899/CloudManager/CommunicationChannel1");

        // Step 2 Create a ServiceHost instance
        if (selfHost != null)
        {
            selfHost.Close();
        }

        selfHost = new ServiceHost(typeof(FileProtocoll), baseAddress);

        try
        {
            // Step 5 Start the service.
            selfHost.Open();



        }
        catch (CommunicationException ce)
        {
             selfHost.Abort();
        }

OnStop-Method 中的代码(在线程中):

if (selfHost != null)
        {
            if (selfHost.State != CommunicationState.Closed)
            { 
                selfHost.Close();
            }

            selfHost = null;
        }

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>     
    </startup>
  <system.serviceModel>
    <services >
      <service behaviorConfiguration="ServiceBehavior"
   name="WCFCloudManagerFolderWatcherService.Communication.FileProtocoll">
        <endpoint address="http://localhost:8899/CloudManager /CommunicationChannel1"
        binding="wsDualHttpBinding"     contract="WCFCloudManagerFolderWatcherService.Interfaces.IFilesDuplex">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex"
        binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true "/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我终于明白了。 必须对我的客户端进行一些更改 app.config 并且必须关闭安全性。

app.config(客户端):

<!-- WCF Client information-->
<system.serviceModel>
<bindings>
  <wsDualHttpBinding>
    <binding name="WSDualHttpBinding_IFilesDuplex">
      <security mode="None"/>
    </binding>
  </wsDualHttpBinding>
</bindings>
<client>
  <endpoint
    address="http://localhost:8899/CloudManager/CommunicationChannel1"
    binding="wsDualHttpBinding"
    bindingConfiguration="WSDualHttpBinding_IFilesDuplex"
        contract="WCFCloudManagerFolderWatcherService.Interfaces.IFilesDuplex"
    name="WSDualHttpBinding_IFilesDuplex">
    <identity>
      <userPrincipalName value="localhost"/>
    </identity>
  </endpoint>
</client>

并且在服务器端的 app.config 中我也不得不设置

<security mode="None"/>

现在连接正常了。