SimpleCrossFrameworkIPC
Release 1.1.4.4
.NET和.CORE之间的简单IPC
这是IPC的灯光,可在.NET或.CORE上使用,甚至混合在一起。转换的“修复程序”是一个黑客,可能不适用于以后的版本。
至于20.09.2020,它在NetStandard 2.0和.NET 4.7.2之间按预期工作。
Class is based on KrakenIPC: https://github.com/darksody/KrakenIPC and Full Duplex Pipes: https://www.codeproject.com/Articles/1179195/Full-Duplex-Asynchronous-Read-Write-with-Named-Pip
我有一个ASP.CORE应用程序,需要从.NET472应用程序中获取数据,而实际工作的唯一IPC是GRPC(https://grpc.io/)。 GRPC对我的项目过大,所以我想要一些简单,更小的东西。
添加了一个时间的自定义延迟,以等待客户端的服务器数据。更改返回方法以投掷异常而不是null,以使将来更容易处理超时。还添加了一个事件,以确保如果您决定抑制异常,则可以捕获所有事件。
服务器和客户端需要共享一个通用接口。
public interface ISimple
{
int Number { get ; }
string Text { get ; }
}服务器包含接口中的数据,此处以静态值表示。您可以使用最常见的数据类型; int,string,char,float,double,long等...
public class Simple : ISimple
{
public int Number { get => 111 ; }
public string Text { get => "Some string" ; }
}现在创建服务器,它所需的只是ChannelName。请注意,该管道仅适用于Localhost
try
{
//Then create server
var handler = new SimpleCrossFrameworkIPC . Server < Simple , ISimple > ( ) ;
handler . Start ( "Channel" ) ;
//Pause for clients
Console . ReadLine ( ) ;
//Stop server
handler . Stop ( ) ;
}
catch ( Exception ex )
{
Console . WriteLine ( ex . ToString ( ) ) ;
}当客户端连接时,它将参考相同的接口。连接后用于从服务器接收数据
int nWaitForServerDataDelay = 2000 ; //2 sec max waiting for data
var client = new SimpleCrossFrameworkIPC . Client < IMySimpleService > ( nWaitForServerDataDelay ) ;
try
{
//Connect with a 1 second connection timeout
client . Connect ( "Channel" , 1000 ) ;
var proxy = client . GetProxy ( ) ;
//Print proxy-data to the console
Console . WriteLine ( "Text: " + proxy . Text ) ;
Console . WriteLine ( "Number: " + proxy . Number . ToString ( ) ) ;
}
catch ( Exception ex )
{
Console . WriteLine ( ex . ToString ( ) ) ;
}管道连接需要异常授权会引发“连接timout”和其他错误。
我从来没有将此课用于复杂的课程,对此的支持是未知的。
public event EventHandler < EventArgs > ClientConnected ;
public event EventHandler < EventArgs > ClientDisconnected ; void Start ( string Pipename )
void Stop ( )
public T GetProxy ( ) public event EventHandler < EventArgs > ClientDisconnected ; void Connect ( string Pipename )
void Connect ( string Pipename , int Timeout )
public void Disconnect ( )
public bool IsConnected ( )
void UseProxy ( Action < T > callback )
public T GetProxy ( ) 欢迎任何贡献:)