为自托管服务生成代理

为自托管服务生成代理

本文介绍了无法使用“添加服务引用"为自托管服务生成代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为 MiniCalc 的基本计算器服务,它只有两个操作.Add 和 Mul,并将其托管在控制台应用程序中.

I created a basic calculator service called MiniCalc that only has two operations. Add and Mul, and hosted it in a Console Application.

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 来调用该服务.

Then I created a console application to consume the service and created the proxy manually by creating a proxy class and then created a ChannelFactory to invoke the service.

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

我能够正确调用服务合同并按预期检索结果.

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

Now I wanted to create the proxy using the Add Service Reference.

在添加服务引用"窗口中单击转到"时出现以下错误

I get the following error when I click Go in the Add Service Reference window

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.

我遗漏了什么或做错了什么?

What am I missing or doing wrong?

推荐答案

在 ServiceHost 中启用元数据交换行为.

Enable Metadata exchange behavior in your 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

这篇关于无法使用“添加服务引用"为自托管服务生成代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:45