Contract-First SOA with WCF

后端 未结 5 1163
悲哀的现实
悲哀的现实 2021-01-31 22:12

This question is more of a probe to discover what people are doing in the community, in practical situations, than a specifically targeted question. I have searched pretty broad

5条回答
  •  醉梦人生
    2021-01-31 22:56

    I use a contract-first approach, generally (but not always) using the same type representation at each end.

    Actually, to use WCF you don't need any special proxies etc; you can use your regular .NET types at both ends and not use svcutil.exe at all. Getting a working service is as simple as adding the "ABC" into the configuration file, and using something like:

    public sealed class WcfClient : System.ServiceModel.ClientBase
        where T : class
    {
        public T Service { get { return base.Channel; } }
    }
    

    Now you can use:

    using(var client = new WcfClient()) {
        int i = client.Service.SomeMethod("abc");
    }
    

    and all you have at the client (and server) is your IMyService interface.


    For other tools; protobuf-net is an implementation of Google's "protocol buffers" API, which has a DSL for describing data and services in a "contract first" (and portable/interoperable) way - for example (a .proto file):

    message SearchRequest {
      required string query = 1;
      optional int32 page_number = 2;
      optional int32 result_per_page = 3;
    }
    message SearchResponse {
      repeated string result = 1; 
    }
    service SearchService {
      rpc Search (SearchRequest) returns (SearchResponse);
    }
    

    The protobuf-net tool (which I maintain) includes a "protogen" utility to transform this DSL into C#/VB; and one of the options (for C#, at least - I'd need to check VB) is to emit a full WCF proxy implementation (with your choice of sync or async methods); very similar to svcutil - but (due to the protobuf-net relationship) it includes the custom [ProtoBehavior] attribute on the operation-contracts so that it uses the protobuf-net serializer instead of DataContractSerializer (faster and more efficient, but different).

    For VS integration; I'm working on exactly that (proof).

提交回复
热议问题