c# - 从 Windows 应用程序调用 WCF - 主机

标签 c# wcf

我的解决方案中有 4 个项目。 2 个名为 GetDetfromDBModifyDBService 的 WCF 服务库项目,一个引用这些 WCF 服务库的控制台应用程序以及一个调用此 WCF 服务的 Windows 应用程序。

我在Windows应用程序中引用了控制台应用程序。现在我可以从窗口应用程序调用客户端应用程序。

但是现在我如何通过从 Windows 应用程序调用来调用控制台应用程序中引用的服务?

注意:当我按 F5 或调试器-->启动新实例时,两个服务都可以从我的控制台正确运行。我正确配置了所有绑定(bind)、端点地址。我想知道如何以编程方式调用它。

这是我的控制台应用程序代码:

namespace ServiceHostConsole
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("enter to start");
            Console.Read();

            Type serviceType = typeof(GetDetfromDB.ImpService);
            Type serviceModifyDB = typeof(ModifyDBService.Manipulate);

            ServiceHost host1 = new ServiceHost(serviceModifyDB);
            host1.Open();

            using (ServiceHost host = new ServiceHost(serviceType))
            {
                host.Open();

                Console.WriteLine("The Product Service is available");
                Console.ReadLine(); host.Close();
            }

            Console.WriteLine("Please enter to close modify service");
            Console.Read();
            host1.Close();
        }

        public string returnPath()
        {
            string folder = Environment.CurrentDirectory;
            return folder;
        }
    }
}

从 Windows 应用程序中我这样调用......

 public Form1()
 {
      InitializeComponent();

      ServiceHostConsole.Program pb = new ServiceHostConsole.Program();
      Process.Start(pb.returnPath()+ @"\ServiceHostConsole.exe");
 }

现在,当程序尝试打开我的服务时,我收到以下错误..

Service 'ModifyDBService.Manipulate' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element

我怀疑我只是盲目地调用该方法而不是调用该服务。

请帮助我解决这个问题,过去 5 天我一直在为此苦苦挣扎。

我需要发布我的 Windows 应用程序,当我单击“设置”时,我的 Windows 应用程序和具有调用服务的客户端应用程序都应该打开。这是我的要求。

我的 ServiceHostConsole 的 App.config 看起来像这样,

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="GetDetfromDB.ImpService">
                <endpoint address="http://localhost:8080/MyService" binding="basicHttpBinding"
                    bindingConfiguration="" contract="GetDetfromDB.IGetExpenseValuestype" />
            </service>
            <service name="ModifyDBService.Manipulate">
                <endpoint address="http://localhost:8081/MyService1." binding="basicHttpBinding"
                    bindingConfiguration="" contract="ModifyDBService.IManipulateDB" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

最佳答案

不要引用 WCF 服务库。只能从托管服务的项目中引用它们。

相反,您应该使用“添加服务引用”来引用服务。

参见“How to Consume a Web Service”。

关于c# - 从 Windows 应用程序调用 WCF - 主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10884206/

相关文章:

wcf - 如何为WCF实现2路SSL证书

javascript - 将数据从 JavaScript 文件发送到另一个 JavaScript 文件

c# - 是否可以捕获使用 SmtpClient 发送的电子邮件的 "Message-ID"?

c# - 没有 ASP.NET AJAX 的 JQuery/WCF :

c# - Windows 服务托管 TCP WCF 服务

c# - WCF 与 .Net 远程处理

c# - 在 C# 中使用 Graphics 类绘图

C# NLog - 如何停止用 NLog 替换换行符?

c# - 在下拉项中添加单个图像

c# - WCF - 套接字连接已中止