我正在尝试创建一个允许我接收SOAP或RESTful消息的WCF应用程序,并且基本上以相同的方式处理它们。我确实有“工作”的代码,但我不确定它是以最有效的方式实现的,所以我想知道如何更优雅地完成它。
我有以下代码:
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
}
}
但我觉得我的方式错了。我想为此使用继承,但如果没有两个单独的代码集,我似乎无法使它继续工作。