日期:2014-05-18  浏览次数:20441 次

“WCF”服务寄宿和客户端地址
服务器端:
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
  {
  host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");
  if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
  {
  ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
  behavior.HttpGetEnabled = true;
  behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");
问题一:这两个地址的作用有什么不同呢?为什么不一样???

问题二:“终结点(Endpoint)”怎样理解?作用是什么???


  host.Description.Behaviors.Add(behavior);
  }
  host.Opened += delegate
  {
  Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
  };
   
  host.Open();
  Console.Read();
  }

客户端:
第一种方式,添加服务引用:
using (CalculatorServiceClient proxy = new CalculatorServiceClient())
  {
  Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
  Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
  Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
  Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
  }

问题三:为什么这种方式,使用地址“http://127.0.0.1:9999/calculatorservice/metadata”?

第二种方式,ChannelFactory<T>:

using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"))
  {
  ICalculator proxy = channelFactory.CreateChannel();
  using (proxy as IDisposable)
  {
  Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
  Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
  Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
  Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
  }
  }

问题四:为什么这种方式,使用地址“http://127.0.0.1:9999/calculatorservice”?

谢谢,各位,小弟刚刚开始使用“WCF”。。。

------解决方案--------------------
问题一:这两个地址的作用有什么不同呢?为什么不一样???
findcaiyzh: 一个是web service的地址,一个是metadata(web service的描述)的地址。
问题二:“终结点(Endpoint)”怎样理解?作用是什么???
findcaiyzh:实际上就是一个web service了,有A(Address),B(Binding)C(Contract)构成


------解决方案--------------------
问题三:为什么这种方式,使用地址“http://127.0.0.1:9999/calculatorservice/metadata”?
findcaiyzh:添加引用是 Vs需要metadata的地址下载web service的描述,用于生成客户端使用的Proxy 类。你的CalculatorServiceClient就是Proxy class

问题四:为什么这种方式,使用地址“http://127.0.0.1:9999/calculatorservice”?
findcaiyzh:因为这里需要的是实际web service的地址。而不是metadata的地址。