CoreWCF - RabbitMQ:合同需要双向绑定

bvk5enib  于 2023-08-05  发布在  RabbitMQ
关注(0)|答案(1)|浏览(119)

我正在执行Launch Announcement Blog for RabbitMQ support in CoreWCF中的代码,但不确定如何处理此异常:

System.InvalidOperationException
  HResult=0x80131509
  Message=Contract requires TwoWay (either request-reply or duplex), but Binding 'RabbitMqBinding' doesn't support it or isn't configured properly to support it.
  Source=System.ServiceModel.Primitives
  StackTrace:
   at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)
   at System.ServiceModel.ChannelFactory.OnOpening()
   at System.ServiceModel.Channels.CommunicationObject.<System-ServiceModel-IAsyncCommunicationObject-OpenAsync>d__88.MoveNext()
   at System.ServiceModel.Channels.CommunicationObject.<OpenAsyncInternal>d__87.MoveNext()
   at System.ServiceModel.ChannelFactory.EnsureOpened()
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)

字符串
我使用CoreWCF Reademe创建了我的服务:

dotnet new --install CoreWCF.Templates 
dotnet new corewcf --name MyService


这是我的契约:

[ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

如何更改合约以支持RabbitMQ绑定?

我确实尝试过更改合约,使其只有void方法,因此不需要双向通信,但我仍然得到异常。
对于其他上下文,这是无法创建通道的代码:

public static TContract CreateChannelOnRabbitMq<TContract>()
{
    var endpointAddress = new System.ServiceModel.EndpointAddress(Uri);
    var rabbitMqBinding = new CoreWCF.ServiceModel.Channels.RabbitMqBinding
    {
        SslOption = new SslOption
        {
            ServerName = Uri.Host,
            Enabled = true
        },
    };
    var factory = new ChannelFactory<TContract>(rabbitMqBinding, endpointAddress);
    
    factory.Credentials.UserName.UserName = Credentials.UserName;
    factory.Credentials.UserName.Password = Credentials.Password;

    var channel = factory.CreateChannel();
    
    (channel as System.ServiceModel.Channels.IChannel)?.Open();

    return channel;
}

du7egjpx

du7egjpx1#

原来,我需要将操作合同标记为单向:

System.ServiceModel.ServiceContract]
    [CoreWCF.ServiceContract]
    public interface IService
    {
        [System.ServiceModel.OperationContract(IsOneWay = true)]
        [CoreWCF.OperationContract(IsOneWay = true)]
        void SendData(int value);
    }

字符串
一旦标记正确,我就可以从客户端发送消息并在服务器上接收它们。

相关问题