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

时间:2012-06-04 15:51:38

标签: c# wcf

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

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

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

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

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

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");
 }

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

  

服务'ModifyDBService.Manipulate'没有应用程序   (非基础设施)端点。这可能是因为没有   找到了您的应用程序的配置文件,或者因为没有   匹配服务名称的服务元素可以在   配置文件,或者因为没有定义端点   服务要素

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

请帮助我摆脱这个问题,过去5天我一直在努力。

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

ServiceHostConsole的My 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>

1 个答案:

答案 0 :(得分:2)

不要引用WCF服务库。它们只应从承载服务的项目中引用。

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

请参阅“How to Consume a Web Service”。

相关问题