WCF与visual studio 2012

Ahs*_*bal 6 .net c# wcf visual-studio

我是新的WCF编程,我从以下链接开始遵循一系列入门教程

http://msdn.microsoft.com/en-us/library/ms734712.aspx

我在控制台应用程序中托管服务,但当我尝试创建客户端并尝试添加服务引用时,我得到以下异常.

下载'http:localhost:8000/GettingStarted/mex/_vti_bin/ListData.svc/$ metadata'时出错.请求失败,HTTP状态为405:Method Not Allowed.元数据包含无法解析的引用:'http:localhost:8000/GettingStarted/mex'.在http:localhost:8000/GettingStarted/mex中没有可以接受该消息的端点.这通常是由错误的地址或SOAP操作引起的.有关更多详细信息,请参阅InnerException(如果存在).远程服务器返回错误:(404)Not Found.如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用.

托管应用程序的代码

class Program
{
    static void Main(string[] args)
    {
        // Step 1 Create a URI to serve as the base address.
        Uri baseAddress = 
            new Uri("http://localhost:8000/GettingStarted/");

        // Step 2 Create a ServiceHost instance
        ServiceHost selfHost = 
            new ServiceHost(typeof(CalculatorService), baseAddress);

        try
        {
            // Step 3 Add a service endpoint.
            selfHost.AddServiceEndpoint(typeof(ICalculator), 
                new WSHttpBinding(), 
                "CalculatorService");

            // Step 4 Enable metadata exchange.
            var smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            // Step 5 Start the service.
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown.
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("exception: {0}", ce.Message);
            selfHost.Abort();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我无法弄清问题是什么.我正在使用visual studio 2012和.net平台4.5.

iMo*_*ySX 8

我也有类似的问题,弄乱了这个.是的,您似乎已经正确地遵循了教程,但是如果您想要连接到它并作为服务使用(如在服务引用中),您还必须添加MEX服务enpoint.在selfhost.Description.Behaviors.Add(smb)之后添加此行:

selfhost.AddServiceEndpoint(
            typeof(IMetadataExchange),
            MetadataExchangeBindings.CreateMexHttpBinding(),
            "http://localhost:8000/GettingStarted/mex");
Run Code Online (Sandbox Code Playgroud)

这应该使您能够通过"添加服务参考"进行连接.此外,我发现根据您的系统,您可能需要以管理员身份运行VS以允许连接到网络(如果您过去不小心告诉它).