WebService で使用可能なメソッドのサブセットをサポートするインターフェイスを手作りして、サービス参照を生成する必要をなくすことができます。
dto や名前空間を含むメソッドの適切な SOAP シグネチャを作成する必要があります。これの欠点は、サービスへの変更を手動で管理しなければならないことです。
ISubsetInterface
を公開するサービスとの通信で作成されたプロキシ クライアントを示す簡単な例を次に示しますIServiceInterface
。これを実現するには、Name プロパティが IServiceInterface コントラクトの名前と一致する必要があります。この場合、デフォルトは "IServiceInterface" ですが、実装では名前空間とアクションの操作が必要になる場合があります。何を操作する必要があるかを知る最も簡単な方法は、生成された 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(); }
}
}