WCF service with JSONP over SSL - json

We have a SSL configured website that hosts a WCF-service. The service's binding has crossDomainScriptAccessEnabled="true" and communication is serialized using JSON.
When we request this service from http it returns JSONP but when it is requested using HTTPS it returns just JSON. I need to have JSONP in either way, please help.
Current configuration is like this:
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
<behaviors>
<serviceBehaviors>
<behavior name="JsonServiceBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors><behavior name="webHttpBehavior">
<webHttp />
</behavior></endpointBehaviors>
</behaviors>
<services>
<service name="Backend.CIService" behaviorConfiguration="JsonServiceBehaviors">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP" contract="Backend.ICIService"
behaviorConfiguration="webHttpBehavior"/>
</service></services>

What happens if you use this configuration:
<webHttpBinding>
<binding name="jsonp" crossDomainScriptAccessEnabled="true" />
<binding name="jsonpSsl" crossDomainScriptAccessEnabled="true">
<security mode="Transport" />
</binding>
</webHttpBinding>
<behaviors>
<serviceBehaviors>
<behavior name="JsonServiceBehaviors">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Backend.CIService" behaviorConfiguration="JsonServiceBehaviors">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="jsonp" contract="Backend.ICIService"
behaviorConfiguration="webHttpBehavior"/>
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="jsonpSsl" contract="Backend.ICIService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
The problem is that if you want to call service over both HTTP and HTTPS you must provide two endpoints - one for HTTP and one for HTTPS.

Related

WCF Rest Service is not returning JSON data. But It works for XML

The service contract info is below
Service Contract:
[OperationContract]
[FaultContract(typeof(DcCustomFaultMessage))]
[WebInvoke(Method="GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/GetCustomData")]
List<DcCustomData> GetCustomData();
and the config details here
Config:
<system.serviceModel>
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="MyServiceBinding" behaviorConfiguration="web" contract="IMyService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="True"/>
<serviceTimeouts transactionTimeout="05:00:00"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web" >
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding name="MyServiceBinding">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<diagnostics performanceCounters="All"/>
When i debugged ,GetCustomData() returns list. but when i check it on browser returns nothing.. no error information. please help me
Please configure WCF Message logging and use svctraceviewer tool to see the actual error that probably is happening behind the scenes:
<configuration>
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="wcfTraceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="E:\Errors\WcfTrace.svclog" traceOutputOptions="DateTime" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
https://msdn.microsoft.com/en-us/library/ms731859%28v=vs.110%29.aspx

Migrated to a WCF service with SOAP and JSON endpoint - JSON response not as expected

I migrated a web service to a WCF service. The service exposes two endpoints, one returns JSON and one returns SOAP.
The SOAP endpoint works as expected, but the response from the JSON endpoint is wrapped with the response object name - something that did not exist in the JSON response the web service I migrated from used to return.
I tried working with the BodyStyle property, but all I got is an exception: The body style 'Bare' is not supported by 'WebScriptEnablingBehavior'. Change the body style to be 'WrappedRequest'.
All my methods accept only POST requests.
This is how my service is configured:
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="basicHttpBinding" bindingNamespace="http://MyService.org/" contract="IMyService"/>
<endpoint address="json" behaviorConfiguration="JsonEndpointBehavior" binding="webHttpBinding" contract="IMyService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonEndpointBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>

WCF oData Insert via Web Service

