DotNetty icon indicating copy to clipboard operation
DotNetty copied to clipboard

How to use autofac in DotNetty

Open cheetahing opened this issue 4 years ago • 2 comments

I want to use autofac,how to contoller MyHandler life time.

bootstrap.ChildHandler(new ActionChannelInitializer<IChannel>(channel =>
{
	IChannelPipeline pipeline = channel.Pipeline;
	pipeline.AddLast(new MyHandler(configuration));//i want to autofac contoller MyHandler life time
}));
public class MyHandler:ChannelHandlerAdapter
{
	public MyHandler(
		Ilogger logger,
		IMyService myService//i want to injection some of service object
	)
}

cheetahing avatar Mar 20 '20 09:03 cheetahing

I try use this code and throw a exception.
Before do it,i know it can't be work.
Can i override like HandlerFactory.Create function?Please tell me how to do.

exception

DotNetty.Transport.Channels.ChannelPipelineException:“MyHandler is not a @Sharable handler, so can't be added or removed multiple times.”

code

public class MyServer
{
	public MyServer(MyHandler myHandler)//myHandler is autofac injection object
	{		
		//some code
		bootstrap.ChildHandler(new ActionChannelInitializer<IChannel>(channel =>
		{
			IChannelPipeline pipeline = channel.Pipeline;
			pipeline.AddLast(myHandler);//autofac contoller lift time object
		}));
	}
}

cheetahing avatar Mar 20 '20 09:03 cheetahing

your MyHandler only new once ,so no need DI,you can new MyHandler by hardcode

wjkhappy14 avatar Apr 16 '20 03:04 wjkhappy14