【问题标题】:How to consume a wcf service in a hostconsole application from client console app如何从客户端控制台应用程序使用主机控制台应用程序中的 wcf 服务
【发布时间】:2010-02-12 15:34:36
【问题描述】:

如何从客户端控制台应用程序使用主机控制台应用程序中的 wcf 服务。 这是 wcf 服务:

namespace HostConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service)))
            {
                host.Open();
                Console.WriteLine("Sai");
                Console.ReadLine();
            }
        }
    }
}

Sky,再次感谢您。你已经读懂了我的想法。

但是我遇到了一个问题:

这是我为主机和客户端修改上述代码的方式:

主持人

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ConsoleApplication1
{
    class Program
    {

        private static void Main(string[] args)
        {
            const string endpointAddress = "net.tcp://localhost:8001/Service";

            var baseAddress = new Uri(endpointAddress);

            using (var host = new ServiceHost(typeof(FirstWcfService.Service), baseAddress))
            {
                host.AddServiceEndpoint(typeof(FirstWcfService.IService), new NetTcpBinding(), baseAddress);
                host.Open();

            }

            Console.WriteLine("Sai");
            Console.ReadLine();
        }

    }
}

客户

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace ConsoleApplication2
{
    class Program
    {
        private static void Main(string[] args)
        {
            const string endpointAddress = "net.tcp://localhost:8001/Service";


                // this is the client code.. simply put this in another console app
                using (var factory = new ChannelFactory<FirstWcfService.IService>(new NetTcpBinding(), endpointAddress))
                {
                    FirstWcfService.IService channel = factory.CreateChannel();

                    //DateTime d = channel.GetDate();
                    string h = channel.Hello();

                    //Console.WriteLine(String.Format("It is {0}", d));
                    Console.WriteLine(String.Format("It is {0}", h));
                }
                //// end client code

        }


    }
}

主机运行良好,当客户端在这里运行时出现异常:

无法连接到 net.tcp://localhost:8001/Service。连接尝试持续了 00:00:00.8899011 的时间跨度。 TCP错误代码10061:无法建立连接,因为目标机器主动拒绝127.0.0.1:8001。

如果您有任何建议,请告诉我。 谢谢 否

【问题讨论】:

  • 使用工作示例编辑答案

标签: wcf


【解决方案1】:

最简单的实现,不知道细节......并且有一些关于错误处理和处理故障的问题,但这应该会让你继续......

您需要确保为您的服务实现了一个接口,并确保服务和客户端之间的绑定和地址匹配。

注意:我使用 net.tcp 有几个原因.. 1) 我知道它符合激发这个问题的要求,2) 它消除了在 2008/vista/7 上处理许多托管 http 服务的陷阱.

using System;
using System.ServiceModel;

namespace ConsoleApplication1
{

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        DateTime GetDate();
    }


    public class ServiceImplementer : IService
    {
        #region IService Members

        public DateTime GetDate()
        {
            return DateTime.Now;
        }

        #endregion
    }



    internal class Program
    {
        private static void Main(string[] args)
        {
            const string endpointAddress = "net.tcp://localhost:8001/Service";

            var baseAddress = new Uri(endpointAddress);

            using (var host = new ServiceHost(typeof (ServiceImplementer), baseAddress))
            {
                host.AddServiceEndpoint(typeof (IService), new NetTcpBinding(), baseAddress);
                host.Open();



                // this is the client code.. simply put this in another console app
                using (var factory = new ChannelFactory<IService>(new NetTcpBinding(), endpointAddress))
                {
                    IService channel = factory.CreateChannel();

                    DateTime d = channel.GetDate();

                    Console.WriteLine(String.Format("It is {0}", d));
                }
                // end client code

            }

            Console.WriteLine("Sai");
            Console.ReadLine();
        }
    }
}

【讨论】:

  • 天空,谢谢。主机、端口和服务的值是什么。
猜你喜欢
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多