【问题标题】:Trouble following Microsoft's WCF tutorial无法遵循 Microsoft 的 WCF 教程
【发布时间】:2012-02-14 11:36:06
【问题描述】:

我正在尝试遵循 Microsoft 的 WCF 教程,并且正在执行此步骤:http://msdn.microsoft.com/en-us/library/ms733133.aspx

不幸的是,当我运行这个命令时:

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service

它给了我一个 404 错误。该服务正在运行,我可以通过 Web 浏览器成功访问它:http://localhost:8000/ServiceModelSamples/service

我做错了什么?如果它有助于我在下面发布我正在使用的示例主机的所有代码。

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

namespace Microsoft.ServiceModel.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri BaseAddress = new Uri("http://locoalhost:8000/ServiceModelSamples/Service");
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), BaseAddress);

            try
            {
                selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An expection occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }

        [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
        public interface ICalculator
        {
            [OperationContract]
            double Add(double n1, double n2);

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

            [OperationContract]
            double Multiply(double n1, double n2);

            [OperationContract]
            double Divide(double n1, double n2);
        }

        public class CalculatorService : ICalculator
        {
            public double Add(double n1, double n2)
            {
                double result = n1 + n2;
                Console.WriteLine("Received Add({0},{1})", n1, n2);
                // Code added to write output to the console window.
                Console.WriteLine("Return: {0}", result);
                return result;
            }

            public double Subtract(double n1, double n2)
            {
                double result = n1 - n2;
                Console.WriteLine("Received Subtract({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }

            public double Multiply(double n1, double n2)
            {
                double result = n1 * n2;
                Console.WriteLine("Received Multiply({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }

            public double Divide(double n1, double n2)
            {
                double result = n1 / n2;
                Console.WriteLine("Received Divide({0},{1})", n1, n2);
                Console.WriteLine("Return: {0}", result);
                return result;
            }
        }
    }
}

【问题讨论】:

    标签: c# wcf svcutil.exe


    【解决方案1】:

    第一件事是你有一个错字localhost(你有本地主机)

    Uri BaseAddress = new Uri("http://locoalhost:8000/ServiceModelSamples/Service");
    

    您还可以下载 fiddler@http://fiddler2.com/fiddler2/ 以查看流量​​和客户端代码发出的请求。这将提供下一个线索。在对 http 服务进行故障排除时,它是必不可少的。

    http://fiddler2.com/fiddler2/

    另外,请确保您的配置良好。见:http://msdn.microsoft.com/en-us/library/ms734663.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 2017-09-04
      • 2023-03-17
      相关资源
      最近更新 更多