How to design and implement a simple WCF service relay?

后端 未结 3 628
陌清茗
陌清茗 2020-12-18 11:18

We are in the process of designing a simple service-oriented architecture using WCF as the implementation framework. There are a handful of services that a few applications

相关标签:
3条回答
  • 2020-12-18 11:46

    It seems it is sufficient to build an interface as follows:

    [OperationContract(Action="*", ReplyAction="*")]
    Message CallThis(Message msg);
    

    I also found this post useful for implementing the CallThis method by "fiddling" with Message objects. A basic implementation of the CallThis method follows:

    public Message CallThis(Message message) {
        MessageBuffer buffer = message.CreateBufferedCopy(524288);
        Message output = buffer.CreateMessage();
        output.Headers.To = <INTERNAL_SERVICE_URI>;
    
        BasicHttpBinding binding = new BasicHttpBinding();
        IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(<INTERNAL_SERVICE_URI>);
        factory.Open();
    
        IRequestChannel channel = factory.CreateChannel(new EndpointAddress(<INTERNAL_SERVICE_URI>));
        channel.Open();
    
        Message result = channel.Request(output);
    
        message.Close();
        output.Close();
        factory.Close();
        channel.Close();
    
        return result;
    }
    

    Adding authentication and authorization should be quite straightforward.

    0 讨论(0)
  • 2020-12-18 11:49

    I have done something very similar to this. What you can do is expose an endpoint with a single operation.

    That operation would look something like

    [OperationContract(Namespace="www.fu.com", Action="*")]
    void CallThis(Message msg);
    

    Have your clients use a proxy that is intended for the service they intended to calling the operation they want. Then have them change the configuration to point to your endpoint/service. The "CallThis" method will accept any operation, regardless of its signature. The Message parameter is the WCF Message.

    Do what you need to do to determine where things are supposed to go, but you will need to change the "To" to go to your internal endpoint.

    I actually have a full implementation of this, so if you have questions, let me know.

    Joe.

    0 讨论(0)
  • 2020-12-18 11:50

    Check out this SO question where one of the responses suggests .NET 4's RoutingService. Very nice addition to WCF.

    0 讨论(0)
提交回复
热议问题