c# - 启动 WCF 客户端时为 "No default endpoint found"

标签 c# wpf wcf windows-services

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

契约(Contract): IFilesDuplex 契约(Contract):

[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();
    }
}

以及抛出错误的主窗口中的代码:

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;
        }

应用程序配置:

<?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"/>

现在连接正常了。

关于c# - 启动 WCF 客户端时为 "No default endpoint found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099104/

相关文章:

c# - 文本标签旋转 45 度

c# - 使用 hWnd 设置 Window.Owner

wcf - 处理 Azure 暂存疯狂 URL

c# - 如何得到 Nancy 谈判代表的回应?

c# - 绑定(bind)到 ASP.NET MVC 中的强类型对象集合

c# - WPF C# 提取并显示默认文件图标

wpf - Winforms属性网格高dpi ui重叠

c# - 不允许 JSONP WCF 方法

silverlight - Silverlight 和 WCF 服务的通信异常

c# - getelementbyid().length 未定义?