简体   繁体   English

将WCF服务转换为控制台应用程序

[英]Converting a WCF service to console application

I have a WCF service. 我有WCF服务。 Now I would like to convert it to console application. 现在,我想将其转换为控制台应用程序。 Basically, it is a simple service. 基本上,这是一项简单的服务。 It only does 2 jobs depending on client request by PowerShell request. 根据PowerShell请求的客户端请求,它仅执行2个作业。

Invoke-WebRequest "http://localhost:10000/backup/DoBackup?id=BackupClient1"

or 要么

Invoke-WebRequest "http://localhost:10000/backup/DoBackup?id=BackupClient2"

My service listens on request for this 2 requests. 我的服务根据请求监听这2个请求。

I have this WCF code: 我有这个WCF代码:

    myService service = new myService(_settings);
    backupServiceHost = new WebServiceHost(service, _backupAddress);
    WebHttpBinding binding = new WebHttpBinding();
    string address = "";
    ServiceEndpoint endpoint = backupServiceHost .AddServiceEndpoint(typeof(ISnapshotServiceContract), binding, address);
    backupServiceHost .Open();

I can't use WebServiceHost and WebHttpBinding in my console app. 我无法在控制台应用程序中使用WebServiceHostWebHttpBinding What is the best replacement for this? 什么是最好的替代品? I'm thinking to use simple HttpClient . 我正在考虑使用简单的HttpClient But I'm not sure is it a correct selection? 但是我不确定这是正确的选择吗?

why don't use command line parameters and avoid WebServiceHost altogether? 为什么不使用命令行参数并完全避免使用WebServiceHost You can then just compare your args[] from Main function (see eg https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/ ) to determine if it is BackupClient1 command or BackupClient2 command. 然后,您可以只比较Main函数中的args[] (请参阅例如https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/main-and-command-args/ )以确定是否是BackupClient1命令或BackupClient2命令。

If you want to take this concept further you can extract functionality of BackupClient1 command and BackupClient2 command into separate library and have frontends (WCF and command line) using same backend. 如果您想进一步了解这个概念,可以将BackupClient1命令和BackupClient2命令的功能提取到单独的库中,并使用相同的后端来创建前端(WCF和命令行)。

If you really need WCF service (as stated by comments below) please refer to this MSDN article ; 如果您确实需要WCF服务(如下面的评论所述),请参阅此MSDN文章 you will have to provide your configuration and figure out how to indicate that you wish to end the process in nice way but it should boil down to something like this: 您将必须提供您的配置并弄清楚如何指示您希望以一种不错的方式结束该过程,但是它应该归结为以下内容:

String baseAddress = "http://localhost:10000/backup";
BasicHttpBinding binding = new BasicHttpBinding();

using (ServiceHost host = new ServiceHost(typeof(myService)))
{
    host.AddServiceEndpoint(typeof(IMyService), binding, baseAddress);
    host.Open();

    while(true) {
         // Figure out how to exit this loop - e.g. listen to some command from service
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM