WCF与Visual Studio 2012

问题描述:

我新的WCF编程,我也跟着系列入门教程从下面的链接WCF与Visual Studio 2012

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

我在控制台应用程序托管服务,但是当我试图创建一个客户端和试图添加服务参考我有以下例外。托管应用

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(); 
     } 
    } 
} 

There was an error downloading 'http: localhost:8000/GettingStarted/mex/_vti_bin/ListData.svc/$metadata'. The request failed with HTTP status 405: Method Not Allowed. Metadata contains a reference that cannot be resolved: 'http: localhost:8000/GettingStarted/mex'. There was no endpoint listening at http: localhost:8000/GettingStarted/mex that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. The remote server returned an error: (404) Not Found. If the service is defined in the current solution, try building the solution and adding the service reference again.

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

+0

首先您是否能够成功托管一项服务?你能分享你的控制台应用程序的输出吗? – Praburaj

我有一个类似的问题还有,这个搞乱。是的,你似乎已经正确地遵循了教程,但是如果你想连接它并作为一个服务使用(就像做一个服务引用一样),你还必须添加MEX服务enpoint。加入这一行你selfhost.Description.Behaviors.Add(SMB)后:

selfhost.AddServiceEndpoint(
      typeof(IMetadataExchange), 
      MetadataExchangeBindings.CreateMexHttpBinding(), 
      "http://localhost:8000/GettingStarted/mex"); 

这应该允许您通过“添加服务引用”进行连接。另外,我发现取决于你的系统,你可能需要以管理员身份运行VS以允许连接到网络(以防过去不小心告知它)。

+0

我有同样的错误,我怎样才能通过app.config而不是代码来配置它? – 2014-07-02 18:57:57

+0

我有同样的问题。 Ijust检查了服务的配置,并且我有这一行这是MEX服务端点吗? –

+0

SoapUI在没有任何问题的情况下可以发现服务,为什么会这样?只有当我尝试使用VS2015的这个(VS2015项目)服务时,我遇到了这个问题。但是SoapUI可以发现没有任何问题。 –

从错误消息判断,似乎没有监听指定端口的服务。当您尝试添加服务引用时,您需要拥有运行该服务的控制台应用程序。

+0

嗨Thanx回复,控制台应用程序正在运行,但仍然相同的错误 –

+0

也请确保HttpGetEnabled在您的服务中打开。否则您的客户将无法获得服务合同。 –

显然,该服务没有运行,这意味着没有端点侦听您用于创建服务引用的URL。

您可以在IIS中托管该服务,或保持控制台应用程序以上述Damir的形式运行。

确保您的服务器在您尝试访问它时运行。还要检查服务器上的配置,并确保客户端的端点与服务器的端点匹配。确保你在使用相同的绑定。 确保服务器正在侦听并且服务器的防火墙未阻止您。 如果您对WCF服务进行了更改,请不要忘记重新生成客户端应用程序的服务引用。

+0

嗨控制台应用程序正在运行,当我试图添加服务参考 –

您确定您已经定义了一个MEX端点吗?这是提供有关您的服务的元数据信息,以便工作室可以生成客户端代理。

在您链接到的教程,这是this bit

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

是啊它是在那里在托管应用程序,因为你可以看到我编辑了这个问题,它包括托管应用程序的代码,我应该也发布app.config? –

如果你正在主持在IIS的Web服务,为您在web.config中(下行为部分)

httpsGetEnabled设置为True

我今天遇到了类似的问题。但是,对于我来说,我并不需要明确添加端点,就像@iMortalitySX已经说过的那样。

我有一个不同的原因:我绑定到http://0.0.0.0,认为听IP不重要。事实上,通过SoapUI我能够连接和使用服务。但是,当试图在另一个Visual Studio项目中发现服务时,发现将失败,因为VS会得到初始响应,但随后会跟进包含http://0.0.0.0的链接,然后失败。

因此,将http://0.0.0.0更改为我的机器的正确IP修复了我的问题。

请尝试把uri地址放入您的浏览器。在我的情况下,我能够看到一个ExceptionDetail。