Microsoft出现问题';的WCF教程

本文关键字:WCF 教程 问题 Microsoft | 更新日期: 2023-09-27 18:24:33

我正在努力遵循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;
            }
        }
    }
}

Microsoft出现问题';的WCF教程

第一个错误是您有一个拼写错误的localhost(您有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