无法调用我的wcf端点

时间:2017-04-19 14:47:25

标签: c# wcf

我已经通过wcf创建了一个端点,并通过控制台应用程序托管了wcf。

我在该方法上放了一个调试器,我试图从浏览器调用该方法,但该方法没有被调用。

我参考了以下网站:

https://msdn.microsoft.com/en-us/library/ms731080(v=vs.110).aspx

http://www.infoworld.com/article/3034606/application-development/how-to-work-with-a-wcf-service-programmatically.html

Wcf服务:

public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
    }

 [ServiceContract]
    public interface ICalculator
    {

        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
    }

托管wcf应用程序的控制台应用程序:

class Program
    {
        static void Main(string[] args)
        {
            var serviceUrl = "http://localhost:56264/CalculatorService.svc";
            var uri = new Uri(serviceUrl);
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), uri);
            try
            {
                BasicHttpBinding binding1 = new BasicHttpBinding();
                selfHost.AddServiceEndpoint(typeof(ICalculator), binding1, "CalculatorService");
                selfHost.Open();
                Console.WriteLine("Service started... " + serviceUrl);
                Console.ReadKey();
            }
            catch (Exception ex)
            {

            }
            finally
            {

            }
        }
    }

enter image description here

当我尝试像下面那样调用我的wcf服务端点时,我得到的结果如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

操作合同应具有WebGet()属性。

 [WebGet()]
 [OperationContract]
 double Add(double n1, double n2);