How to add Custom Service Behavior To WCF Configuration - wcf

I have a service with ComplexType in the Uri Template Parameter, I have Override the CanConvert() and ConvertStringToValue() methods to the class MyQueryStringConverter.
I need to add that behavior to the web.config file.
Here is the Behavior
public class MyWebHttpBehavior : WebHttpBehavior
{
protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new MyQueryStringConverter();
}
}
Here Is the Configuration File :
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttp"
maxReceivedMessageSize="20000000" >
<security mode="None">
<transport clientCredentialType = "None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="80" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service.Service1" behaviorConfiguration="mexBehaviour">
<endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="web"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
Please help how to add that behavior.

First, you must register your behavior, using behaviorExtensions element in your configuration file:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="MyWebHttpBehavior"
type="FullNameSpace.MyWebHttpBehavior, MyWebHttpBehavior.AssemblyName, Version=1.0.0.0, Culture=neutral" />
</behaviorExtensions>
</extensions>
Where:
"FullNameSpace.MyWebHttpBehavior" is your class with full namespace,
"MyWebHttpBehavior.AssemblyName" is your assembly name with extension
where your class is compiled.
"1.0.0.0" is the version of your assembly
"neutral" is your Culture. Change if any special Culture is required
Secound, declare your behavior:
<behaviors>
<endpointBehaviors>
<behavior name="customMyWebHttpBehavior">
<webHttp/>
<MyWebHttpBehavior/>
</behavior>
</endpointBehaviors>
<behaviors>
Next, add to the binding:
<bindings>
<webHttpBinding>
<binding name="webHttp"
maxReceivedMessageSize="20000000" >
<security mode="None">
<transport clientCredentialType = "None"/>
</security>
</binding>
<MyWebHttpBehavior />
</webHttpBinding>
</bindings>
And finally, set the behavior to your service endpoint:
<endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="customMyWebHttpBehavior"/>
I've copied this configuration of a service I've developed and changed the names to fit your class/configuration, but check if I did not wrote anything wrong.
I've used this link as base to setup my configuration: Custom Behavior won't register in my web.config
Hope it helps.

Related

WCF picking basicHttpBinding & ignoring the defined customBindings

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<bindings>
<customBinding>
<binding name="httpBinding">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
<binding name="httpsBinding">
<binaryMessageEncoding/>
<httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
</customBinding>
</bindings>
<services>
<service name="MyNamespace.MyIService" behaviorConfiguration="MyNamespace.MyIService" >
<endpoint address="http://wcf-client-url/virtualDirectory/MyService.svc"
binding="customBinding" contract="MyNamespace.MyIService"
name="httpBinding"/>
<endpoint address="https://wcf-client-url/virtualDirectory/MyService.svc"
binding="customBinding" bindingConfiguration="httpsBinding"
contract="MyNamespace.MyIService" name="httpsBinding" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyNamespace.MyIService" >
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
<behavior name="">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<client>
<endpoint address="http://wcf-client-url/virtualDirectory/MyService.svc"
binding="customBinding"
bindingConfiguration="httpBinding"
contract="MyNamespace.MyIService"
name="httpBinding" />
<endpoint address="https://wcf-client-url/virtualDirectory/MyService.svc"
binding="customBinding"
bindingConfiguration="httpsBinding"
contract="MyNamespace.MyIService"
name="httpsBinding" />
</client>
</system.serviceModel>
I have the above configuration in a WCF service that is hosted in IIS. I can successfully browse to the service in the browser, as you can observer i want to use a customBinding.
When i use visual studio to add a reference to this service in my client application using VS inbuilt add service reference dialogue, below is the client endpoint that visual studio creates for me.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_MyIService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://wcf-client-url/virtualDirectory/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyIService"
contract="ServiceReference1.MyIService" name="BasicHttpBinding_MyIService" />
</client>
</system.serviceModel>
What i'm not understanding is why VS is creating for me a client basicHttpBinding yet i have defined a customBinding on the server.
Also, when i test my service in "WCF Test Client" and then double click the config file in WCF Test Client, i see that indeed it displays the below config in the Test Client.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_MyIService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://myPCHostName/virtualDirectory/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyIService"
contract="MyIService" name="BasicHttpBinding_MyIService" />
</client>
</system.serviceModel>
</configuration>
I'm sort of new to this WCF development & i'm not clearly understanding how my customBinding are being ignored.
From my config above, is BasicHttpBinding being set any where as the default binding, or is there away i can completely remove it even though i can't seem to figure where its being set from in that configuration.
try to specify behaviorConfiguration and bindingConfiguration for both endpoints on service side. You have specified bindingConfiguration only for https endpoint, and have not done this for behaviors at all.
Should be something like
<bindings>
<customBinding>
<binding name="httpBinding">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
<binding name="httpsBinding">
<binaryMessageEncoding/>
<httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/>
</binding>
</customBinding>
</bindings>
<services>
<service name="MyNamespace.MyIService" behaviorConfiguration="MyNamespace.MyIService" >
<endpoint address="http://wcf-client-url/virtualDirectory/MyService.svc"
binding="customBinding"
contract="MyNamespace.MyIService"
bindingConfiguration="httpBinding"
behaviorConfiguration="httpEndpoint"
name="httpBinding"/>
<endpoint address="https://wcf-client-url/virtualDirectory/MyService.svc"
binding="customBinding"
bindingConfiguration="httpsBinding"
behaviorConfiguration="httpEndpoint"
contract="MyNamespace.MyIService"
name="httpsBinding" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="httpEndpoint" >
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
</serviceBehaviors>
</behaviors>

