Reputation: 2265
I am using following code to connect WCF service. But I am getting following error.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://demo.ca/LicenseWebService.svc. The client and service bindings may be mismatched.
Code:
var address = new EndpointAddress("http://demo.ca/LicenseWebService.svc");
var transport = new HttpTransportBindingElement();
transport.KeepAliveEnabled = false;
var binding = new CustomBinding(transport);
factory = new ChannelFactory<LicenseWebIService>(binding, address);
channel = factory.CreateChannel();
Webconfig of WCF Service:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>
Previously I was using BasicHttpBinding and it was working.
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Name = "BasicHttpBinding_LicenseWebIService";
myBinding.Security.Mode = BasicHttpSecurityMode.None;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endPointAddress = new EndpointAddress("http://demo.ca/LicenseWebService.svc");
factory = new ChannelFactory<LicenseWebIService>(myBinding, endPointAddress);
channel = factory.CreateChannel();
Code of BasicHttpBinding was working correctly. But I needed to set KeepAlive = false;
according to Microsoft Review of my code. So I have tried to replace with CustomBinding
. Now I am getting above error of Mismatched.
Can anybody suggest me solution of above problem with CustomBinding?
However, it is also fine if anybody can sugget me how to set KeepAlive = false
in BasicHtttpBinding.
Upvotes: 2
Views: 1620
Reputation: 1446
CustomBinding typically requires at least two elements: transport (you have it) and message encoding (you don't have it and WCF tries to guess it from the transport one). Try to add message encoding part explicitly (and you'll be able to specify encoding):
var address = new EndpointAddress("http://demo.ca/LicenseWebService.svc");
var transport = new HttpTransportBindingElement();
transport.KeepAliveEnabled = false;
var messageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8);
var binding = new CustomBinding(new [] { messageEncoding, transport });
factory = new ChannelFactory<LicenseWebIService>(binding, address);
channel = factory.CreateChannel();
Upvotes: 1