如何以编程方式从 WCF 服务生成 WSDL(集成测试)

发布于 2024-09-05 12:02:26 字数 750 浏览 5 评论 0原文

我希望编写一些集成测试来将 WCF 服务生成的 WSDL 与以前(和已发布)的版本进行比较。这是为了确保服务合同与发布时相同。

我希望我的测试是独立的,不依赖于任何外部资源,例如托管在 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 定义的客户端代码。

I am looking to write some integration tests to compare the WSDL generated by WCF services against previous (and published) versions. This is to ensure the service contracts don't differ from time of release.

I would like my tests to be self contained and not rely on any external resources such as hosting on IIS.

I am thinking that I could recreate my IIS hosting environment within the test with something like...

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();
}

Does anyone else have any better ideas?

EDIT:
Obviously this code is simply creating a host for the service, I am still missing the client code to obtain the WSDL definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

<逆流佳人身旁 2024-09-12 12:02:26

只需在 URL 中使用 WebClient 和 ?wsdl 后缀

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();

    string wsdl = null;
    using (WebClient wc = new WebClient())
    {
        using (var stream = wc.OpenRead("localhost:8000/Omega?wsdl"))
        {
            using (var sr = new StreamReader(stream))
            {
                wsdl = sr.ReadToEnd();
            }
        }
    }
    Console.Write(wsdl);
}

Just use WebClient and ?wsdl sufix in URL

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();

    string wsdl = null;
    using (WebClient wc = new WebClient())
    {
        using (var stream = wc.OpenRead("localhost:8000/Omega?wsdl"))
        {
            using (var sr = new StreamReader(stream))
            {
                wsdl = sr.ReadToEnd();
            }
        }
    }
    Console.Write(wsdl);
}

葮薆情 2024-09-12 12:02:26

查看 WsdlExporter 在 MSDN 上。它用于在WCF中生成wsdl。
您还可以使用 Reflector 查看 svcutil,了解它如何生成 wsdl 信息,因为该工具可以从 dll 文件生成 wsdl。

另一种比较方法是使用 svcutil 工具生成 wsdl 并将其与服务的已保存/基线版本进行比较。在测试中运行 svcutil 并根据旧文件验证输出。不是真正独立的测试,因为您需要 svcutil...

Check out the WsdlExporter on MSDN. Its used to generate wsdl in WCF.
You could also have a look in svcutil with reflector to see how its generating the wsdl information, since the tool can generate wsdl from a dll-file.

Another way to do your comparison would be to use the svcutil tool to generate the wsdl and compare it to a saved/baselined version of the service. Run the svcutil in your test and verify the output against the old files. Not really self-contained test since you'll need the svcutil...

阳光下慵懒的猫 2024-09-12 12:02:26

像这样的事情怎么样?
使用 C# 创建 WSDL

How about something like this?
Creating a WSDL using C#

蓝梦月影 2024-09-12 12:02:26

您需要注意的一件事是比较整个 WSDL。与经典 Web 服务 (asmx) WSDL 不同,WCF 分解了 WSDL。这意味着信息的核心位于 ?WSDL 页面上,但是,还会有多个 XSD (.svc?XSD=XSD0, 1, 2 ...) 以及可能的多个 WSDL 页面(?WSDL 和 ?WSDL=例如 WSDL0)。

实现此目的的一种方法是生成一个 Web 请求以从根 wsdl 获取数据。然后,您可以在 WSDL 中搜索 (yourServicename).svc?WSDL=WSLD0 和 (yourServicename)?XSD=XSD0 等内容,为每个 WSDL 和 XSD 生成额外的 Web 请求。

One thing you need to be careful of is to compare the entire WSDL. WCF breaks up WSDLs, unlike classic web services (asmx) WSDLs. This means that the core of the info is on the ?WSDL page, however, there will also be multiple XSDs (.svc?XSD=XSD0, 1, 2 ...) and possibly multiple WSDL pages (?WSDL and ?WSDL=WSDL0 for example).

One way to accomplish this would be to generate a webrequest to get the data from the root wsdl. Then you can search the WSDL for anything like (yourServicename).svc?WSDL=WSLD0 and (yourServicename)?XSD=XSD0 and so on, spawning additional webrequests for each WSDL and XSD.

久伴你 2024-09-12 12:02:26

您可能最好使用 SoapUI 来测试 WSDL,而不是直接依赖 NUnit。

如果您想从 NUnit 调用 SoapUI,这是可能的,但有点笨拙。请参阅 http://enthusiasm.soapui.org/forum/viewtopic .php?f=2&t=15 了解更多信息。

You might be better off using SoapUI to test the WSDL rather than relying on NUnit directly.

If you want to call SoapUI from NUnit, it's possible, but a little clunky. See http://enthusiasm.soapui.org/forum/viewtopic.php?f=2&t=15 for more information.

路还长,别太狂 2024-09-12 12:02:26

相同的答案翻译成VB

    Using host = New ServiceHost(GetType(MyHelloWorldWcfLib.HelloWorldServer), New Uri("http://localhost:8000/Omega"))

        host.AddServiceEndpoint(GetType(MyHelloWorldWcfLib.IHelloWorld), New BasicHttpBinding(), "Primary")
        Dim behavior = New ServiceMetadataBehavior()
        behavior.HttpGetEnabled = True
        host.Description.Behaviors.Add(behavior)
        host.AddServiceEndpoint(GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex")
        host.Open()

        Dim wsdl As String = Nothing
        Using wc = New System.Net.WebClient()
            Using stream = wc.OpenRead("http://localhost:8000/Omega?wsdl")
                Using sr = New IO.StreamReader(stream)
                    wsdl = sr.ReadToEnd()
                End Using
            End Using
        End Using
        Console.Write(wsdl)
    End Using

Same answer translated to VB

    Using host = New ServiceHost(GetType(MyHelloWorldWcfLib.HelloWorldServer), New Uri("http://localhost:8000/Omega"))

        host.AddServiceEndpoint(GetType(MyHelloWorldWcfLib.IHelloWorld), New BasicHttpBinding(), "Primary")
        Dim behavior = New ServiceMetadataBehavior()
        behavior.HttpGetEnabled = True
        host.Description.Behaviors.Add(behavior)
        host.AddServiceEndpoint(GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex")
        host.Open()

        Dim wsdl As String = Nothing
        Using wc = New System.Net.WebClient()
            Using stream = wc.OpenRead("http://localhost:8000/Omega?wsdl")
                Using sr = New IO.StreamReader(stream)
                    wsdl = sr.ReadToEnd()
                End Using
            End Using
        End Using
        Console.Write(wsdl)
    End Using
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文