I am trying to increase maxReceivedMessageSize for my wcf odata service. I have an another web service which is posting values to my wcf service to insert values to sql. I am posting
{
A:1,
B:1,
C:3,
D:4,
}
this raised an error. The remote server returned an error: (400) Bad Request. If i post the value below, it inserted successfully.
{
A:1
}
so can somebody please help me how to fix this error please? There are examples on the web which are trying to modify web config like below but they dont suits me because i dont have a service contract.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<!-- Create a custom binding for our service to enable sending large amount of data -->
<binding name="MyBasicHttpBinding"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<!-- Enable the serializer to serialize greater number of records -->
<behavior name="WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<!-- Bind the WCF service to our custom binding -->
<service behaviorConfiguration="WCFServiceBehavior"
name="WcfDataService">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="MyBasicHttpBinding"
contract=" `WHICH I DONT HAVE, OR I HAVE IT BUT I AM NOOB` "/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
My WCF working with ADO.net Entity Model
public class WcfDataService : DataService< DataModel.DataModelEntities >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
Contract is System.Data.Services.IRequestHandler from Microsoft.Data.Services.
My configuration:
<system.serviceModel>
<services>
<service name="ODataSampleRun.DemoDataService" behaviorConfiguration="Behavior">
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
<endpoint contract="System.Data.Services.IRequestHandler" binding="customBinding" bindingConfiguration="RawODataService" address="http://localhost:9090/DemoDataService" behaviorConfiguration="test" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="NoneWebBinding">
<security mode="None" />
</binding>
</webHttpBinding>
<customBinding>
<binding name="RawODataService">
<webMessageEncoding webContentTypeMapperType="ODataSampleRun.RawContentTypeMapper, ODataSampleRun, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<httpTransport manualAddressing="true" maxReceivedMessageSize="524288000" transferMode="Streamed" />
</binding>
</customBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="test">
<webHttp faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>

WCF authentication over https

I have a WCF service that is using https to communicate and json for the response format. I don't want my methods to be available to anyone so i change the authentication in IIS from anonymous & basic to just basic.
So far, the browser is asking for user and pass but i get the following error :
Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https].
What do i have to change to my endpoint to work with authentication ?
My web.config looks like :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="restBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" proxyCredentialType="Basic" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="ContactLibrarySecure.ContactLibraryService">
<endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration=""
name="mex" contract="IMetadataExchange" />
<endpoint address="rest" behaviorConfiguration="restBehavior"
binding="webHttpBinding" bindingConfiguration="restBinding"
name="rest" contract="ContactLibrarySecure.IContact" />
<host>
<baseAddresses>
<add baseAddress="https://192.168.1.31/ContactLibrary2.0HTTPS" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="soapBinding" maxBufferSize="2000000000" maxBufferPoolSize="2000000000"
maxReceivedMessageSize="2000000000">
<security mode="Transport">
<transport clientCredentialType="Windows" proxyCredentialType="Basic" />
<message clientCredentialType="UserName" />
</security>
</binding>
</basicHttpBinding>
<wsHttpBinding>
<binding name="mexBinding">
<security mode="Transport">
<transport clientCredentialType="Windows" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
<webHttpBinding>
<binding name="restBinding" closeTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2000000000" maxBufferPoolSize="2000000000" maxReceivedMessageSize="2000000000">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="ContactLibrarySecure.ContactLibraryService">
<endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="mexBinding"
name="mex" contract="IMetadataExchange" />
<endpoint address="rest" behaviorConfiguration="restBehavior"
binding="webHttpBinding" bindingConfiguration="restBinding"
name="rest" contract="ContactLibrarySecure.IContact" />
<endpoint address="soap" behaviorConfiguration="soapBehavior"
binding="basicHttpBinding" bindingConfiguration="soapBinding"
name="soap" contract="ContactLibrarySecure.IContact" />
<host>
<baseAddresses>
<add baseAddress="https://192.168.1.31/ContactLibrary2.0HTTPS" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
<behavior name="soapBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
This config file solved my problem. I changed from basic to windows authentication, after i installed windows authentication in iis.

WCF and JSON binding

I am trying to create a wcf service that returns json. I have some problems with my config file and i also don't know how to test it.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="ContactLibraryJSON.ContactLibrary">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="JSONEndpointBehavior"
contract="ContactLibraryJSON.IContactServiceJSON" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.31/ContactLibraryJSON" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="JSONEndpointBehavior">
<webHttp/>
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
I still get
"Cannot add the behavior extension webhttp to the service behavior
names JSONEndpointBehavior because the underlying behavior type does
not implement the IServiceBehaviorInterface"
Contact is defined like :
[DataContract(Name="Contact")]
public class Contact
{
[DataMember(Name="FirstName")]
public string firstName=null;
[DataMember(Name="LastName")]
public string lastName=null;
[DataMember(Name="Email")]
public string email=null;
[DataMember(Name = "Age")]
public int age = 0;
[DataMember(Name = "Street")]
public string street=null;
[DataMember(Name = "City")]
public string city=null;
[DataMember(Name = "Country")]
public string country=null;
}
IContactService is defined like :
[ServiceContract]
public interface IContactServiceJSON
{
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
Contact GetContact();
}
The implementation of GetContact:
public Contact GetContact()
{
return new Contact()
{
firstName = "primulNume",
lastName = "alDoileaNume",
age = 33,
city = "Cluj",
country = "Romania",
email = "ceva#mail.com",
street = "Bizusa 8"
};
}
My service runs on another computer in my lan. Base address is like: http://192.168.1.xxx/ContactLibraryService. ContactLibraryService is hosted by IIS and is converted to an application.
You need to add the <webHttp/> to the list of endpoint behaviors. Also, the endpoint needs to use the webHttpBinding. And finally, to respond to GET HTTP requests, you need to use the WebGet attribute (instead of WebInvoke(Method="GET").
<system.serviceModel>
<services>
<service name="ContactLibrary.ContactLibrary">
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="JSONEndpointBehavior"
contract="ContactLibrary.IContact"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="ws"
binding="wsHttpBinding"
bindingConfiguration=""
contract="ContactLibrary.IContact" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/ContactLibrary" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JSONEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
And the service contract:
[ServiceContract]
public interface IContact
{
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "contact")]
Contact GetContact(int idContact);
}
It's not good that you have Service Contract IContact and Data Contact Contact. Rename Service Contract like IContactService.
<services>
<service name="ContactLibrary.ContactService">
<endpoint address="" binding="webHttpBinding" contract="ContactLibrary.IContactService" behaviorConfiguration="JsonBehavior" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
During Debug time (it looks you have WCF library), service address will be http://localhost:8732/contact
I think you missed bindings here you have to add webHttpBinding and basichttpbing under the bindings tab in system.serviceModel tag.
<services>
<service name="VInfotech.Server.Intranet.IntranetService" behaviorConfiguration="IntranetService.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="VInfotech.Server.Intranet.IIntranet">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
<service name="VInfotech.Server.Intranet.MobileServicesController" behaviorConfiguration="ServBehave">
<endpoint address="RestService" bindingConfiguration="StreamedRequestWebBinding" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="VInfotech.Server.Intranet.IMobileServices" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="StreamedRequestWebBinding"
bypassProxyOnLocal="true"
useDefaultWebProxy="false"
hostNameComparisonMode="WeakWildcard"
sendTimeout="10:15:00"
openTimeout="10:15:00"
receiveTimeout="10:15:00"
maxReceivedMessageSize="9223372036854775807"
maxBufferPoolSize="9223372036854775807"
maxBufferSize="2147483647"
transferMode="StreamedRequest" >
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
</binding>
</webHttpBinding>
<basicHttpBinding>
<binding name="Binding1" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1073741824" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="1073741824" maxStringContentLength="1073741824" maxArrayLength="1073741824" maxBytesPerRead="1073741824" maxNameTableCharCount="1073741824"/>
</binding>
<!-- For Cyber Source bindings-->
<binding name="ITransactionProcessor" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1073741824" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="1073741824" maxStringContentLength="1073741824" maxArrayLength="1073741824" maxBytesPerRead="1073741824" maxNameTableCharCount="1073741824"/>
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
<!--Cyber Source bindings ends here-->
</bindings>