Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wcf - Host a SOAP service within a BackgroundService using .Net Core 5

I'm new to .NET Core. I currently have a WCF host service that hosts another service that I'm trying to convert to .NET core. Using .NET 5, I created a worker service that handles the host background tasks and setup another service w/ an endpoint to handle incoming responses from another client. I'm having trouble using the EndpointAddress and ChannelFactory approach to create the endpoint and channel so the endpoint can be accessible via the outside world for response messages, but in doing so, I get the following error:

"No connection could be made because the target machine actively refused it. (localhost:8000)"

Maybe I'm going about this in the wrong way to host the service, not sure. Does anyone know?

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
            return;
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<JLinkHostService>();
                });
        }
    }

    [ServiceContractAttribute]
    public interface IResponseService
        {
            [OperationContractAttribute]
            bool ResponseMessage(string sTermID, string sRespMsg);
        }
        
        
    public class ResponseService : IResponseService
    {
        public bool ResponseMessage(string sTermID, string sRespMsg)
        {
            string filePath = $"{c:est"}\{DateTime.Now.ToString("yyyy -MM-dd_HHmmssfff")}.txt";
            System.IO.File.WriteAllText(filePath, $"{sTermID}

{sRespMsg}");
            return true;
        }
    }

    public class HostService : BackgroundService
        {
            public override Task StartAsync(CancellationToken cancellationToken)
            {
                return base.StartAsync(cancellationToken);
            }

            public override Task StopAsync(CancellationToken cancellationToken)
            {
                return base.StopAsync(cancellationToken);
            }

            protected override async Task ExecuteAsync(CancellationToken stoppingToken)
            {
                ChannelFactory<IResponseService> factory = null;

                try
                {
                    Binding binding = new BasicHttpBinding();
                    EndpointAddress respAddress = new EndpointAddress("http://localhost:8000/response.svc");
                    factory = new ChannelFactory<IResponseService>(binding, respAddress);
                    IResponseService channel = factory.CreateChannel();
                    
                    // Test service proxy
                    channel.ResponseMessage("test", "test");

                    while (!stoppingToken.IsCancellationRequested)
                    {
                        // Host background tasks happen here

                        await Task.Delay(Int32.Parse(GetCfgValue("AppSettings:pollingIntervalMilli")), stoppingToken);
                    }
                     }
                catch (Exception ex)
                {
                    Log.Fatal(ex.ToString());
                }
                finally
                {
                    if(factory != null)
                        factory.Close();
                }
            }
        }
    }
question from:https://stackoverflow.com/questions/65945740/host-a-soap-service-within-a-backgroundservice-using-net-core-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...