REST Service WebHttpBinding with Http and Https - rest

I have a REST service. This service need to be work with http and https.
I've tried to add two endpoint in my web.config file. But i get this error when i try to browse my service over http:
Could not find a base address that matches scheme https for the
endpoint with binding WebHttpBinding. Registered base address schemes
are [http].
and i get this error when i try to browse over https:
Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https].
If i remove one of endpoint from my config file, the both http and https services works fine.
I checked this link: WebHttpBinding with Http and Https
But when i remove endpoints from my config file, the both http and https services runs without any errors on web browser. But when i try to call one of my methods (over a rest client tool) in this service it gets:
500 Internal Server Error.
How can i run this service over http and https without any errors?
My config file is like this:
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding" bindingConfiguration="webHttpBinding"/>
<add scheme="https" binding="webHttpBinding" bindingConfiguration="webHttpsBinding"/>
</protocolMapping>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding">
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" />
</security>
</binding>
<binding name="webHttpsBinding">
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="MyProject.MyService" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="webHttpBinding" contract="MyProject.IMyService" />
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="webHttpsBinding" contract="MyProject.IMyService" />
<!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />-->
<!--<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceAuthorization serviceAuthorizationManagerType="MyProject.RestAuthorizationManager, MyProject"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>

here is a sample web.config in order to have both http and https support. i hope solve your problem:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SoapBinding" />
</basicHttpBinding>
<basicHttpsBinding>
<binding name="SecureSoapBinding" />
</basicHttpsBinding>
<webHttpBinding>
<binding name="RestBinding" />
<binding name="SecureRestBinding">
<security mode="Transport" />
</binding>
</webHttpBinding>
<mexHttpBinding>
<binding name="MexBinding" />
</mexHttpBinding>
<mexHttpsBinding>
<binding name="SecureMexBinding" />
</mexHttpsBinding>
</bindings>
<client />
<services>
<service behaviorConfiguration="ServiceBehavior" name="Interface.Core">
<endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="SoapBinding" name="Soap" contract="Interface.ICore" />
<endpoint address="soap" binding="basicHttpsBinding" bindingConfiguration="SecureSoapBinding" name="SecureSoap" contract="Interface.ICore" />
<endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Rest" contract="Interface.ICore" />
<endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="SecureRestBinding" name="SecureRest" contract="Interface.ICore" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="MexBinding" name="Mex" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration="SecureMexBinding" name="SecureMex" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>

Related

Bad Request REST/SOAP for a WCF Webservice

A WCF service needs to be capable to SOAP and REST format.
It returns Bad Request (400), below is my web config file:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ILoginSVC" allowCookies="true" maxBufferPoolSize="20000000" maxBufferSize="70000000" maxReceivedMessageSize="70000000">
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webHttpBindingILoginSVC" allowCookies="true" maxBufferSize="100000000" maxBufferPoolSize="100000000" maxReceivedMessageSize="100000000" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="LoginSVC.LoginSVC">
<host>
<baseAddresses>
<add baseAddress="http://localhost:59900/LoginSVC" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="LoginSVC.ILoginSVC">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="REST" binding="webHttpBinding" contract="LoginSVC.ILoginSVC" behaviorConfiguration="RESTEndPointBehaviour">
</endpoint>
</service>
</services>
</system.serviceModel>
In your configuration file, you have not assigned the binding configuration you created, so use the default BasicHttpBinding. You need to explicitly assign the binding you defined (BasicHttpBinding_ILoginSVC) to your endpoint like this:
<endpoint address="" bindingConfiguration="BasicHttpBinding_ILoginSVC" binding="basicHttpBinding" contract="LoginSVC.ILoginSVC" />

Getting error "the provided uri scheme 'https' is invalid expected 'http" from service

I'm getting this error. Do you have any idea?
My code is below;
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Integration" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://test/WcfService/Integration.svc"
binding="basicHttpBinding" bindingConfiguration="Integration"
contract="IntegrationService.Integration" name="Integration" />
</client>
</system.serviceModel>
You may modify your app.config as shown in the following example:
<bindings>
<basicHttpBinding>
<binding name="Integration">
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="Certificate" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>

Calling JSON Rest service from browser

