NetMessagingBinding始终使用BinaryMessageEncodingBindingElement + NetMessagingTransportBindingElement构建通道堆栈。如果ServiceBus队列/订阅中的BrokeredMessages是普通的[text] xml,那么BinaryMessageEncoding将不起作用,使用WCF您将使用带有TextMessageEncoder和NetMessagingTransportBindingElement的CustomBinding。
总之,您需要使用带有TextMessageEncodingBindingElement(具有MessageVersion = None)的CustomBinding和NetMessagingTransportBindingElement,确保Action =“*”,并在您的ServiceBehavior上设置AddressFilterMode = Any。
下面是使用NetMessagingTransportBindingElement读一个普通的旧XML消息两种方式:
解决方案#1 使用System.ServiceModel.Channels.Message中的ServiceContract和调用Message.GetBody()
namespace MessagingConsole
{
static class Constants {
public const string ContractNamespace = "http://contoso";
}
[DataContract(Namespace = Constants.ContractNamespace)]
class Record
{
[DataMember]
public string Id { get; set; }
}
[ServiceContract]
interface ITestContract
{
[OperationContract(IsOneWay = true, Action="*")]
void UpdateRecord(Message message);
}
[ServiceBehavior(
AddressFilterMode = AddressFilterMode.Any)] // This is another way to avoid “The message with To ” cannot be processed at the receiver…”
class TestService : ITestContract
{
[OperationBehavior]
public void UpdateRecord(Message message)
{
Record r = message.GetBody<Record>();
Console.WriteLine("UpdateRecord called! " + r.Id);
}
}
class ServiceProgram
{
static void Main(string[] args)
{
string solution = "sb://SOMENS";
string owner = "owner";
string key = "XXXXXX=";
string topicPath = "Topic2";
string subscriptionName = "Sub0";
TokenProvider tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(owner, key);
MessagingFactory factory = MessagingFactory.Create(solution, tokenProvider);
TopicClient sender = factory.CreateTopicClient(topicPath);
SubscriptionClient receiver = factory.CreateSubscriptionClient(topicPath, subscriptionName, ReceiveMode.ReceiveAndDelete);
string interopPayload = "<Record xmlns='" + Constants.ContractNamespace + "'><Id>4</Id></Record>";
BrokeredMessage interopMessage = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(interopPayload)), true);
sender.Send(interopMessage);
CustomBinding binding = new CustomBinding(
new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.None },
new NetMessagingTransportBindingElement());
ServiceHost serviceHost = new ServiceHost(typeof(TestService), new Uri(solution));
ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(ITestContract), binding, topicPath + "/Subscriptions/" + subscriptionName);
endpoint.Behaviors.Add(new TransportClientEndpointBehavior(tokenProvider));
serviceHost.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
}
}
}
解决方案#2 定义MessageContract数据类型,来使预期肥皂合同匹配什么互操作的客户端发送:
namespace MessagingConsole
{
static class Constants
{
public const string ContractNamespace = "http://contoso";
}
[DataContract(Namespace = Constants.ContractNamespace)]
class Record
{
[DataMember]
public string Id { get; set; }
}
[MessageContract(IsWrapped=false)]
class RecordMessageContract
{
[MessageBodyMember(Namespace = Constants.ContractNamespace)]
public Record Record { get; set; }
}
[ServiceContract]
interface ITestContract
{
[OperationContract(IsOneWay = true, Action="*")]
void UpdateRecord(RecordMessageContract recordMessageContract);
}
class ServiceProgram
{
static void Main(string[] args)
{
string solution = "sb://SOMENS";
string owner = "owner";
string key = "XXXXXXXXXXXXXX=";
string topicPath = "Topic2";
string subscriptionName = "Sub0";
TokenProvider tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(owner, key);
MessagingFactory factory = MessagingFactory.Create(solution, tokenProvider);
TopicClient sender = factory.CreateTopicClient(topicPath);
SubscriptionClient receiver = factory.CreateSubscriptionClient(topicPath, subscriptionName, ReceiveMode.ReceiveAndDelete);
string interopPayload = "<Record xmlns='" + Constants.ContractNamespace + "'><Id>5</Id></Record>";
BrokeredMessage interopMessage = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(interopPayload)), true);
sender.Send(interopMessage);
CustomBinding binding = new CustomBinding(
new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.None },
new NetMessagingTransportBindingElement());
ServiceHost serviceHost = new ServiceHost(typeof(TestService), new Uri(solution));
ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(ITestContract), binding, topicPath + "/Subscriptions/" + subscriptionName);
endpoint.Behaviors.Add(new TransportClientEndpointBehavior(tokenProvider));
serviceHost.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
}
}
[ServiceBehavior(
AddressFilterMode = AddressFilterMode.Any
)]
class TestService : ITestContract
{
[OperationBehavior]
public void UpdateRecord(RecordMessageContract recordMessageContract)
{
Record r = recordMessageContract.Record;
Console.WriteLine("UpdateRecord called! " + r.Id);
}
}
}