Search code examples
wcfazurewcf-bindingazureservicebus

Having two different topics within same windows server service bus namespace in WCF endpoint


I have two endpoints defined pointing to two different service bus topics. With same transportClientEndpointBehavior and on same service.

<endpointBehaviors>
            <behavior name="securityBehavior">

          <transportClientEndpointBehavior>
            <tokenProvider>
              <windowsAuthentication>
                <stsUris>
                  <stsUri value="https://on-permises:9355/Namespace" />
                </stsUris>
              </windowsAuthentication>
            </tokenProvider>
          </transportClientEndpointBehavior>
</endpointBehaviors>

<customBinding>
        <binding name="messagingBinding" >
          <textMessageEncoding messageVersion="None" writeEncoding="utf-8"  >
            <readerQuotas maxStringContentLength="2147483647"/>
          </textMessageEncoding>
          <netMessagingTransport/>
        </binding>
</customBinding>

<endpoint name="endpoint1"
           address="sb://on-permises/Namespace/topic1"
                   listenUri="sb://on-permises/Namespace/topic1/subscriptions/sub"
                   binding="customBinding"
                   bindingConfiguration="messagingBinding"
                   contract="WCFService.IService1"
                   behaviorConfiguration="securityBehavior" />

        <endpoint name="endpoint2"
           address="sb://on-permises/Namespace/topic2"
                   listenUri="sb://on-permises/Namespace/topic2/subscriptions/sub"
                   binding="customBinding"
                   bindingConfiguration="messagingBinding"
                   contract="WCFService.IService2"
                   behaviorConfiguration="securityBehavior" />

Once i run the application, I get the error : System.ArgumentException: The value could not be added to the collection, as the collection already contains an item of the same type: 'Microsoft.ServiceBus.TransportClientEndpointBehavior'. This collection only supports one instance of each type. Parameter name: item

I tried by defining two different endpoint behaviors, but get the same error. any help here will be helpful.


Solution

  • Found the work around solution, the issue was service host was trying to add these two service endpoints in WSDL/Metadata. which was not necessary. So with the help of ServiceMetadataContractBehavior (IContractBehavior ) stopped exposing the WSDL/Metadata .

    Any better approach or correction please let me know.

    public class DisableContractMetadata : Attribute, IContractBehavior
    
        {
    
            public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    
            {
    
            }
    
    
            public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    
            {
    
    
    
            }
    
    
            public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    
            {
    
                /// Here ServiceMetadataContractBehavior type is derived from IContractBehavior
    
                /// MetadataGenerationDisabled property of ServiceMetadataContractBehavior type = flase disables disables exposing WSDL 
    
                contractDescription.ContractBehaviors.Add(new ServiceMetadataContractBehavior() { MetadataGenerationDisabled = true });
    
            }
    
    
            public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    
            {
    
    
    
            }
    
        }