0

我正在编写一个简单的 WCF 服务并将其托管在控制台应用程序中。

我知道服务正在运行,因为一个简单的客户端可以执行服务上公开的操作。

如果我将 Wcf 测试客户端指向我的服务,我会收到以下错误(注意 tempuri.com 实际上是 localhost,但 stackoverflow 要求我将此输出包装为代码块或包含 FQDN):

System.InvalidOperationException:元数据包含无法解析的引用:“ http://tempuri.com:27198/UsingWindsor.svc?wsdl ”。----> System.ServiceModel.ProtocolException : 内容类型 application/soap+xml; 服务http://tempuri.com:27198/UsingWindsor.svc?wsdl不支持 charset=utf-8 。客户端和服务绑定可能不匹配。----> System.Net.WebException:远程服务器返回错误:(415)无法处理消息,因为内容类型'application/soap+xml; charset=utf-8' 不是预期的类型 'text/xml; 字符集=utf-8'..

不完全理解错误,我开始使用作为 WcfFacility 一部分的测试。

我在 Castle.Facilities.WcfIntegration.Tests.ServiceHostFixture 中修改了一个测试,以显示我遇到的相同错误(当然它无需修改即可工作):

[Test]
public void CanPubishMEXEndpointsUsingDefaults()
{
    using (new WindsorContainer()
                .AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
                .Register(Component.For<Operations>()
                    .DependsOn(new { number = 42 })
                    .AsWcfService(new DefaultServiceModel()
                        .AddBaseAddresses(
                            //"net.tcp://localhost/Operations",
                            "http://localhost:27198/UsingWindsor.svc")
                        .AddEndpoints(
                            WcfEndpoint.ForContract<IOperations>()
                                //.BoundTo(new NetTcpBinding { PortSharingEnabled = true })
                                .BoundTo(new BasicHttpBinding())
                            )
                        .PublishMetadata(mex => mex.EnableHttpGet())
                    )
                ))
            {
                //var tcpMextClient = new MetadataExchangeClient(new EndpointAddress("net.tcp://localhost/Operations/mex"));
                //var tcpMetadata = tcpMextClient.GetMetadata();
                //Assert.IsNotNull(tcpMetadata);

                var httpMextClient = new MetadataExchangeClient(new EndpointAddress("http://localhost:27198/UsingWindsor.svc?wsdl"));
                var httpMetadata = httpMextClient.GetMetadata();
                Assert.IsNotNull(httpMextClient);
            }
        }

为什么当我消除 NetTcp 绑定并绑定到 Http 时,此测试会失败?我在控制台应用程序中的配置与修改后的测试非常相似。(包括完整性)

container.Register(Component.For<ISimpleService>()
                        .ImplementedBy<SimpleService>()
                        .AsWcfService(new DefaultServiceModel()
                                            .AddBaseAddress("http://localhost:515")
                                            .PublishMetadata(x => x.EnableHttpGet())
                                            .AddEndPoints(WcfEndpoint.ForContract<ISimpleService>().BoundTo(new BasicHttpBinding()).At("SimpleService"))));
4

1 回答 1

0

不太清楚为什么测试失败,但我确实解决了我的问题。

我从这里更改了注册:

container.Register(Component.For<ISimpleService>()
                        .ImplementedBy<SimpleService>()
                        .AsWcfService(new DefaultServiceModel()
                                            .AddBaseAddress("http://localhost:515")
                                            .PublishMetadata(x => x.EnableHttpGet())
                                            .AddEndPoints(WcfEndpoint.ForContract<ISimpleService>().BoundTo(new BasicHttpBinding()).At("SimpleService"))));

对此:

container.Register(Component.For<ISimpleService>()
                        .ImplementedBy<SimpleService>()
                        .AsWcfService(new DefaultServiceModel()
                                            .AddBaseAddress("http://localhost:515/SimpleService")
                                            .PublishMetadata(x => x.EnableHttpGet())
                                            .AddEndPoints(WcfEndpoint.ForContract<ISimpleService>().BoundTo(new BasicHttpBinding()))));

在定义 BaseAddress 时,我现在包含“SimpleService”并在定义 EndPoints 时将其删除。

于 2013-11-26T16:16:24.683 回答