The rabbitmq. Net Client extends WCF through the custom binding and transport binding element, enabling it to be used on the amqp protocol. In WCF, as a series of binding elements stacks, binding controls most aspects of message transmission, such as security, message format, and transactions. The transport binding element in binding transmits the binding element, which specifies the communication protocol between the server and the client. Rabbitmq. the binding provided by the net client enables us to conveniently implement one-way, two-way (Request/reply), and duplex (asynchronous callback) on the amqp protocol) this common communication method provides reliable and transactional transmission. We can useCodeAnd Traditional WCF configuration files.
ABC of WCF
Address
The address using rabbitmq binding must use the following format: serviceaddress = "Soap. amqp: //" servicename, for example, soap. amqp: // myservice. It uses direct exchange. Other types of exchange do not seem to be supported.
It is not recommended to use IIS as the host. You should use system. servicemodel. servicehost to host and rabbitmq server. You can use the code or config file to configure the address.
Binding
The binding used by rabbitmq is not provided in the. NET client rabbitmq. client, but in the new DLL rabbitmq. servicemodel. dll. You can get it from the official website or nuget. Therefore, when referencing rabbitmq. Client. dll, you must also reference rabbitmq. servicemodel. dll. When defining in the configuration file, you must configure the extension of binding.
We configure the following in the configuration file:
<Bindings>
<Rabbitmqbinding>
<Binding name ="Rabbitmqconfig"
Protocolversion ="Amqp_0_9_1"
Hostname ="Localhost"
Port ="5672"/>
</Rabbitmqbinding>
</Bindings>
<Extensions>
<Bindingextensions>
<Add
Name ="Rabbitmqbinding"
Type ="Rabbitmq. servicemodel. rabbitmqbindingsection, rabbitmq. servicemodel, version = 1.0.110.0, culture = neutral, publickeytoken = NULL"/>
</Bindingextensions>
</Extensions>
For details about binding in rabbitmq. servicemodel. dll, refer to the official documentation for binding elements that are used and available by default.
Contract
The definition of contract is the same as that of the original WCF, and there is no difference. With servicecontractattribute and operationcontractattribute, you can:
[Servicecontract]
Public InterfaceIcalculator
{
[Operationcontract]
IntAdd (IntX,IntY );
[Operationcontract]
IntSubtract (IntX,IntY );
}
If you want to use behavior in the implementation of a specific service, it can be the same as before:
[Servicebehavior (instancecontextmode = instancecontextmode. percall)]
Public Sealed ClassCalculator: icalculator
{
Public IntAdd (IntX,IntY)
{
ReturnX + Y;
}
Public IntSubtract (IntX,IntY)
{
ReturnX-y;
}
}
Service
The definition of a service is the same as that of a commonly used WCF. The configuration file definition and Code definition are also the same. Host should use system. servicemodel. servicehost. Servicehost must define a base address or a complete endpoint address, which is in the soap. amqp format. The endpoint must be added using rabbitmqbinding.
Service =NewServicehost (
Typeof(Calculator ),
NewUri ("Soap. amqp :///"));
Service. addserviceendpoint (
Typeof(Icalculator ),
NewRabbitmqbinding (
"Localhost",
5672,
"Guest",
"Guest",
"/",
8192,
Protocols. amqp_0_9_1 ),
"Calculator");
The complete server configuration file is defined as follows:
<System. servicemodel>
<Services>
<Service name ="Esbtest. wcfrabbitmq. consoleserverperformance. rabbitmqserver">
<Host>
<Baseaddresses>
<Add baseaddress ="Soap. amqp :///"/>
</Baseaddresses>
</Host>
<Endpoint
Address ="Esbtest. wcfrabbitmq. consoleserverperformance"
Binding ="Rabbitmqbinding"
Bindingconfiguration ="Rabbitmqconfig"
Contract ="Esbtest. Contracts. irequest"/>
</Service>
</Services>
<Bindings>
<Rabbitmqbinding>
<Binding name ="Rabbitmqconfig"
Hostname ="Localhost"
Port ="5672"
Protocolversion ="Amqp_0_9_1"
Oneway ="False"/>
</Rabbitmqbinding>
</Bindings>
<Extensions>
<Bindingextensions>
<Add
Name ="Rabbitmqbinding"
Type ="Rabbitmq. servicemodel. rabbitmqbindingsection, rabbitmq. servicemodel, version = 1.0.110.0, culture = neutral, publickeytoken = NULL"/>
</Bindingextensions>
</Extensions>
</System. servicemodel>
Server Load balancer can also be used to enable multiple servers.
Client
The definition of the client is the same as that of commonly used WCF. It is recommended that you inherit from clientbase <t> or duplexclientbase <t>. For duplex clients, instancecontext must be specified.
The complete client configuration file is defined as follows:
<System. servicemodel>
<Client>
<Endpoint address ="Soap. amqp: // esbtest. wcfrabbitmq. leleserverperformance"
Binding ="Rabbitmqbinding"
Bindingconfiguration ="Rabbitmqconfig"
Contract ="Esbtest. Contracts. irequest"
Name ="Client"/>
</Client>
<Bindings>
<Rabbitmqbinding>
<Binding name ="Rabbitmqconfig"
Protocolversion ="Amqp_0_9_1"
Hostname ="Localhost"
Port ="5672"/>
</Rabbitmqbinding>
</Bindings>
<Extensions>
<Bindingextensions>
<Add
Name ="Rabbitmqbinding"
Type ="Rabbitmq. servicemodel. rabbitmqbindingsection, rabbitmq. servicemodel, version = 1.0.110.0, culture = neutral, publickeytoken = NULL"/>
</Bindingextensions>
</Extensions>
</System. servicemodel>
We can see that the provided WCF binding is only some simple and common functions, and some other good functions of rabbitmq are not provided. Therefore, using WCF or directly using the client depends on the business and system environment. If you only use some simple functions or the entire system is based on WCF, use WCF for rabbitmq, it will provide you with a fast and consistent development model. However, if the application scenario is complex and the performance requirements are high, the. NET client API is ideal. By comparing the client API with WCF, in terms of speed, it takes about seven times the time for directly using the API, which is much slower. We believe this is foreseeable.
simple sample code is downloaded here. Is one-way.