Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 配置不正确的WCF服务器主机名没有例外_C#_.net_Wcf - Fatal编程技术网

C# 配置不正确的WCF服务器主机名没有例外

C# 配置不正确的WCF服务器主机名没有例外,c#,.net,wcf,C#,.net,Wcf,我有一个使用WCF的服务器客户端实现,其中客户端在服务器启动时向服务器发送Connect消息 这是我在客户端应用程序中使用的代码(在本例中,WCF服务是自托管的): 我遇到的问题是,当客户端的配置文件中没有正确配置服务器地址时,当我尝试发送Connect消息时,通道会出现故障,但没有捕获到异常 这是因为连接发生在不同的线程上,因此它永远不会出现在我的catch代码中吗?如何在代码中捕获端点配置异常?InnerChannel\u Faulted的EventArgs成员不包含任何有用信息 非常感谢

我有一个使用
WCF
的服务器客户端实现,其中客户端在服务器启动时向服务器发送
Connect
消息

这是我在客户端应用程序中使用的代码(在本例中,WCF服务是自托管的):

我遇到的问题是,当客户端的配置文件中没有正确配置服务器地址时,当我尝试发送
Connect
消息时,通道会出现故障,但没有捕获到异常

这是因为连接发生在不同的线程上,因此它永远不会出现在我的
catch
代码中吗?如何在代码中捕获端点配置异常?
InnerChannel\u Faulted
EventArgs
成员不包含任何有用信息

非常感谢

public void InitializeCommunicationChannel()
    {
        this._proxy = new DistributionServiceClient(this._context, "NetTcpBinding_IDistributionService");

        Log.Write(Level.Debug, "Initialized proxy..");

        //set credentials
        this._proxy.ClientCredentials.Windows.ClientCredential.Domain = PasswordManager.ServerAdminDomain;
        this._proxy.ClientCredentials.Windows.ClientCredential.UserName = PasswordManager.ServerAdminUsername;
        this._proxy.ClientCredentials.Windows.ClientCredential.Password = PasswordManager.ServerAdminPassword;

        this._proxy.InnerChannel.Faulted += new EventHandler(this.InnerChannel_Faulted);
        this._proxy.InnerChannel.Closing += new EventHandler(this.InnerChannel_Closing);

        //send a connect message to the server
        try
        {
            Log.Write(Level.Debug, "Sending connect message..");

            this.StationId = this._proxy.ClientConnected(ClientConfiguration.HostName, this.Version, ClientConfiguration.CurrentIpAddress);
            Log.Write(Level.Debug, "Connect message sent..");

            Log.Write(Level.Info, "Connected to server.");
            this.ConnectionState = "Connected";
        }
        catch (ConfigurationErrorsException cEx)
        {
            Log.Write(Level.Error, cEx.Message);
            Log.Write(Level.Debug, cEx.ToString());
        }
        catch (FaultException fEx)
        {
            //probably server didn't recognize configured ip or hostname
            Log.Write(Level.Error, fEx.Message);
            Log.Write(Level.Debug, fEx.ToString());
        }
        catch (CommunicationException cEx)
        {
            Log.Write(Level.Debug, cEx.Message);
            Log.Write(Level.Debug, cEx.ToString());
        }
        catch (System.Security.Authentication.InvalidCredentialException iEx)
        {
            Log.Write(Level.Error, iEx.Message);
            Log.Write(Level.Debug, iEx.ToString());
        }
        catch (Exception ex)
        {
            Log.Write(Level.Error, ex.Message);
            Log.Write(Level.Debug, ex.ToString());
        }

    void InnerChannel_Closing(object sender, EventArgs e)
    {
        Log.Write(Level.Debug, "Communication channel is closing down..");
        this.ConnectionState = "Attempting connection..";
    }

    void InnerChannel_Faulted(object sender, EventArgs e)
    {
        this.ConnectionState = "Not connected";
        Log.Write(Level.Debug, "Communication channel faulted..");

        //something went wrong
        this._proxy.Abort();
        this._proxy.InnerChannel.Faulted -= this.InnerChannel_Faulted;
        this._proxy.InnerChannel.Closing -= this.InnerChannel_Closing;

        //give the server a chance to get back online; retry after 10s
        Thread.Sleep(10000);

        //re-initialize the communication channel
        this.InitializeCommunicationChannel();
    }