Azure / DotNetty

DotNetty project – a port of netty, event-driven asynchronous network application framework
Other
4.09k stars 977 forks source link

How to use autofac in DotNetty #533

Open cheetahing opened 4 years ago

cheetahing commented 4 years ago

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 commented 4 years ago

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
        }));
    }
}
wjkhappy14 commented 4 years ago

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