WCF 命名管道错误:管道已结束。 (109, 0x6d)

2024-03-08

我看过其他有关“管道已结束。(109,0x6d)”的帖子,但没有一个能解决我的问题。我在这个博客上有一个相对简单的设置基础:http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication

我觉得我非常严格地遵循它,只是删除了 HTTP 绑定。

这是服务器代码:

public class InterProcessServer : IInterProcessServer 
{
    private ServiceHost _host = null;

    public event EventHandler<CommandLineArgsEventArgs> CommandLineArgsReceived;

    protected InterProcessServer(Uri serverAddress, string serviceName)
    {
        IPassCommandLineArgs passArgs = null;
        passArgs = CreatePassCommandLineArgs();
        passArgs.CommandLineArgsReceived += new EventHandler<CommandLineArgsEventArgs> passArgs_CommandLineArgsReceived);

        _host = new ServiceHost(passArgs, new Uri[] { serverAddress });
        _host.AddServiceEndpoint(typeof(IPassCommandLineArgs), new NetNamedPipeBinding(), serviceName);
        _host.Open();
    }

    public static IInterProcessServer CreateInterProcessServer(Uri serverAddress, string serviceName)
    {
        return new InterProcessServer(serverAddress, serviceName);
    }

    public void Dispose()
    {
        try
        {
            _host.Close();
        }
        catch { }
    }

    private void passArgs_CommandLineArgsReceived(object sender, CommandLineArgsEventArgs e)
    {
        EventHandler<CommandLineArgsEventArgs> handler = CommandLineArgsReceived;

        if (handler != null)
            handler(sender, e);
    }

    protected virtual IPassCommandLineArgs CreatePassCommandLineArgs()
    {
        return new PassCommandLineArgs();
    }
}

这是客户端代码:

public class InterProcessClient : IInterProcessClient
{
    private IPassCommandLineArgs _pipeProxy = null;
    private ChannelFactory<IPassCommandLineArgs> _pipeFactory = null;

    protected InterProcessClient(Uri serviceAddress)
    {
        _pipeFactory = new ChannelFactory<IPassCommandLineArgs>(new NetNamedPipeBinding(), new EndpointAddress(serviceAddress));
        _pipeProxy = _pipeFactory.CreateChannel();
    }

    public static IInterProcessClient CreateInterProcessClient(Uri serviceAddress)
    {
        return new InterProcessClient(serviceAddress);
    }

    public void SendArgs(string[] args)
    {
        _pipeProxy.PassArgs(args);           
    }


    public void Dispose()
    {
        try
        {
            if (_pipeFactory != null)
                _pipeFactory.Close();
        }
        catch { }
    }
}

我已确保客户端连接的地址是正确的。任何人都可以提供一个想法为什么我可能会收到错误_pipeProxy.PassArgs(args);是从客户端调用的?该测试仅在同一台计算机上运行不同进程的两个控制台应用程序之间进行。

顺便说一句,框架 4.0。

Thanks!

EDIT以下是服务接口和实现:

[ServiceContract]
public interface IPassCommandLineArgs
{
    event EventHandler<CommandLineArgsEventArgs> CommandLineArgsReceived;

    [OperationContract]
    void PassArgs(string[] args);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class PassCommandLineArgs : IPassCommandLineArgs
{
    public event EventHandler<CommandLineArgsEventArgs> CommandLineArgsReceived;

    public void PassArgs(string[] args)
    {
        EventHandler<CommandLineArgsEventArgs> hander = CommandLineArgsReceived;

        if (hander != null)
            hander(this, new CommandLineArgsEventArgs() { Args = args });
    }
}

好的。这是向客户端传递具有无效字符的地址的调用代码的问题。而已。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WCF 命名管道错误:管道已结束。 (109, 0x6d) 的相关文章

随机推荐