DotNetOpenAuth and ResourceServer service https configuration

I am trying to configure the DataApi.svc service of the DotNetOpenAuth to call my resources via https using AJAX.
I can call the service and hit the code behind but the OperationContext.Current.ServiceSecurityContext will be not authenticated
In IIS, I have "Anonymous authentication" set to "true".
In Fiddler I can see that the header is sent:
Authorization: Bearer gAAAAMcRmG5vw3LykShq7cNOEGUACBiNtlVGxGYdSVfkkXjR-[truncated]
The interface is decorated like that:
[ServiceContract]
public interface IDataApi {
[OperationContract, WebGet(UriTemplate = "/email", ResponseFormat = WebMessageFormat.Json)]
string GetEmail();
And here is my config:
<bindings>
<wsHttpBinding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
<webHttpBinding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DataApiBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="DataApiWebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DataApiBehavior" name="OAuthResourceServer.DataApi">
<endpoint address="" binding="wsHttpBinding" contract="OAuthResourceServer.Code.IDataApi" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
<endpoint address="web" binding="webHttpBinding" contract="OAuthResourceServer.Code.IDataApi" behaviorConfiguration="DataApiWebBehavior">
</endpoint>
</service>
</services>
Any idea of what can be wrong?
Thanks!
I was missing the
<serviceAuthorizationserviceAuthorizationManagerType="OAuthResourceServer.Code.OAuthAuthorizationManager, OAuthResourceServer" principalPermissionMode="Custom" />
in the service behavior! Solved :)

Configuring WCF for wsHttpBinding

I have a WCF service that works using basicHttpBinding, I'm trying to configure it to go over https and authenticate with a SQL membership provider, and to do this I'm trying to convert it to use wsHttpBinding.
However, with the updated config I get the following error when I try to connect with the client:
Could not find default endpoint element that references contract 'KFileService.IKFileWcfService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Here's the relevant portion of the web.config for the server:
<system.serviceModel>
<protocolMapping>
<remove scheme="http" />
<add scheme="https" binding="wsHttpBinding" bindingConfiguration="wsHttpBinding_IKFileWcfServiceBinding" />
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior name="KFileWcfServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
membershipProviderName="SqlMembershipProvider" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="KFileWcfServiceBehavior" name="KFileWcfService">
<endpoint address="https://localhost:36492/KFileWcfService.svc"
binding="wsHttpBinding" bindingConfiguration="wsHttpBinding_IKFileWcfServiceBinding"
name="wsHttpBinding_IKFileWcfService" bindingName="wsHttpBinding_IKFileWcfServiceBinding"
contract="KFileWcfService.IKFileWcfService">
<identity>
<certificateReference storeLocation="CurrentUser" />
</identity>
</endpoint>
</service>
</services>
<serviceHostingEnvironment minFreeMemoryPercentageToActivateService="0" multipleSiteBindingsEnabled="true"/>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding_IKFileWcfServiceBinding" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Message">
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
And here's the client side:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding_IKFileWcfService" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost:36492/KFileWcfService.svc"
binding="wsHttpBinding" bindingConfiguration="wsHttpBinding_IKFileWcfService"
contract="KFileWcfService.IKFileWcfService" name="wsHttpBinding_IKFileWcfService" />
</client>
</system.serviceModel>
I've done my best to go through all the existing questions and examples already, but haven't had any luck. Can anyone tell me what I'm doing wrong?
Edit:
For further reference, here's the server side configuration that works with basicHttpBinding:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false 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" />
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="KFileWcfService">
<endpoint address="https://localhost:36492/KFileWcfService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IKFileWcfService" contract="KFileService.IKFileWcfService" name="BasicHttpBinding_IKFileWcfService" />
</service>
</services>
<serviceHostingEnvironment minFreeMemoryPercentageToActivateService="0" multipleSiteBindingsEnabled="true"/>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IKFileWcfService" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed">
<readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
And client:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IKFileWcfService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost:36492/KFileWcfService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IKFileWcfService"
contract="KFileService.IKFileWcfService" name="BasicHttpBinding_IKFileWcfService" />
</client>
</system.serviceModel>
The type of the service contract on the client which you're trying to use, based on your error messsage:
KFileService.IKFileWcfService
The type of the service contract interface which you have on your client config:
KFileWcfService.IKFileWcfService
They should be the same. Change the client config to
... contract="KFileService.IKFileWcfService" ...
And it should work.

