簡體   English   中英

使用不同的界面使用Web服務

[英]Consuming a web service using a different interface

我有一個以極其無益的方式構建的wsdl文件。 它是巨大的,在某些情況下是幾兆字節,當我使用各種Visual Studio工具從中生成包裝器時,生成的代碼庫非常大,以至於它會使較弱的機器上的Visual Studio崩潰。 編譯時間是荒謬的,結果類使用的屬性是絕對必要的更動態的訪問模式(即某種索引器)。 服務器端沒有任何更改選項。

wsdl文件遠遠大於手工處理的文件,並且有任意數量的文件。 到目前為止我使用的解決方案是使用反射或后期綁定生成的自動生成的類。 然而,因為我在這里處理的是封裝了基本上是一個客戶端的SOAP消息它將使意義,如果有另一種方式的包裝

本質上,我想創建一個包裝器,它暴露出一個更動態的界面,特別是在涉及字段的地方。 這個任務並不完全是直截了當的,所以我正在尋找有關該做什么的建議,以及各種類,可定制的代碼生成器,WSDL瀏覽器/解析器等,這些都將使這項任務不那么耗時。 我應該構建自己的SOAP客戶端嗎? 我會以什么為基礎? 什么.NET功能可以幫助我完成這項任務?

您可以手工制作支持WebService上可用方法子集的接口,並且不需要生成服務引用。

您必須為方法創建正確的soap簽名,包括dto和名稱空間。 這樣做的缺點是您將被迫手動管理對服務的任何更改。

下面是一個簡單的示例,顯示了使用ISubsetInterface通信創建的代理客戶端以及公開IServiceInterface的服務。 要實現此目的,Name屬性必須與IServiceInterface合約的名稱匹配,在這種情況下,默認為“IServiceInterface”,但您的實現可能需要操作Namespace和Actions。 了解您需要操作的最簡單方法是查看生成的wsdl。

[TestFixture]
public class When_using_a_subset_of_a_WCF_interface
{
    [Test]
    public void Should_call_interesting_method()
    {
        var serviceHost = new ServiceHost(typeof(Service));

        serviceHost.AddServiceEndpoint( typeof(IServiceInterface), new BasicHttpBinding(), "http://localhost:8081/Service" );
        serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;

        serviceHost.Open();

        using( var channelFactory = new ChannelFactory<ISubsetInterface>( new BasicHttpBinding(), "http://localhost:8081/Service") )
        {
            var client = channelFactory.CreateChannel();

            client.InterestingMethod().Should().Be( "foo" );
        }

        serviceHost.Close();
    }

    [ServiceContract]
    interface IServiceInterface
    {
        [OperationContract]
        string InterestingMethod();
        [OperationContract]
        string UninterestingMethod();
    }

    [ServiceContract(Name = "IServiceInterface")]
    interface ISubsetInterface
    {
        [OperationContract]
        string InterestingMethod();
    }

    class Service : IServiceInterface
    {
        public string InterestingMethod() { return "foo"; }

        public string UninterestingMethod() { throw new NotImplementedException(); }
    }
}

我建議使用包含DataContracts的共享程序集。

然后使用ChannelFactory<T>類創建服務器接口的實例。 然后,您可以在沒有任何WSDL的情況下調用服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM