WCF: Getting (400) Bad Request when sending small amount of data - wcf

I am getting "The remote server returned an unexpected response: (400) Bad Request" when I try to send more than 100 audit entries. I am using Fiddler to help debug and do see the request being sent to the server. The client and server both use the same interface.
[ServiceContract]
public interface ISyncDataContract
{
#region Audit Log
/// <summary>
/// Creates a collection of new audit entries items in the database.
/// </summary>
/// <param name="items">The audit entry items to be created.</param>
/// <returns><c>True</c> if created successfully; otherwise, <c>false</c>.</returns>
[OperationContract]
[WebInvoke(UriTemplate = "AuditEntries", Method = "PUT")]
bool CreateAuditEntryItems(AuditEntryItemCollection items);
/// <summary>
/// Gets all the audit entry items available.
/// </summary>
/// <returns>An <see cref="AuditEntryItemCollection"/> object containing all the
/// available audit entry items.</returns>
[OperationContract]
[WebGet(UriTemplate = "AuditEntries")]
Message GetAuditEntryItems();
#endregion
}
AuditEntryItem.cs
[DataContract]
public class AuditEntryItem
{
#region Constructor/Deconstructor
/// <summary>
/// Initializes a new instance of the <see cref="AuditEntryItem"/> class.
/// </summary>
public AuditEntryItem()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AuditEntryItem"/> class.
/// </summary>
/// <param name="auditEntry">The audit entry.</param>
public AuditEntryItem(AuditEntry auditEntry)
{
if (auditEntry == null)
{
throw new ArgumentNullException("auditEntry");
}
this.Audit_Type = auditEntry.Audit_type;
this.ComputerName = Environment.MachineName;
this.Message = auditEntry.Message;
this.Sequence_Number = auditEntry.Sequence_number;
this.Session_ID = auditEntry.Session_ID;
this.SyncDate = DateTime.Now;
this.Time_Stamp = auditEntry.Time_stamp;
this.User_ID = auditEntry.User_ID;
}
#endregion Constructor/Deconstructor
#region Properties
/// <summary>
/// Gets or sets the session ID.
/// </summary>
/// <value>
/// The session ID.
/// </value>
[DataMember]
[XmlElement(ElementName = #"Session_ID")]
public string Session_ID { get; set; }
/// <summary>
/// Gets or sets the user ID.
/// </summary>
/// <value>
/// The user ID.
/// </value>
[DataMember]
[XmlElement(ElementName = #"User_ID")]
public string User_ID { get; set; }
/// <summary>
/// Gets or sets the time stamp.
/// </summary>
/// <value>
/// The time stamp.
/// </value>
[DataMember]
[XmlElement(ElementName = #"Time_Stamp")]
public string Time_Stamp { get; set; }
/// <summary>
/// Gets or sets the sequence number.
/// </summary>
/// <value>
/// The sequence number.
/// </value>
[DataMember]
[XmlElement(ElementName = #"Sequence_number")]
public int Sequence_Number { get; set; }
/// <summary>
/// Gets or sets the message.
/// </summary>
/// <value>
/// The message.
/// </value>
[DataMember]
[XmlElement(ElementName = #"Message")]
public string Message { get; set; }
/// <summary>
/// Gets or sets the type of the audit.
/// </summary>
/// <value>
/// The type of the audit.
/// </value>
[DataMember]
[XmlElement(ElementName = #"Audit_type")]
public string Audit_Type { get; set; }
/// <summary>
/// Gets or sets the name of the computer.
/// </summary>
/// <value>
/// The name of the computer.
/// </value>
[DataMember]
[XmlElement(ElementName = #"ComputerName")]
public string ComputerName { get; set; }
/// <summary>
/// Gets or sets the sync date.
/// </summary>
/// <value>
/// The sync date.
/// </value>
[DataMember]
[XmlElement(ElementName = #"SyncDate")]
public DateTime? SyncDate { get; set; }
/// <summary>
/// Gets the time stamp value in a date time format.
/// </summary>
[XmlIgnore]
public DateTime DisplayTimeStamp
{
get { return this.TimeStampDateTime(); }
}
#endregion Properties
#region Overrides
public override bool Equals(object obj)
{
return obj is AuditEntryItem ? this.Equals((AuditEntryItem)obj) : false;
}
public bool Equals(AuditEntryItem other)
{
if (ReferenceEquals(this, other))
{
return true;
}
return string.Equals(this.Audit_Type, other.Audit_Type) &&
string.Equals(this.ComputerName, other.ComputerName) &&
string.Equals(this.Message, other.Message) &&
this.Sequence_Number == other.Sequence_Number &&
string.Equals(this.Session_ID, other.Session_ID) &&
this.SyncDate == other.SyncDate &&
string.Equals(this.Time_Stamp, other.Time_Stamp) &&
string.Equals(this.User_ID, other.User_ID);
}
public override int GetHashCode()
{
unchecked
{
var result = (this.Audit_Type != null ? this.Audit_Type.GetHashCode() : 0);
result = (result * 397) ^ (this.ComputerName != null ? this.ComputerName.GetHashCode() : 0);
result = (result * 397) ^ (this.Message != null ? this.Message.GetHashCode() : 0);
result = (result * 397) ^ this.Sequence_Number.GetHashCode();
result = (result * 397) ^ (this.Session_ID != null ? this.Session_ID.GetHashCode() : 0);
result = (result * 397) ^ (this.SyncDate != null ? this.SyncDate.GetHashCode() : 0);
result = (result * 397) ^ (this.Time_Stamp != null ? this.Time_Stamp.GetHashCode() : 0);
result = (result * 397) ^ (this.User_ID != null ? this.User_ID.GetHashCode() : 0);
return result;
}
}
#endregion Overrides
/// <summary>
/// Converts the Java time stamp value into a readable format.
/// </summary>
/// <returns>A readable date time format.</returns>
private DateTime TimeStampDateTime()
{
if (this.Time_Stamp.IsNullOrEmpty())
{
return new DateTime(1970, 01, 01);
}
long value;
if (!long.TryParse(this.Time_Stamp, out value))
{
return new DateTime(1970, 01, 01);
}
value = value / 1000;
return new DateTime(1970, 01, 01).AddSeconds(value);
}
}
AuditEntryItemCollection.cs
[DataContract]
[XmlRoot(ElementName = "AuditLog")]
public class AuditEntryItemCollection
{
#region Declarations
#endregion Declarations
#region Constructor/Deconstructor
/// <summary>
/// Initializes a new instance of the <see cref="AuditEntryItemCollection"/> class.
/// </summary>
public AuditEntryItemCollection()
{
this.AuditEntryItems = new List<AuditEntryItem>();
}
#endregion Constructor/Deconstructor
#region Properties
/// <summary>
/// Gets or sets the collection of <see cref="AuditEntryItem"/>
/// objects.
/// </summary>
/// <value>
/// The collection of <see cref="AuditEntryItem"/> objects.
/// </value>
[XmlElement(ElementName = #"AuditEntry")]
[DataMember]
public List<AuditEntryItem> AuditEntryItems { get; set; }
#endregion Properties
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<behaviors>
<endpointBehaviors>
<behavior name="restXmlBehavior">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Xml" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
<behavior name="rssAtomBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="metadataBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="StreamedHttp"
maxReceivedMessageSize="2147483647"
transferMode="Streamed" >
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="6000000" />
</binding>
</webHttpBinding>
</bindings>
UPDATE:
The error I am now getting is "The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element." As far as I can tell, I have already done this. Is there some other setting I need to set?

You could make sure the httpRuntime is also configured for large requests:
(An example grabbed from a service I use to upload files:)
<httpRuntime executionTimeout="3600" maxRequestLength="50000000" maxQueryStringLength="2097151" requestValidationMode="2.0" />
And also perhaps look at the buffer pool sizes on the binding (again, these values are just examples):
<binding name="WHB" maxReceivedMessageSize="50000000" maxBufferPoolSize="50000000" crossDomainScriptAccessEnabled="true">
<readerQuotas maxArrayLength="50000000" maxStringContentLength="50000000" />

Related

Linq to sql table dynamically with ExecuteQuery VB

I am using VB Framework 4.0 and Linq to sql.
I want to choose dynamycally the name of table. I have used the library namedtable.dll and I have mapped all the tables of database and it's Ok.
My problem is when I try to execute executequery. Here my code.
Imports Microsoft.VisualBasic
Imports System.Data.Linq
Imports Prototype.NamedTable.Data
Imports Prototype.NamedTable.Utility
Public Class tipos
Private _conexion As String = "conexion"
Public Sub New()
End Sub
...........
Public Function getConsulta(byval tableName as String) As IList(Of TIPOS)
Dim context As New DataContext(_conexion)
sql = " select COD, NAME from " & tableName
Dim a = context.ExecuteQuery(Of TIPOS)(sql)
Return sql.ToList
End Function
End Class
but I have an error: "El tipo 'TIPOS' debe declarar un constructor predeterminado (sin parámetros) para que pueda construirse durante la asignación." that in English is:
"The type 'Type TIPOS' must declare a default (parameterless) constructor in order to be constructed during mapping"
I have defined "TIPOS" in other file:
Public Interface TIPOS
<Column(CanBeNull:=False)> Property COD Integer
<Column(CanBeNull:=False)> Property NAME As String
End Interface
Public Class ITIPO : Implements TIPO
Private _cod As Integer
Private _name As String
Public Property COD As Integer Implements TIPO.COD
Get
Return _cod
End Get
Set(ByVal value As Integer)
_cod = value
End Set
End Property
Public Property NAME As String Implements TIPO.NAME
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
I need help!
Sorry for my English.
The solution can be found on codeproject.com in article "Dynamic Table Mapping for LINQ-to-SQL." Below is a static class you can use. Please see the article for instructions on what you must do to use the 4 different generic methods. Here is an invocation example:
public interface IResult
{
[Column(IsPrimaryKey = true)]
int Id { get; set; }
[Column]
string Name { get; set; }
[Column]
double Value { get; set; }
}
public void TestThis()
{
var connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=YourDatabaseName;Integrated Security=True;Pooling=False";
var context = new DataContext(connectionString);
var table = context.GetTable<IResult>("YourTableName");
var query = from r in table where r.Id == 108 select r;
var list = query.ToList();
}
Class Code:
namespace Prototype.NamedTable
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
/// <summary>
/// The utility.
/// </summary>
public static class Utility
{
#region Constants and Fields
/// <summary>
/// The named types.
/// </summary>
private static readonly Dictionary<string, Type> NamedTypes = new Dictionary<string, Type>();
/// <summary>
/// The _assembly builder.
/// </summary>
private static AssemblyBuilder _assemblyBuilder;
/// <summary>
/// The _module builder.
/// </summary>
private static ModuleBuilder _moduleBuilder;
#endregion
#region Properties
/// <summary>
/// Gets or sets a value indicating whether Verbose.
/// </summary>
public static bool Verbose { get; set; }
#endregion
#region Public Methods
/// <summary>
/// The clear.
/// </summary>
public static void Clear()
{
_assemblyBuilder = null;
NamedTypes.Clear();
}
/// <summary>
/// Retrieve a table from the data context which implements ITable<TEntity> by T and use ITable<TBack>
/// </summary>
/// <typeparam name="TEntity">
/// Entity Type
/// </typeparam>
/// <typeparam name="TBack">
/// Backing Type
/// </typeparam>
/// <param name="context">
/// Data Context
/// </param>
/// <returns>
/// </returns>
public static ATable<TEntity> GetTable<TEntity, TBack>(this DataContext context) where TEntity : class
where TBack : class
{
// Create the backup table
Table<TBack> refer = context.GetTable<TBack>();
// Prepare the cloning method
Delegate cloneFrom = CompileCloning(typeof(TEntity), typeof(TBack));
// Construct the table wrapper
return new ATable<TEntity>(refer, cloneFrom);
}
/// <summary>
/// Retrieve a table from the data context which implements ITable<TEntity> uses specific backing table
/// </summary>
/// <typeparam name="TEntity">
/// Entity Type
/// </typeparam>
/// <param name="context">
/// Data context
/// </param>
/// <param name="name">
/// Table name
/// </param>
/// <returns>
/// </returns>
public static ATable<TEntity> GetTable<TEntity>(this DataContext context, string name) where TEntity : class
{
// Create/Retrieve a type definition for the table using the TEntity type
Type type = DefineEntityType(typeof(TEntity), name);
// Create the backup table using the new type
ITable refer = context.GetTable(type);
// Prepare the cloning method
Delegate cloneFrom = CompileCloning(typeof(TEntity), type);
// Construct the table wrapper
return new ATable<TEntity>(refer, cloneFrom);
}
/*
/// <summary>
/// The log.
/// </summary>
/// <param name="format">
/// The format.
/// </param>
/// <param name="args">
/// The args.
/// </param>
public static void Log(string format, params object[] args)
{
if (!Verbose)
{
return;
}
Console.Write("*** ");
if ((args == null) || (args.Length == 0))
{
Console.WriteLine(format);
}
else
{
Console.WriteLine(format, args);
}
}*/
#endregion
#region Methods
/// <summary>
/// Clone an attribute
/// </summary>
/// <param name="attr">
/// </param>
/// <returns>
/// </returns>
private static CustomAttributeBuilder CloneColumn(object attr)
{
Type source = attr.GetType();
Type target = typeof(ColumnAttribute);
var props = new List<PropertyInfo>();
var values = new List<object>();
// Extract properties and their values
foreach (PropertyInfo prop in source.GetProperties())
{
if (!prop.CanRead || !prop.CanWrite)
{
continue;
}
props.Add(target.GetProperty(prop.Name));
values.Add(prop.GetValue(attr, null));
}
// Create a new attribute using the properties and values
return new CustomAttributeBuilder(
target.GetConstructor(Type.EmptyTypes), new object[0], props.ToArray(), values.ToArray());
}
/// <summary>
/// Make a delegate that copy content from "source" to "dest"
/// </summary>
/// <param name="source">
/// Source Type
/// </param>
/// <param name="dest">
/// Destination Type
/// </param>
/// <returns>
/// Executable delegate
/// </returns>
private static Delegate CompileCloning(Type source, Type dest)
{
// Input parameter
ParameterExpression input = Expression.Parameter(source);
// For every property, create a member binding
List<MemberBinding> binds =
source.GetProperties().Select(
prop =>
Expression.Bind(
dest.GetProperty(
prop.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly),
Expression.MakeMemberAccess(input, prop))).Cast<MemberBinding>().ToList();
// Expression of creating the new object
MemberInitExpression body = Expression.MemberInit(
Expression.New(dest.GetConstructor(Type.EmptyTypes)), binds);
// The final lambda
LambdaExpression lambda = Expression.Lambda(body, input);
// MJE
//Log("{0}", lambda.ToString());
// Return the executable delegate
return lambda.Compile();
}
/// <summary>
/// Create a class based on the template interface
/// </summary>
/// <param name="template">
/// </param>
/// <param name="name">
/// </param>
/// <returns>
/// </returns>
private static Type DefineEntityType(Type template, string name)
{
// Prepare the builders if not done
if (_assemblyBuilder == null)
{
_assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run);
_moduleBuilder = _assemblyBuilder.DefineDynamicModule("Types");
}
// Check if there is already a type created for that table
if (NamedTypes.ContainsKey(name))
{
return NamedTypes[name];
}
// Create the new type
TypeBuilder tbuilder = null;
if (template.IsInterface)
{
tbuilder = DefineInterfaceChild(name, template);
}
else
{
tbuilder = DefineOverriddenChild(name, template);
}
Type final = tbuilder.CreateType();
NamedTypes[name] = final;
return final;
}
/// <summary>
/// The define interface child.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="template">
/// The template.
/// </param>
/// <returns>
/// </returns>
private static TypeBuilder DefineInterfaceChild(string name, Type template)
{
TypeBuilder tbuilder = _moduleBuilder.DefineType(
name, TypeAttributes.Public, typeof(Object), new[] { template });
// Default constructor
tbuilder.DefineDefaultConstructor(MethodAttributes.Public);
// Attach Table attribute
var abuilder = new CustomAttributeBuilder(
typeof(TableAttribute).GetConstructor(Type.EmptyTypes),
new object[0],
new[] { typeof(TableAttribute).GetProperty("Name") },
new object[] { name });
tbuilder.SetCustomAttribute(abuilder);
List<PropertyInfo> properties = template.GetProperties().ToList(); // May require sorting
// Implement all properties));
foreach (PropertyInfo prop in properties)
{
// Define backing field
FieldBuilder fbuilder = tbuilder.DefineField(
"_" + prop.Name, prop.PropertyType, FieldAttributes.Private);
// Define get method
MethodBuilder pgbuilder = tbuilder.DefineMethod(
"get_" + prop.Name,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig
| MethodAttributes.Virtual | MethodAttributes.Final,
prop.PropertyType,
Type.EmptyTypes);
// Define get method body { return _field; }
ILGenerator ilg = pgbuilder.GetILGenerator();
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldfld, fbuilder);
ilg.Emit(OpCodes.Ret);
// Define set method
MethodBuilder psbuilder = tbuilder.DefineMethod(
"set_" + prop.Name,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig
| MethodAttributes.Virtual | MethodAttributes.Final,
null,
new[] { prop.PropertyType });
// Define set method body { _field = value; }
ILGenerator ils = psbuilder.GetILGenerator();
ils.Emit(OpCodes.Ldarg_0);
ils.Emit(OpCodes.Ldarg_1);
ils.Emit(OpCodes.Stfld, fbuilder);
ils.Emit(OpCodes.Ret);
// Define the property
PropertyBuilder pbuilder = tbuilder.DefineProperty(
prop.Name, PropertyAttributes.None, CallingConventions.Standard, prop.PropertyType, null);
// Set get/set method
pbuilder.SetGetMethod(pgbuilder);
pbuilder.SetSetMethod(psbuilder);
// Attach Column attribute
foreach (object attr in prop.GetCustomAttributes(false))
{
if (attr is ColumnAttribute || attr is AlterColumnAttribute)
{
// MJE
//Log("Create column attribute for {0}", prop.Name);
pbuilder.SetCustomAttribute(CloneColumn(attr));
break;
}
}
}
return tbuilder;
}
/// <summary>
/// The define overridden child.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="template">
/// The template.
/// </param>
/// <returns>
/// </returns>
private static TypeBuilder DefineOverriddenChild(string name, Type template)
{
TypeBuilder tbuilder = _moduleBuilder.DefineType(name, TypeAttributes.Public, template);
// Default constructor
tbuilder.DefineDefaultConstructor(MethodAttributes.Public);
// Attach Table attribute
var abuilder = new CustomAttributeBuilder(
typeof(TableAttribute).GetConstructor(Type.EmptyTypes),
new object[0],
new[] { typeof(TableAttribute).GetProperty("Name") },
new object[] { name });
tbuilder.SetCustomAttribute(abuilder);
List<PropertyInfo> properties = template.GetProperties().ToList(); // May require sorting
// Implement all properties));
foreach (PropertyInfo prop in properties)
{
// Define get method
MethodBuilder pgbuilder = tbuilder.DefineMethod(
"get_" + prop.Name,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig
| MethodAttributes.Virtual | MethodAttributes.Final,
prop.PropertyType,
Type.EmptyTypes);
// Define get method body { return _field; }
ILGenerator ilg = pgbuilder.GetILGenerator();
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Call, template.GetMethod("get_" + prop.Name));
ilg.Emit(OpCodes.Ret);
// Define set method
MethodBuilder psbuilder = tbuilder.DefineMethod(
"set_" + prop.Name,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig
| MethodAttributes.Virtual | MethodAttributes.Final,
null,
new[] { prop.PropertyType });
// Define set method body { _field = value; }
ILGenerator ils = psbuilder.GetILGenerator();
ils.Emit(OpCodes.Ldarg_0);
ils.Emit(OpCodes.Ldarg_1);
ils.Emit(OpCodes.Call, template.GetMethod("set_" + prop.Name));
ils.Emit(OpCodes.Ret);
// Define the property
PropertyBuilder pbuilder = tbuilder.DefineProperty(
prop.Name, PropertyAttributes.None, CallingConventions.Standard, prop.PropertyType, null);
// Set get/set method
pbuilder.SetGetMethod(pgbuilder);
pbuilder.SetSetMethod(psbuilder);
// Attach Column attribute
foreach (object attr in prop.GetCustomAttributes(false))
{
if (attr is ColumnAttribute || attr is AlterColumnAttribute)
{
// MJE
//Log("Create column attribute for {0}", prop.Name);
pbuilder.SetCustomAttribute(CloneColumn(attr));
break;
}
}
}
return tbuilder;
}
#endregion
/// <summary>
/// A table wrapper implements ITable<TEntity> backed by other ITable object
/// </summary>
/// <typeparam name="TEntity">
/// </typeparam>
public class ATable<TEntity> : ITable<TEntity>
where TEntity : class
{
#region Constants and Fields
/// <summary>
/// Cloning method
/// </summary>
private readonly Delegate _clone;
/// <summary>
/// Backing table
/// </summary>
private readonly ITable _internal;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="ATable{TEntity}"/> class.
/// Construct from backing table
/// </summary>
/// <param name="inter">
/// </param>
/// <param name="from">
/// </param>
public ATable(ITable inter, Delegate from)
{
this._internal = inter;
this._clone = from;
}
#endregion
#region Properties
/// <summary>
/// Gets ElementType.
/// </summary>
public Type ElementType
{
get
{
// Use the backing table element
return this._internal.ElementType;
}
}
/// <summary>
/// Gets Expression.
/// </summary>
public Expression Expression
{
get
{
// Use the backing table expression
return this._internal.Expression;
}
}
/// <summary>
/// Gets Provider.
/// </summary>
public IQueryProvider Provider
{
get
{
// Use the backing table provider
return this._internal.Provider;
}
}
#endregion
#region Implemented Interfaces
#region IEnumerable
/// <summary>
/// The get enumerator.
/// </summary>
/// <returns>
/// </returns>
/// <exception cref="NotImplementedException">
/// </exception>
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable<TEntity>
/// <summary>
/// The get enumerator.
/// </summary>
/// <returns>
/// </returns>
/// <exception cref="NotImplementedException">
/// </exception>
public IEnumerator<TEntity> GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region ITable<TEntity>
/// <summary>
/// The attach.
/// </summary>
/// <param name="entity">
/// The entity.
/// </param>
/// <exception cref="NotImplementedException">
/// </exception>
public void Attach(TEntity entity)
{
throw new NotImplementedException();
}
/// <summary>
/// The delete on submit.
/// </summary>
/// <param name="entity">
/// The entity.
/// </param>
public void DeleteOnSubmit(TEntity entity)
{
// Directly invoke the backing table
this._internal.DeleteOnSubmit(entity);
}
/// <summary>
/// The insert on submit.
/// </summary>
/// <param name="entity">
/// The entity.
/// </param>
public void InsertOnSubmit(TEntity entity)
{
// Input entity must be changed to backing type
object v = this._clone.DynamicInvoke(entity);
// Invoke the backing table
this._internal.InsertOnSubmit(v);
}
#endregion
#endregion
}
/// <summary>
/// The alter column attribute.
/// </summary>
public class AlterColumnAttribute : Attribute
{
#region Constants and Fields
/// <summary>
/// The _can be null.
/// </summary>
private bool _canBeNull = true;
/// <summary>
/// The _update check.
/// </summary>
private UpdateCheck _updateCheck = UpdateCheck.Always;
#endregion
#region Properties
/// <summary>
/// Gets or sets AutoSync.
/// </summary>
public AutoSync AutoSync { get; set; }
/// <summary>
/// Gets or sets a value indicating whether CanBeNull.
/// </summary>
public bool CanBeNull
{
get
{
return this._canBeNull;
}
set
{
this._canBeNull = value;
}
}
/// <summary>
/// Gets or sets DbType.
/// </summary>
public string DbType { get; set; }
/// <summary>
/// Gets or sets Expression.
/// </summary>
public string Expression { get; set; }
/// <summary>
/// Gets or sets a value indicating whether IsDbGenerated.
/// </summary>
public bool IsDbGenerated { get; set; }
/// <summary>
/// Gets or sets a value indicating whether IsDiscriminator.
/// </summary>
public bool IsDiscriminator { get; set; }
/// <summary>
/// Gets or sets a value indicating whether IsPrimaryKey.
/// </summary>
public bool IsPrimaryKey { get; set; }
/// <summary>
/// Gets or sets a value indicating whether IsVersion.
/// </summary>
public bool IsVersion { get; set; }
/// <summary>
/// Gets or sets UpdateCheck.
/// </summary>
public UpdateCheck UpdateCheck
{
get
{
return this._updateCheck;
}
set
{
this._updateCheck = value;
}
}
#endregion
}
}
}
Linq-to-Sql cannot materialize interfaces. It needs a class specification to know what instances it should create from a query. The exception message is elusive, to say the least. I don't know why it isn't more to the point.
Note that the class you want to materialize must have been mapped, or: it must be in the dbml. I say this because your ITIPO class is not partial, which makes me wonder how you can make it implement an interface (well, maybe you just slimmed down the code).
Side note: don't use all capitals for class names, and prefix an interface specification with "I", not a class.

Workflow services scalability issue

I'm currently experiencing some issues with workflow services.
They work fine if I start 4, 5 in short sequence, but if I increase this value (starting from ~10) then I get the following exception:
This channel can no longer be used to send messages as the output session was auto-closed due to a server-initiated shutdown. Either disable auto-close by setting the DispatchRuntime.AutomaticInputSessionShutdown to false, or consider modifying the shutdown protocol with the remote server.
I think that the problem is in the way I create proxies. I use the following code to provide proxies, attempting to reuse existing ones:
public abstract class ProxyProvider<TService>
where TService : class
{
/// <summary>
/// Static reference to the current time provider.
/// </summary>
private static ProxyProvider<TService> current = DefaultProxyProvider.Instance;
private TService service;
/// <summary>
/// Gets or sets the current time provider.
/// </summary>
/// <value>
/// The current time provider.
/// </value>
public static ProxyProvider<TService> Current
{
get
{
return ProxyProvider<TService>.current;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
ProxyProvider<TService>.current = value;
}
}
/// <summary>
/// Resets to default.
/// </summary>
public static void ResetToDefault()
{
ProxyProvider<TService>.current = DefaultProxyProvider.Instance;
}
/// <summary>
/// Loads the proxy.
/// </summary>
/// <param name="forceNew">if set to <c>true</c> [force new].</param>
/// <returns>The instance of the proxy.</returns>
public virtual TService Provide(bool forceNew = false)
{
if (forceNew || !this.IsInstanceValid())
{
this.service = this.CreateInstance();
return this.service;
}
return this.service;
}
/// <summary>
/// Internals the load.
/// </summary>
/// <returns>The new created service.</returns>
protected abstract TService CreateInstance();
private bool IsInstanceValid()
{
var instance = this.service as ICommunicationObject;
if (instance == null)
{
return false;
}
return instance.State != CommunicationState.Faulted && instance.State != CommunicationState.Closed && instance.State != CommunicationState.Closing;
}
/// <summary>
/// Defines the default <see cref="ProxyProvider<TService>"/> which uses the System DateTime.UtcNow value.
/// </summary>
private sealed class DefaultProxyProvider : ProxyProvider<TService>
{
/// <summary>
/// Reference to the instance of the <see cref="ProxyProvider<TService>"/>.
/// </summary>
private static ProxyProvider<TService> instance;
/// <summary>
/// Gets the instance.
/// </summary>
public static ProxyProvider<TService> Instance
{
get
{
if (DefaultProxyProvider.instance == null)
{
DefaultProxyProvider.instance = new DefaultProxyProvider();
}
return DefaultProxyProvider.instance;
}
}
/// <summary>
/// Loads the specified force new.
/// </summary>
/// <returns>A non-disposed instance of the given service.</returns>
protected override TService CreateInstance()
{
var loadedService = Activator.CreateInstance<TService>();
return loadedService;
}
}
With an additional "lazy" provider:
public class CustomConstructorProxyProvider<TService> : ProxyProvider<TService>
where TService : class
{
private readonly Func<TService> constructor;
/// <summary>
/// Initializes a new instance of the <see cref="CustomConstructorProxyProvider<TService>"/> class.
/// </summary>
/// <param name="constructor">The constructor.</param>
public CustomConstructorProxyProvider(Func<TService> constructor)
{
this.constructor = constructor;
}
/// <summary>
/// Internals the load.
/// </summary>
/// <returns>The new created service.</returns>
protected override TService CreateInstance()
{
var service = this.constructor();
return service;
}
}
Used this way:
var proxy = ProxyProvider<IWorkflowService>.Current.Provide();
proxy.DoSomething();
Initialized like this:
ProxyProvider<IWorkflowService>.Current = new CustomConstructorProxyProvider<IWorkflowService>(() => new WorkflowServiceProxy("endpoint"));
Workflow services are hosted by IIS and I added the following throttling settings:
<serviceThrottling
maxConcurrentCalls="512"
maxConcurrentInstances="2147483647"
maxConcurrentSessions="1024"/>
which should be enough for my needs.
I hope that someone can help me configuring client and server to have achieve the desired scalability (a few hundreds started in sequence and running in parallel, using the WorkflowInstance sql store).
UPDATE:
I'm using NetTcpBinding for all services.
UPDATE 2:
All services are hosted and consumed by now locally.
Thanks
Francesco

Can I associate a connection string with an endpoint in WCF?

We have a WCF REST service that connects to a database. In fact, we have several instances of the database, all with the same schema.
We would like to set up one endpoint for each database instance and associate a connection string with the endpoint. The service would read the connection string and connect to the appropriate SQL Server instance.
I'm sure this is possible; is it a good idea? How do I set it up? Is there documentation on MSDN?
Edit: I found this question, where the answer suggests adding connection information on the client in a header. I don't want to do that—for security reasons, and because I do want to have a distinct uri for each database.
This was a bit harder than I thought. WCF has so many extensibility points its hard to pick the right one. Please answer or comment if you think there's a better way, or anything wrong with this.
I've settled on using a custom class that implements IEndpointBehavior and IDispatchMessageInspector. I have a class derived from BehaviorExtensionElement that lets me associate the behavior with an endpoint in configuration. This blog post describes hot do do that.
My DatabaseConnectionContext class looks like this:
/// <summary>
/// An endpoint behavior that associates a database connection string name with the endpoint and adds it to the
/// properties of incoming messages.
/// </summary>
public class DatabaseConnectionContext : IEndpointBehavior, IDispatchMessageInspector
{
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseConnectionContext"/> class with the provided connection string name.
/// </summary>
/// <param name="connectionStringName">The name of the connection string to associate with the endpoint.</param>
public DatabaseConnectionContext(string connectionStringName)
{
this.ConnectionStringName = connectionStringName;
}
/// <summary>
/// Gets the name of the connection string to associate with the endpoint.
/// </summary>
public string ConnectionStringName { get; private set; }
/// <inheritdoc />
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
/// <inheritdoc />
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
}
/// <inheritdoc />
public void Validate(ServiceEndpoint endpoint)
{
}
/// <inheritdoc />
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
request.Properties["connectionStringName"] = this.ConnectionStringName;
return null;
}
/// <inheritdoc />
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
In my service class I have this method:
/// <summary>
/// Returns the connection string to use for this service call.
/// </summary>
/// <returns>A SQL Server database connection string.</returns>
private string GetConnectionString()
{
string connectionStringName = (string)OperationContext.Current.IncomingMessageProperties["connectionStringName"];
return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}
My BehaviorExtensionElement class looks like this:
/// <summary>
/// Associates a <see cref="DatabaseConnectionContext"/> with an endpoint in configuration.
/// </summary>
public class DatabaseConnectionContextBehaviorExtension : BehaviorExtensionElement
{
/// <summary>
/// The name of the <see cref="ConnectionStringName"/> property when it appears in a configuration file.
/// </summary>
private const string ConnectionStringNamePropertyName = "connectionStringName";
/// <summary>
/// Gets or sets the name of the configuration string to associate with the endpoint.
/// </summary>
[ConfigurationProperty(ConnectionStringNamePropertyName)]
public string ConnectionStringName
{
get
{
return (string)this[ConnectionStringNamePropertyName];
}
set
{
this[ConnectionStringNamePropertyName] = value;
}
}
/// <inheritdoc />
public override Type BehaviorType
{
get { return typeof(DatabaseConnectionContext); }
}
/// <inheritdoc />
protected override object CreateBehavior()
{
return new DatabaseConnectionContext(this.ConnectionStringName);
}
}
My web.config contains something like this:
<behaviors>
<endpointBehaviors>
<behavior name="DevRestEndpointConfiguration">
<webHttp helpEnabled="false" />
<connectionStringInterceptor connectionStringName="myDevConnectionStringName" />
</behavior>
<behavior name="ProductionRestEndpointConfiguration">
<webHttp helpEnabled="false" />
<connectionStringInterceptor connectionStringName="myProductionConnectionStringName" />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="connectionStringInterceptor" type="DatabaseConnectionContextBehaviorExtension, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
Each <endpoint /> element in the <services /> section has its behaviorConfiguration set to the name of an appropriate element from the <endpointBehaviors /> section.
why don't you add a new parameters specifying what is the database the call will connect?
for example:
you can add a db parameters that will get a number and from there you will connect
you can add such parameter on the authentication method
as the example for the first item:
public ProductItem GetProduct(int productId, int db = 1)
{
ProductItem product = new ProductItem();
string connectionString = getConnectionStringForDb(db);
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("SELECT name, price FROM Products WHERE productId = #product;", connection);
command.Parameters.AddWithValue("#product", productId);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
reader.Read();
product = new product({
Name = reader[0],
Price = reader[1]
});
reader.Close();
}
catch (Exception ex)
{
// Log exception
}
}
return product;
}
taken from MSDN
private string getConnectionStringForDb(int type)
{
System.Configuration.ConnectionStringSettings connString;
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0) {
connString = rootWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString_" + type];
if (connString == null) {
// LOG ERROR
}
}
return connString.ConnectionString;
}
and just add your connection strings in your web.config and name then like:
DBConnectionString_1, DBConnectionString_2, DBConnectionString_3
or anything that makes sense to you.
<connectionStrings>
<add
name="DBConnectionString_1"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
<add
name="DBConnectionString_2"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
<add
name="DBConnectionString_3"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
With this in your web.config:
<configuration>
<appSettings>
<add key="Foo.svc" value="tagvalue1"/>
</appSettings>
...
You could retrieve the value at runtime this way:
private static string GetConfigValue()
{
ServiceEndpointCollection ec = OperationContext.Current
.Host.Description.Endpoints;
if (ec!=null)
{
var segments = ec[0].Address.ToString().Split('/');
var s = segments[segments.Length-1]; // "Foo.svc"
return ConfigurationManager.AppSettings[s]; // "tagvalue1"
}
return null;
}

Protobuf net not serializing when using WCF

I am trying to use protobuf to serialize my WCF calls, but it seems the object is not getting serialized by the client. Some things to note:
I am using a shared DTO library.
I am using a ChannelFactory to
invoke the service (so the types are not losing their datamember
attributes).
I can serialize and deserialize the objects just using
normal protobuf.net code, so the types themselves seem to be ok
I am using version 2.0.0.480 of protobuf.net
I haven't posted the service code as the problem is with the outgoing message (message log posted below)
The client and service work fine if I don't use the protobuf endpoint behaviour.
My main method looks as follows
static void Main(string[] args)
{
Base.PrepareMetaDataForSerialization();
FactoryHelper.InitialiseFactoryHelper(new ServiceModule());
Member m = new Member();
m.FirstName = "Mike";
m.LastName = "Hanrahan";
m.UserId = Guid.NewGuid();
m.AccountStatus = MemberAccountStatus.Blocked;
m.EnteredBy = "qwertt";
ChannelFactory<IMembershipService> factory = new ChannelFactory<IMembershipService>("NetTcpBinding_MembershipService");
var client = factory.CreateChannel();
using (var ms = new MemoryStream())
{
Serializer.Serialize<Member>(ms, m);
Console.WriteLine(ms.Length.ToString());
ms.Position = 0;
var member2 = Serializer.Deserialize<Member>(ms);
Console.WriteLine(member2.EnteredBy);
Console.WriteLine(member2.FirstName);
}
var result = client.IsMemberAllowedToPurchase(m);
System.Console.Write(result.IsValid.ToString());
factory.Close();
var input = Console.ReadLine();
}
My client configuration looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_Common" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
maxBufferSize="1000065536" maxConnections="10" maxReceivedMessageSize="1000000">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="Proto.Common.EndpointBehavior">
<protobuf />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.480, Culture=neutral, PublicKeyToken=257b51d87d2e4d67" />
</behaviorExtensions>
</extensions>
<client>
<endpoint address="net.tcp://mikes-pc:12002/MembershipService.svc"
behaviorConfiguration="Proto.Common.EndpointBehavior" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_Common" contract="PricesForMe.Core.Entities.ServiceInterfaces.IMembershipService"
name="NetTcpBinding_MembershipService">
<identity>
<userPrincipalName value="Mikes-PC\Mike" />
</identity>
</endpoint>
</client>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="E:\Work\Logs\IMembershipServiceWcfTrace_Client.svclog" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="E:\Work\Logs\IMembershipServiceWcfTrace_Client_messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
After logging the client message, I get the following entry in the log
<MessageLogTraceRecord>
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.pricesforme.com/services/MembershipService/IsMemberAllowedToPurchase</a:Action>
<a:MessageID>urn:uuid:8b545576-c453-4be6-8d5c-9913e2cca4bf</a:MessageID>
<ActivityId CorrelationId="b4e9361f-1fbc-4b2d-b7ee-fb493847998a" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">6d712899-62fd-4547-9517-e9de452305c6</ActivityId>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink"></VsDebuggerCausalityData>
</s:Header>
<s:Body>
<IsMemberAllowedToPurchase xmlns="http://www.pricesforme.com/services/">
<proto></proto>
</IsMemberAllowedToPurchase>
</s:Body>
</s:Envelope>
</MessageLogTraceRecord>
So as can been seen in the above log message the proto entry has no data in it. My member class looks as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using PricesForMe.Core.Entities.Common;
using PricesForMe.Core.Entities.Ordering;
namespace PricesForMe.Core.Entities.Members
{
/// <summary>
/// This entity represents a member or user of the site.
/// </summary>
[DataContract]
[Serializable]
public class Member: User
{
public Member()
:base()
{
EntityType = Entities.EntityType.Member;
}
[DataMember(Order = 20)]
public int Id { get; set; }
[DataMember(Order = 21)]
public string MemberName { get; set; }
[DataMember(Order = 22)]
public PaymentInfo DefaultPaymentMethod { get; set; }
[DataMember(Order = 23)]
public MemberAccountStatus AccountStatus { get; set; }
#region static
public static readonly string CacheCollectionKey = "MemberCollection";
private static readonly string CacheItemKeyPrefix = "Member:";
public static string GetCacheItemKey(int id)
{
return CacheItemKeyPrefix + id.ToString();
}
#endregion
}
}
The parent user class looks as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Diagnostics.Contracts;
namespace PricesForMe.Core.Entities.Common
{
/// <summary>
/// This class represents a user in the system. For example, a user could be a member or a merchant user.
/// </summary>
[DataContract]
[Serializable]
public class User: Base
{
public User()
:base()
{
EntityType = Entities.EntityType.User;
}
[DataMember(Order = 10)]
public Guid UserId { get; set; }
[DataMember(Order = 11, Name = "First Name")]
public string FirstName { get; set; }
[DataMember(Order = 12, Name = "Last Name")]
public string LastName { get; set; }
}
}
}
And the base class looks as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Diagnostics.Contracts;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using ProtoBuf.Meta;
namespace PricesForMe.Core.Entities
{
/// <summary>
/// This is the base class for all entities involved in the request/response pattern of our services
/// </summary>
/// <remarks>
/// The objects derived from this class are used to transfer data from our service classes to our UIs and back again and they should
/// not contain any logic.
/// </remarks>
[DataContract]
[Serializable]
public abstract class Base
{
public Base()
{
//Set some defaults for this
EnteredBy = System.Environment.UserName;
EnteredSource = System.Environment.MachineName;
}
/// <summary>
/// This is the record timestamp
/// </summary>
[DataMember(Order = 2)]
public DateTime RecordTimeStamp { get; set; }
/// <summary>
/// This is the name of the user who last edited the entity
/// </summary>
[DataMember(Order = 3)]
public string EnteredBy { get; set; }
/// <summary>
/// This is the source of the last edited entity
/// </summary>
[DataMember(Order = 4)]
public string EnteredSource { get; set; }
[DataMember(Order = 5)]
private PricesForMe.Core.Entities.Common.ValidationResult _validationResult = null;
/// <summary>
/// Data on the validity of the entity.
/// </summary>
public PricesForMe.Core.Entities.Common.ValidationResult ValidationData
{
get
{
_validationResult = Validate();
return _validationResult;
}
set
{
_validationResult = value;
}
}
/// <summary>
/// Flag denoting if the record is a new record or not.
/// </summary>
/// <remarks>
/// To flag an entity as an existing record call the "FlagAsExistingReport()" method.
/// </remarks>
public bool IsNewRecord
{
get
{
return _isNewRecord;
}
}
[DataMember(Order = 6)]
protected bool _isNewRecord = true;
/// <summary>
/// Flags the entity as a record that already exists in the database
/// </summary>
/// <remarks>
/// This is a method rather than a field to demonstrait that this should be called with caution (as opposed to inadvertantly setting a flag!)
/// <para>
/// Note that this method should only need to be called on object creation if the entity has a composite key. Otherwise the flag is
/// set when the id is being set. It should always be called on saving an entity.
/// </para>
/// </remarks>
public void FlagAsExistingRecord()
{
_isNewRecord = false;
}
public virtual PricesForMe.Core.Entities.Common.ValidationResult Validate()
{
if (_validationResult == null)
{
_validationResult = new PricesForMe.Core.Entities.Common.ValidationResult();
_validationResult.MemberValidations = new List<Common.ValidationResult>();
_validationResult.RulesViolated = new List<Common.ValidationRule>();
}
return _validationResult;
}
/// <summary>
/// This is the type of entity we are working with
/// </summary>
[DataMember(Order = 7)]
private EntityType _entityType = EntityType.Unknown;
public EntityType EntityType
{
get
{
return _entityType;
}
protected set
{
_entityType = value;
}
}
/// <summary>
/// Flag to say if the id generated for this class need to be int64 in size.
/// </summary>
[DataMember(Order = 9)]
public bool IdRequiresInt64 { get; protected set; }
/// <summary>
/// This method tells us if the database id has been assigned. Note that this does
/// not mean the entity has been saved, only if the id has been assigned (so the id could be greater than 0, but the
/// entity could still be a NewRecord
/// </summary>
/// <returns></returns>
[DataMember(Order = 8)]
public bool HasDbIdBeenAssigned { get; protected set; }
private Guid _validationId = Guid.NewGuid();
public Guid EntityValidationId
{
get
{
return _validationId;
}
}
/// <summary>
/// Converts an object into another type of object based on the mapper class provided.
/// </summary>
/// <remarks>
/// This method allows us to easily convert between objects without concerning ourselves with the mapping implementation. This
/// allows us to use various mapping frameworks (e.g. Automapper, ValueInjector) or create our own custom mapping.
/// </remarks>
/// <typeparam name="TDestination">The type we want to convert to</typeparam>
/// <typeparam name="KMapper">The mapping type</typeparam>
/// <returns>The new type</returns>
public TDestination ConvertTo<TDestination, TSource, KMapper>()
where KMapper : IEntityMapper<TDestination, TSource>
where TSource : class
{
return Base.ConvertToItem<TDestination, TSource, KMapper>(this as TSource);
}
/// <summary>
/// Returns all known child types
/// </summary>
public IEnumerable<Type> GetAllTypes()
{
Assembly current = Assembly.GetCallingAssembly();
List<Type> derivedTypes = new List<Type>();
var allTypes = current.GetTypes();
foreach (var t in allTypes)
{
if (t.IsAssignableFrom(typeof(Base)))
{
derivedTypes.Add(t);
}
}
return derivedTypes;
}
#region Static Methods
/// <summary>
/// Converts a list of one type to a list of another type
/// </summary>
/// <typeparam name="TDestination">The type we want to convert to</typeparam>
/// <typeparam name="TSource">The source type</typeparam>
/// <typeparam name="KMapper">The mapper class</typeparam>
/// <param name="source">The source list of items.</param>
/// <returns></returns>
public static List<TDestination> ConvertToList<TDestination, TSource, KMapper>(IEnumerable<TSource> source)
where KMapper : IEntityMapper<TDestination, TSource>
where TSource : class
{
List<TDestination> result = new List<TDestination>();
KMapper mapper = Activator.CreateInstance<KMapper>();
foreach (var item in source)
{
result.Add(mapper.Convert(item));
}
return result;
}
public static TDestination ConvertToItem<TDestination, TSource, KMapper>(TSource source)
where KMapper : IEntityMapper<TDestination, TSource>
where TSource : class
{
//Return default (i.e. null for ref objects) if the source is null.
if (source == null) { return default(TDestination); }
KMapper mapper = Activator.CreateInstance<KMapper>();
return mapper.Convert(source);
}
private static object _metaLock = new object();
private static bool _metaDataPrepared = false;
/// <summary>
/// Creates protobuf type models from the entities in this assembly
/// </summary>
public static void PrepareMetaDataForSerialization()
{
lock (_metaLock)
{
if (_metaDataPrepared) { return; }
Assembly current = Assembly.GetExecutingAssembly();
var allTypes = current.GetTypes();
foreach (var t in allTypes)
{
checkType(t);
}
}
}
private static void checkType(Type type)
{
Assembly current = Assembly.GetExecutingAssembly();
var allTypes = current.GetTypes();
int key = 1000;
foreach (var t in allTypes)
{
if (t.IsSubclassOf(type) && t.BaseType == type)
{
RuntimeTypeModel.Default[type].AddSubType(key, t);
key++;
}
}
}
#endregion
}
}
The PrepareMetaDataForSerialization method on base configures the RuntimeModel for protobuf.net, but I mentioned earlier, the serialization and deserialization works fine outside of the WCF, so I think the DTOs are ok. Any ideas around what could be causing the problem are greatly appreciated.
k; the element name looks correct (proto, matching XmlProtoSerializer.PROTO_ELEMENT), so protobuf-net definitely tried to do something. It also doesn't include #nil to represent null, so it knows there was data. Beyond that, it serializes the object to a MemoryStream and writes it as base-64 (identical to how byte[] etc is represented, which allows WCF to hoist the data silently and automatically if things like MTOM are enabled). So the question becomes "why would my type serialize to nothing?"
The use of DataContract/DataMember is fine, and matches my existing WCF integration tests.
I wonder how much of this is due to the inheritance (only one of the members shown relates to the concrete type, and I would hazard a guess that Blocked is 0, which has particular handling).
I cannot, however, emphasise enough how unsafe your current inheritance handling is; the numbers matter, and reflection makes no guarantees re order. I would strongly suggest you revisit this and make inheritance numbering more predicatable.
Very minor observation, but there is no need to store EntityType - it is entirey redundant and could be handled via polymorphism with no need for storage.
Also, there's an important bug that _metaDataPrepared is never set to true.
However! Ultimately I cannot reproduce this; I have used your code (or most of it) to generate an integration test, and - it passes; meaning: using WCF, NetTcpBinding, your classes (including your inheritance fixup code), and the protobuf packing, it just works. The data that goes over the wire is the data we expect.
Happy to try to help further, but I need to be able to repro it... and right now, I can't.
The first thing I'd do is add the missing _metaDataPrepared = true; to see if that helps.
I thought you need to use ProtoContract instead of DataContract per this SO? Also, make sure you set "Reuse types in referenced assemblies" when configuring service reference. And according to this SO they support data contracts but you have to have the order set [DataMember(Order = 0)] (at least that's what worked for me).

Biztalk set WCF MessageHeaders in Biztalk message assignment

I'm trying to set the MessageHeader of a message that my Biztalk orchestration is sending to WCF. The Message Contract looks as follows in the WCF project.
[MessageContract(IsWrapped = true, WrapperName = "PerformTransformationRequestWrapper", WrapperNamespace="http://www.iasreo.com/integration/servicetypes")]
public class PerformTransformationRequest
{
[MessageHeader(Namespace = "http://www.iasreo.com/integration/servicetypes")]
public string Version { get; set; }
/// <summary>
///
/// </summary>
[MessageHeader(Namespace = "http://www.iasreo.com/integration/servicetypes", MustUnderstand = true)]
public TransformType TransformType { get; set; }
/// <summary>
///
/// </summary>
[MessageBodyMember(Namespace = "http://www.iasreo.com/integration/servicetypes")]
public System.IO.Stream Payload { get; set; }
}
TransformType.cs
[DataContract(Namespace = "http://www.iasreo.com/integration/servicetypes")]
public enum TransformType
{
/// <summary>
/// Transform to Excel
/// </summary>
[EnumMember]
ExcelTransform = 1,
/// <summary>
/// Transform to PDF
/// </summary>
[EnumMember]
PDFTransform = 2
}
I'm creating the message to send to Biztalk in a Message Assignment shape and that code looks as follows:
xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(#"<ns0:PerformTransformationRequestWrapper xmlns:ns0=""http://www.iasreo.com/integration/servicetypes""><ns0:Payload>GpM7</ns0:Payload></ns0:PerformTransformationRequestWrapper>");
PerformTransformationRequest = xmlDoc;
xpath(PerformTransformationRequest, "/*[local-name()='PerformTransformationRequestWrapper' and namespace-uri()='http://www.iasreo.com/integration/servicetypes']/*[local-name()='Payload']") = System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(Input.OuterXml));
How can I set the Version and TransformType that MessageHeaders in the WCF MessageContract?
You can set the promoted WCF.OutboundCustomHeaders property of the outgoing message
OutboundMessage(WCF.OuboundCustomHeaders) = "...";
You need to supply the complete WCF Header string as the parameter.
This http://bencode.net/biztalk-custom-wcf-behaviour might be of use to you.