Contract requires Session, but Binding ‘WSHttpBinding’ doesn’t support it or isn’t configured properly to support it

I have specified the service contract to require the session.
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ITicketSales
{
}
The service decorated like this:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
public class TicketSalesService : ITicketSales
{
}
Here is my App.config file:
<system.serviceModel>
<services>
<service name="InternetRailwayTicketSales.TicketSalesImplementations.TicketSalesService" behaviorConfiguration="defaultBehavior">
<host>
<baseAddresses>
<add baseAddress = "https://localhost/TicketSales/"></add>
</baseAddresses>
</host>
<endpoint address="MainService" binding="wsHttpBinding" bindingConfiguration="wsSecureConfiguration"
contract="InternetRailwayTicketSales.TicketSalesInterface.ITicketSales" />
<endpoint address="mex" binding="mexHttpsBinding"
contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="wsSecureConfiguration">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceThrottling maxConcurrentInstances="5000" maxConcurrentSessions="5000"/>
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
When I press F5 I receive the error message "Contract requires Session, but Binding ‘WSHttpBinding’ doesn’t support it or isn’t configured properly to support it."
I really need the channel which supports SSL and requires session.
You can support sessions by enabling message security:
<binding name="wsHttpSecureSession">
<security>
<message establishSecurityContext="true"/>
</security>
</binding>
If you need to go with transport security you may need to specify a client credential type

maxItemsInObjectGraph ignored

I have a problem with a WCF service, which tries to serialize too much data. From the trace I get an error which says that the maximum number of elements that can be serialized or unserialized is '65536', try to increment the MaxItemsInObjectGraph quota.
So I went and modified this value, but it is just ignored (the error is the same, with the same number). All this is server-side. I am calling the service via wget for the moment.
My web config is like this:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="AlgoMap.Web.MapService.MapServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="131072" />
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:02:00">
<binaryMessageEncoding>
<readerQuotas maxDepth="64" maxStringContentLength="16384"
maxArrayLength="16384" maxBytesPerRead="16384"
maxNameTableCharCount="16384" />
</binaryMessageEncoding>
<httpTransport />
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="AlgoMap.Web.MapService.MapServiceBehavior"
name="AlgoMap.Web.MapService.MapService">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
contract="AlgoMap.Web.MapService.MapService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
Version 2, not working either:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AlgoMap.Web.MapService.MapServiceEndpointBehavior">
<dataContractSerializer maxItemsInObjectGraph="131072" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="AlgoMap.Web.MapService.MapServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:02:00">
<binaryMessageEncoding>
<readerQuotas maxDepth="64" maxStringContentLength="16384"
maxArrayLength="16384" maxBytesPerRead="16384"
maxNameTableCharCount="16384" />
</binaryMessageEncoding>
<httpTransport />
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="AlgoMap.Web.MapService.MapServiceBehavior"
name="AlgoMap.Web.MapService.MapService">
<endpoint
address="" binding="customBinding" bindingConfiguration="customBinding0"
contract="AlgoMap.Web.MapService.MapService"
behaviorConfiguration="AlgoMap.Web.MapService.MapServiceEndpointBehavior" />
<endpoint
address="mex" binding="mexHttpBinding" contract="IMetadataExchange"
behaviorConfiguration="AlgoMap.Web.MapService.MapServiceEndpointBehavior" />
</service>
</services>
</system.serviceModel>
Can anyone help??
Thanks!!
Any setting put in the web.config were happily ignored, I haven't found out why. But I found a workaround, that is, to put the MaxItemsInObjectGraph as a class decoration. This works flawlessly:
// MyService.svc
// using...
namespace MyNamespace {
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(MaxItemsInObjectGraph = 65536000)]
public class MyWebService {
[OperationContract]
[WebGet(UriTemplate = "tree/{sessionId}", ResponseFormat = WebMessageFormat.Json)]
public MyData GetTree(string sessionId) {
...
...
I ran into this as well, in my instance, I had forgot to place this setting in my client app.config file.
May be it is small yet? did you try to give larger value like 655360000? Note that you should change the value in client's and Server's config files. My guess is that you changed only in one part ;)
From a little search in Google, it seems you are adding the setting in the wrong place.
You need to create a new behavior in the endPointBehaviors section (not serviceBehaviors).
I have the same issue. Using service behavior attribute at the class level works fine, which make sense. I prefer config level change. I have added the config entries both at the client(web.config) and service level(app.config). Did this work for you?