I am new to wcf and trying to create a webservice from a client provided wsdl; I'm having trouble changing some wcf generated wsdl entries to match the provided wsdl. I found this: WSDL-first approach: How to specify different names for wsdl:port and wsdl:binding? which exactly describes the issue i'm having but the solution provided there is not working in Visual Studio 2010 with .NET 4.0.
Here is the web config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="portName" type="CustomWsdlExtension.PortNameWsdlBehaviorExtension, CustomWsdlExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<services>
<service name="CustomWsdlExtension.Service" behaviorConfiguration="MyBehavior">
<endpoint address="" binding="basicHttpBinding" contract="CustomWsdlExtension.IService" behaviorConfiguration="customPortName"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="customPortName">
<portName name="myCustomName"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
</configuration>
And the custom class:
using System;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
namespace CustomWsdlExtension
{
public class PortNameWsdlBehavior : IWsdlExportExtension, IEndpointBehavior
{
public string Name { get; set; }
public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
{
}
public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
if (!string.IsNullOrEmpty(Name))
{
context.WsdlPort.Name = Name;
}
}
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
public class PortNameWsdlBehaviorExtension : BehaviorExtensionElement
{
[ConfigurationProperty("name")]
public string Name
{
get
{
object value = this["name"];
return value != null ? value.ToString() : string.Empty;
}
set { this["name"] = value; }
}
public override Type BehaviorType
{
get { return typeof(PortNameWsdlBehavior); }
}
protected override object CreateBehavior()
{
return new PortNameWsdlBehavior { Name = Name };
}
}
}
It compiles ok (I have a warning web config saying The element 'behavior' has invalid child element 'portName'. List of possible elements expected: 'clientVia, callbackDebug, callbackTimeouts, clear, clientCredentials, transactedBatching, dataContractSerializer, dispatcherSynchronization, remove, synchronousReceive, enableWebScript, webHttp, endpointDiscovery, soapProcessing' which seems to be related to a bug in VS)
The generated wsdl still shows wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1">' rather than the modified port name.
You can find the whole test project here: http://dl.dropbox.com/u/13875536/CustomWsdlExtension.zip Thanks in advance.