Saturday, 1 September 2012

How to Publish WCF Service metadata Multiple Contracts?



Publishing Metadata in WCF:

Generally When we executing our .net program CLR requires metadata to execute.like client execute the service then also require metadata which is exposed to client in 2 ways

for http bindings :

<ServiceDebug>
GetHttp="ture"
</ServiceDebug>
 or
 mex endpoints:

mexHttpbinding, mexTcpbinding....ect
A single service can implement multiple contracts with multiple bindings?
Yes, we can implement a single service(IService1.cs)
Like
Public class Service1: IService1,IEmployee,IManager.

Every contract can have their apporiate binding mechanisms.

IService-àWshttpbinding
IEmployeeàbasicHttpbinding
IManageràNetTcpBinding.

Note:
Any two contracts can share same ListenUri.whenever they have same binding.
Like
IService, IEmployee--àWShttpbinding

EX:
<baseAddresses>
<addbaseAddress = "http://localhost:8732/WCFMultiContractsDiffBindings/Service1/" />
</baseAddresses>

They can share single binding instance.
<endpointaddress =""binding="wsHttpBinding"contract="WCFMultiContractsDiffBindings.IService1">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<endpointaddress =""binding="wsHttpBinding "contract="WCFMultiContractsDiffBindings.IEmployee">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>


But coming to different binding then we required to give relavent address to their base address.

EX:

IService, IEmployeeàbasicHttpbinding

<baseAddresses>
<addbaseAddress = "http://localhost:8732/WCFMultiContractsDiffBindings/Service1/" />
</baseAddresses>

<endpointaddress =""binding="wsHttpBinding"contract="WCFMultiContractsDiffBindings.IService1">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<endpointaddress ="/second"binding="basicHttpBinding"contract="WCFMultiContractsDiffBindings.IEmployee">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>

Here Single Registered Base address is enough for both bindings
Basichttpbinding, and wshttpbinding.

So the registered schema is http.


Error:

System.InvalidOperationException: A binding instance has already been associated to listen URI 'http://localhost:8732/Design_Time_Addresses/WCFMultiContractsDiffBindings/Service1/'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config.

Incase of NetTcpBinding require anrelavent registered schema of [net.tcp].

So require anrelavent base address of net.tcp.

<baseAddresses>
<addbaseAddress = "http://localhost:8732/WCFMultiContractsDiffBindings/Service1/" />
<addbaseAddress = "net.tcp://localhost:8733/WCFMultiContractsDiffBindings/Service1/" />
</baseAddresses>
<endpointaddress =""binding="wsHttpBinding"contract="WCFMultiContractsDiffBindings.IService1">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<endpointaddress ="/second"binding="basicHttpBinding"contract="WCFMultiContractsDiffBindings.IEmployee">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<endpointaddress ="/tcpbinds"binding="netTcpBinding"contract="WCFMultiContractsDiffBindings.IManager">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
Otherwise error: System.InvalidOperationException: Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].at System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding, UriSchemeKeyedCollectionbaseAddresses).



Coming to publishing metadata..
We can publish service-metadata in wcf 2 ways:
1.       Using ServiceBehaviour of service

<serviceMetadatahttpGetEnabled="true"/> or httpsGetEnabled=”true”

Using this service metadata client can able to consume the service. With the help of WSDL.


2.       Using Relavent-MexEndpoint.

What is Relavent-MexEndpoint?
To publish or expose metadata of a service to clients wcf provides their apporiate binding expoints.
Like
mexHttpBinding-àbasicHttpbinding, WsHttpBinding,DualHttpbinding..ect
mexTcpBindingànetTcpBinding,…ect.

why relavent address: or <endpoint address="relavent address" bidning="",contract="" />
because
single service instance can't used by multiple objects of different bindings:
 wsHttpbinding, basicHttpbding..ect

No comments:

Post a Comment