很难确切地知道发生了什么;但是,我假设您有这样的网络服务
[ServiceContract]
public interface IMyService
{
[OperationContract]
String Hello(String Name);
[OperationContract]
Person GetPerson();
}
你可能有这样的代理:
public class MyPipeClient : IMyService, IDisposable
{
ChannelFactory<IMyService> myServiceFactory;
public MyPipeClient()
{
//This is likely where your culprit will be.
myServiceFactory = new ChannelFactory<IMyService>(new NetNamedPipeBinding(), new EndpointAddress(Constants.myPipeService + @"/" + Constants.myPipeServiceName));
}
public String Hello(String Name)
{
//But this is where you will get the exception
return myServiceFactory.CreateChannel().Hello(Name);
}
public Person GetPerson()
{
return myServiceFactory.CreateChannel().GetPerson();
}
public void Dispose()
{
((IDisposable)myServiceFactory).Dispose();
}
}
如果您在连接时出现错误,您不会在尝试连接到通道工厂时得到它,而是在您实际尝试调用函数时得到它。
要解决此问题,您可以在每个函数调用周围放置一个 try catch 并手动处理异步调用。
相反,您可以使用像 init() 这样的函数,每次实例化连接时都会同步调用该函数。这样,您就知道如果该呼叫连接,则表明您已建立连接。
如果您有随时断开连接的风险,我建议您使用前一个选项。
无论如何,这里有一个如何解决它的示例:
public class MyPipeClient : IMyService, IDisposable
{
ChannelFactory<IMyService> myServiceFactory;
public MyPipeClient()
{
myServiceFactory = new ChannelFactory<IMyService>(new NetNamedPipeBinding(), new EndpointAddress(Constants.myPipeService + @"/" + Constants.myPipeServiceName + 2) );
}
public String Hello(String Name)
{
try
{
return Channel.Hello(Name);
}
catch
{
return String.Empty;
}
}
public Person GetPerson()
{
try
{
return Channel.GetPerson();
}
catch
{
return null;
}
}
public Task<Person> GetPersonAsync()
{
return new Task<Person>(()=> GetPerson());
}
public Task<String> HelloAsync(String Name)
{
return new Task<String>(()=> Hello(Name));
}
public void Dispose()
{
myServiceFactory.Close();
}
public IMyService Channel
{
get
{
return myServiceFactory.CreateChannel();
}
}
}
我上传了我写的源代码,以便您可以下载完整的源代码。你可以在这里得到它:https://github.com/Aelphaeis/MyWcfPipeExample
PS:这个存储库抛出了你得到的异常。要删除它,只需转到 MyPipeClient 并删除构造函数中的 + 2。
如果您使用的是 Duplex,请考虑使用此存储库:
https://github.com/Aelphaeis/MyWcfDuplexPipeExample