Want to enable https on my REST JSON WCF service and test it in IE browser.
WSDL is loading with no issues (https://localhost/myservice/Imyservice.svc?WSDL).
But i tried to call a operation ( https://localhost/myservice/Imyservice.svc/Getdata), I am getting
Request Error The server encountered an error processing the request. .
Below is my web.config. Can anyone help me with this
<webHttpBinding>
<binding name="SecureBasicRest" allowCookies="true" >
<security mode="Transport" />
<readerQuotas maxDepth="32"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="svcBehavior">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="svcEndpoint">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="myservice.Imyservice" behaviorConfiguration="svcBehavior">
<endpoint binding="webHttpBinding" bindingConfiguration="SecureBasicRest"
behaviorConfiguration="svcEndpoint" name="webHttp"
contract="myservice.Imyservice" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
The root cause is that there is something wrong with the definition of the service endpoint.
<service name="myservice.Imyservice" behaviorConfiguration="svcBehavior">
<endpoint binding="webHttpBinding" bindingConfiguration="SecureBasicRest"
behaviorConfiguration="svcEndpoint" name="webHttp"
contract="myservice.Imyservice" />
</service>
Your idea is correct, we should add a service endpoint with transport security mode. However, the service name should be a fully qualified name of the service implemented class instead of the service interface.
<service name="myservice.myservice"
Feel free to let me know if the problem still exists.

Exception: The client certificate is not provided

I am trying to configure WCF service with security. I have generated 2 certificates (for server and client side) stored in LocalComputer\Personal Certificates. My configuration is:
Server:
<netTcpBinding>
<binding name="defaultBinding">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</netTcpBinding>
<service name="..." behaviorConfiguration="serviceBehavior">
<endpoint address="..." binding="netTcpBinding" bindingConfiguration="defaultBinding" contract="...">
<identity>
<dns value="ClientSide"/>
</identity>
</endpoint>
</service>
<behavior name="serviceBehavior">
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" storeName="My" findValue="ServerSide" x509FindType="FindBySubjectName"/>
<clientCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</clientCertificate>
</serviceCredentials>
<behavior>
Client:
<netTcpBinding>
<binding name="defaultBinding">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</netTcpBinding>
<endpoint name="..." binding="netTcpBinding" bindingConfiguration="defaultBinding" contract="..."
behaviorConfiguration="endpointBehavior">
<identity>
<dns value="ServerSide"/>
</identity>
</endpoint>
<behavior name="endpointBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</serviceCertificate>
<clientCertificate storeLocation="LocalMachine" storeName="My" findValue="ClientSide" x509FindType="FindBySubjectName"/>
</clientCredentials>
<behavior>
I am getting the exception: The client certificate is not provided. Specify a client certificate in ClientCredentials
I have tried many tutorials, but none of them works. Any suggestion?
The answer is actually in the Exception.
You don't have a client certificate. You define a service certificate for the client certificate with this
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</serviceCertificate>
<clientCertificate storeLocation="LocalMachine" storeName="My" findValue="ClientSide" x509FindType="FindBySubjectName"/>
</clientCredentials>
But what you actually should have done is defining a client certificate for the client
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="endpointBehavior">
<clientCredentials>
<clientCertificate storeLocation="LocalMachine" storeName="My" findValue="ClientSide" x509FindType="FindBySubjectName" />
<serviceCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
This should at least solve your The client certificate is not provided. Specify a client certificate in ClientCredentials exception.

Web.config transformation: how to apply a transformation to all node matching a Locator expression?

I've recently discovered the web.config automatic transformation in the web deploy tool of visual studio 2010.
It's working well, but I have a scenario I can't seem to get working.
Assume I have the following root Web.config
<services>
<service name="Service1">
<endpoint address="" binding="customBinding" bindingConfiguration="LargeBufferBinding"
contract="Service1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service name="Service2">
<endpoint address="" binding="customBinding" bindingConfiguration="LargeBufferBinding"
contract="Service2" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service name="Service3">
<endpoint address="" binding="customBinding" bindingConfiguration="LargeBufferBinding"
contract="Service3" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
For my Web.Release.config, I want all the endpoint nodes with a binding of mexHttpBinding to be removed.
I've used the following in my Web.Release.config:
<services>
<service>
<endpoint binding="mexHttpBinding" xdt:Locator="Match(binding)" xdt:Transform="Remove" />
</service>
</services>
However, this will only remove the first match, in the Service1, but not the following ones.
I've tried various way of locating the node, on the endpoint and service node, but only the first match ever gets replaced.
Is there a way to get all the <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> to be removed ?
Thanks.
I've just tried this and using RemoveAll instead of Remove seems to do the trick:
<services>
<service>
<endpoint binding="mexHttpBinding" xdt:Locator="Match(binding)" xdt:Transform="RemoveAll" />
</service>
</services>