Dynamically updating WCF endpoint addresses in WSDL meta data
Join the DZone community and get the full member experience.
Join For Freeintroduction
i have already written some blog articles about wcf and how flexible and extensible it is. a lot of wcf its internal components can be extended or replaced by custom, user developed implementations. not surprisingly, we can also hook into the wsdl meta data generation.
recently i was in a situation were i had to develop a self hosted wcf service, but i did not know the target hosting server(s) address(es) upfront and it was impossible for me to change the address in the app.config before deployment. because wcf requires an absolute address for self hosted services, i had to use localhost. this works fine and gives no issues when calling the service from another computer, but it has one important drawback: wcf will use the localhost address in the generated wsdl meta data. in order to fix this, i created a custom wcf behavior.
iwsdlexportextension
we can hook into the wsdl meta data generation by implementing the interface iwsdlexportextension . this interface contains 2 methods: exportcontract and exportendpoint. if the class also implements icontractbehavior, the method exportcontract will be invoked by the wcf pipeline and vice versa for iendpointbehavior and exportendpoint.
because i only wanted to modify the endpoint addresses, i only had to implement the interface iendpointbehavior and the method exportcontract. i wanted this also as a service behavior, so i also implemented the interface iservicebehavior and i also extended the behavior from attribute. this way, the behavior can be easily injected into the wcf pipeline by annotating the service implementation class with the behavior.
the modification itself is very simple; every occurrence of localhost in the endpoint addresses is replaced by the dns name of the host machine.
public class hostnameaddressbehavior : attribute, iwsdlexportextension, iendpointbehavior, iservicebehavior { private readonly regex addressregex; private readonly string hostname; public hostnameaddressbehavior() { hostname = dns.gethostname(); addressregex = new regex(@"^(?<scheme>[a-z\.]+)://localhost(?<path>.*)", regexoptions.ignorecase); } public void addbindingparameters(serviceendpoint endpoint, bindingparametercollection bindingparameters) { } public void applyclientbehavior(serviceendpoint endpoint, clientruntime clientruntime) { } public void applydispatchbehavior(serviceendpoint endpoint, endpointdispatcher endpointdispatcher) { } public void validate(serviceendpoint endpoint) { } public void addbindingparameters(servicedescription servicedescription, servicehostbase servicehostbase, collection<serviceendpoint> endpoints, bindingparametercollection bindingparameters) { // if we are added as service behavior, add us to all the endpoints foreach (serviceendpoint endpoint in endpoints) { endpoint.behaviors.add(this); } } public void applydispatchbehavior(servicedescription servicedescription, servicehostbase servicehostbase) { } public void validate(servicedescription servicedescription, servicehostbase servicehostbase) { } public void exportcontract(wsdlexporter exporter, wsdlcontractconversioncontext context) { } public void exportendpoint(wsdlexporter exporter, wsdlendpointconversioncontext context) { // overwrite the address in the service meta data endpointaddress address = context.endpoint.address; string absoluteuri = address.uri.absoluteuri; match m = addressregex.match(absoluteuri); if (m.success) { string scheme = m.groups["scheme"].value; string path = m.groups["path"].value; // update base address uri newabsoluteuri = new uri(string.format("{0}://{1}{2}", scheme, hostname, path)); context.endpoint.address = new endpointaddress(newabsoluteuri, address.identity, address.headers, address.getreaderatmetadata(), address.getreaderatextensions()); } } }
if we also want to add this behavior using the config file, we have to create a behaviorextensionelement.
public class hostnameaddressbehaviorextension : behaviorextensionelement { public override type behaviortype { get { return typeof(hostnameaddressbehavior); } } protected override object createbehavior() { return new hostnameaddressbehavior(); } }
now the behavior can be added declarative as endpoint behavior or service behavior using the config file:
</system.servicemodel> ... <behaviors> <servicebehaviors> <behavior name="hellobehavior"> <servicemetadata httpgetenabled="true" /> <hostnameaddress /> </behavior> </servicebehaviors> </behaviors> <extensions> <behaviorextensions> <add name="hostnameaddress" type="helloconsoleservice.hostnameaddressbehaviorextension, helloconsoleservice, version=1.0.0.0, culture=neutral, publickeytoken=null"/> </behaviorextensions> </extensions> </system.servicemodel>
with the behavior in place, the meta data now contains the dns name instead of “localhost” in the endpoint addresses.
conclusion
of course creating the endpoints dynamically in code at runtime could also have solved this problem, but that would result in code that is not reusable and the wcf service programmer would have been responsible for the implementation. by developing this as a behavior, it is reusable across services.
Published at DZone with permission of Pieter De Rycke, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
-
File Upload Security and Malware Protection
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
RBAC With API Gateway and Open Policy Agent (OPA)
Comments