2012-03-24 118 views
0

我创建了一个名为MiniCalc的基本计算器服务,该服务只有两个操作。添加和Mul,并将其托管在控制台应用程序中。无法使用“添加服务参考”生成自托管服务的代理

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service), 
             new Uri("http://localhost:8091/MiniCalcService"))) 
{ 
    host.AddServiceEndpoint(typeof(MiniCalcService.IService), 
          new BasicHttpBinding(), 
          "Service"); 
    host.Open(); 
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . "); 
} 

然后创建了一个控制台应用程序来消费服务和通过创建一个代理类手动创建的代理,然后创建的ChannelFactory来调用服务。

EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService/Service"); 
IService proxy = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(),ep); 

我能够正确调用服务契约并按预期检索结果。

现在我想使用Add Service Reference创建代理。

我收到以下错误,当我在添加服务引用窗口

There was an error downloading 'http://localhost:8091/MiniCalcService/Service'. 
The request failed with HTTP status 400: Bad Request. 
Metadata contains a reference that cannot be resolved: 'http://localhost:8091/MiniCalcService/Service'. 
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:8091/MiniCalcService/Service. The client and service bindings may be mismatched. 
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.. 
If the service is defined in the current solution, try building the solution and adding the service reference again. 

我缺少什么或者做错了点击Go?

+0

当您访问.SVC在浏览器中它显示了你的服务没有提供有关它的信息如何使用一些例如什么? – 2012-03-24 13:18:52

+0

没有.svc文件。我只创建了一个Service.cs及其对应的IService.cs。请不要介意我的无知,但.svc文件是必要的,我们是否应该使用工具来生成代理并使用该服务? – Animesh 2012-03-24 13:25:35

+0

无知是如果你从未问过。没有人知道一切。在某些时候,你需要从零开始。看到我的答案。 – 2012-03-24 13:50:27

回答

3

在您的ServiceHost中启用元数据交换行为。

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service), 
          new Uri("http://localhost:8091/MiniCalcService"))) 
{ 
    host.AddServiceEndpoint(typeof(MiniCalcService.IService), 
          new BasicHttpBinding(), 
          "Service"); 

    //Enable metadata exchange 
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
    smb.HttpGetEnabled = true; 
    host.Description.Behaviors.Add(smb); 

    host.Open(); 
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . "); 
} 

http://wcftutorial.net/WCF-Self-Hosting.aspx

+0

minmin,我已经添加了上面几行来启用元数据交换,但是在尝试添加服务引用时仍然出现相同的错误。 – Animesh 2012-03-24 13:41:53

+0

minmin,你的答案适合我。我的错误是认为端点和地址是一样的。所以URL:'http:// localhost:8091/MiniCalcService/Service'是端点,应该在手动代理中使用,正如我在本文中描述的那样,并且URL:'http:// localhost:8091/MiniCalcService'是地址,并应在“添加服务参考”窗口中使用。 – Animesh 2012-03-24 17:31:37

1

既然你没有.SVC我想你一定在你的服务的.config有这样的:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"> 
     <serviceActivations> 
      <add relativeAddress="Service.svc" service="MiniCalcService.Service" /> 
     </serviceActivations> 
    </serviceHostingEnvironment> 

之后,你需要一个选项,允许服务元数据:

<serviceMetadata httpGetEnabled="true" /> 

这有点复杂所以我劝你tu创建一个新的WCF S在一个新的解决方案中,你可以看到这个配置是怎样的。所以你只需要做一些复制/粘贴配置。

该点之后:

http://localhost:8091/MiniCalcService/Service.svc

+0

Vitor,是否有可能在代码而不是配置上实现上述功能?我问这是因为我还没有app.config文件。 – Animesh 2012-03-24 16:17:08

+0

您可以轻松添加一个app.config文件 - >添加 - >应用程序配置文件。如果你不想要这个配置,我想你可以用@minmin写的这样的方式达到这个目的。 – 2012-03-24 16:40:19

+0

我现在已经用minmin的代码实现了我想要的。我在他的回答下发布了一个解释。谢谢您的帮助。 – Animesh 2012-03-24 17:32:37

相关问题