WCF consolidate RESTful and SOAP listener

49 views Asked by At

I'm trying to make a single WCF application that will allow me to receive either SOAP or RESTful messages and essentially process them the same way. I do have code that "works" but I'm not sure it's implemented in the most efficient manner, so I'm wondering how this can be accomplished more elegantly.

I have the following code:

class Interfaces
{
    [ServiceContract(Namespace = "myNameSpace")]
    public interface IMessageBrokers_SOAP
    {
        [OperationContract]
        [WebInvoke]
        bool Broker_SOAP_Messages(string source, string destination,Dictionary<string, string> Incoming_Message);
    }

    [ServiceContract(Namespace = "myNameSpace")]
    public interface ImessageBrokers_REST
    {
        [OperationContract, WebGet(ResponseFormat = WebMessageFormat.Xml)]
        bool Broker_REST_Messages();
    }
}

class MessageBrokers : Interfaces.IMessageBrokers_SOAP, Interfaces.ImessageBrokers_REST
{
    public bool Broker_SOAP_Messages(string source, string destination,Dictionary<string, string> Incoming_Values)
    {
        //take the object passed in and pass it along to wholesale to Broker_Message
        return Broker_Message(source, destination, Incoming_Values);
    }

    public bool Broker_REST_Messages()
    {
        //take the query params and put them into dictionary for Broker_Message to use
        var queryParameters = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;
        Dictionary<string, string> Incoming_Values = new Dictionary<string, string>();
        foreach (string key in queryParameters.AllKeys)
        {
            string value = queryParameters[key];
            Incoming_Values.Add(key, value);
        }

        return Broker_Message(queryParameters["source"], queryParameters["destination"], Incoming_Values);

    }

    bool Broker_Message(string source, string destination, Dictionary<string, string> Incoming_Values)
    {
            //do work
    }
}

But I feel like I'm going about this the wrong way. I'd like to use inheritance for this, but I can't seem to get it to work without having two separate code sets.

0

There are 0 answers