我希望编写一些集成测试来比较 WCF 服务生成的 WSDL 与以前(和发布的)版本。这是为了确保服务契约(Contract)与发布时间没有区别。
我希望我的测试是自包含的,而不依赖于任何外部资源,例如在 IIS 上托管。
我想我可以在测试中重新创建我的 IIS 托管环境,例如......
using (ServiceHost host = new ServiceHost(typeof(NSTest.HelloNS), new Uri("http://localhost:8000/Omega")))
{
host.AddServiceEndpoint(typeof(NSTest.IMy_NS), new BasicHttpBinding(), "Primary");
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
host.Open();
}
有没有其他人有更好的想法?
编辑:
显然,这段代码只是为服务创建了一个主机,我仍然缺少获取 WSDL 定义的客户端代码。
最佳答案
只需在 URL 中使用 WebClient 和 ?wsdl 后缀
使用 (ServiceHost host = new ServiceHost(typeof(NSTest.HelloNS),
new Uri("http://localhost:8000/Omega")))
{
host.AddServiceEndpoint(typeof(NSTest.IMy_NS), new BasicHttpBinding(), "Primary");
ServiceMetadataBehavior 行为 = new ServiceMetadataBehavior();
行为.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
主机.Open();
字符串 wsdl = null;
使用 (WebClient wc = new WebClient())
{
使用 (var stream = wc.OpenRead("localhost:8000/Omega?wsdl"))
{
使用 (var sr = new StreamReader(stream))
{
wsdl = sr.ReadToEnd();
}
}
}
Console.Write(wsdl);
}
关于wcf - 如何以编程方式从 WCF 服务生成 WSDL(集成测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052369/