WCF整合RESTful和SOAP侦听器

WCF整合RESTful和SOAP侦听器,wcf,rest,soap,solid-principles,Wcf,Rest,Soap,Solid Principles,我正在尝试创建一个WCF应用程序,它允许我接收SOAP或RESTful消息,并且基本上以相同的方式处理它们。我确实有“有效”的代码,但我不确定它是否以最有效的方式实现,所以我想知道如何更优雅地完成这项工作 我有以下代码: class Interfaces { [ServiceContract(Namespace = "myNameSpace")] public interface IMessageBrokers_SOAP { [OperationContr

我正在尝试创建一个WCF应用程序,它允许我接收SOAP或RESTful消息,并且基本上以相同的方式处理它们。我确实有“有效”的代码,但我不确定它是否以最有效的方式实现,所以我想知道如何更优雅地完成这项工作

我有以下代码:

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
    }
}
类接口
{
[ServiceContract(Namespace=“myNameSpace”)]
公共接口IMessageBrokers_SOAP
{
[经营合同]
[WebInvoke]
bool Broker_SOAP_消息(字符串源、字符串目标、字典传入_消息);
}
[ServiceContract(Namespace=“myNameSpace”)]
公共接口ImessageBrokers\u REST
{
[OperationContract,WebGet(ResponseFormat=WebMessageFormat.Xml)]
bool Broker_REST_Messages();
}
}
MessageBrokers类:Interfaces.IMessageBrokers\u SOAP、Interfaces.IMessageBrokers\u REST
{
public bool Broker\u SOAP\u消息(字符串源、字符串目标、字典传入的\u值)
{
//获取传入的对象,并将其传递给批发到代理的消息
返回代理_消息(源、目标、传入_值);
}
公共bool Broker_REST_Messages()
{
//获取查询参数并将其放入字典中,以供Broker_消息使用
var queryParameters=WebOperationContext.Current.IncomingRequest.UriTemplateMatch.queryParameters;
字典传入值=新字典();
foreach(queryParameters.AllKeys中的字符串键)
{
字符串值=查询参数[key];
输入值。添加(键、值);
}
返回代理_消息(queryParameters[“source”]、queryParameters[“destination”]、传入_值);
}
bool Broker_消息(字符串源、字符串目标、字典传入_值)
{
//工作
}
}

但我觉得我走错了方向。我想使用继承来实现这一点,但如果没有两个单独的代码集,我似乎无法让它工作。

为什么对
Broker\u*\ u Message有两个单独的定义?为什么不只是一个实现?您在接口中的方法定义上放置的属性以及公开的端点应满足您的需要,除非有设计原因/限制强制不同的方法实现。如果我正确理解您的问题。。我这样实现它是因为我想不出其他方法来处理包含dicitonary的SOAP消息,而不是包含作为查询参数传递的KVP的REST消息。。我想这就是我要问的。。有没有更好的方法来实现这一点?如果方法实现需要在两者之间有所不同,可以将它们都放在同一个接口中,只需使用适当的属性(取决于您想要的是SOAP还是REST)来修饰方法。如果SOAP和REST方法确实不同,那么您需要在接口中定义单独的方法,但不需要每个方法都有一个接口。然后你只需要让你的服务实现这个接口,你能给我举个例子吗。。我很确定这就是我一直想做的。。但到目前为止,我只能让接口处理其中一个。