我正在编写一个简单的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"))));
答案 0 :(得分: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时将其删除。