Git Product home page Git Product logo

Comments (11)

esskar avatar esskar commented on August 22, 2024

you have to define the MyType in a shared library, and reference it both, on the client and the server.
tell me, if that works.

from serialize.linq.

gromer avatar gromer commented on August 22, 2024

This all happens well before any WCF usage, since it fails when it's converting the Expression I made to the ExpressionNode.

from serialize.linq.

gromer avatar gromer commented on August 22, 2024

Full code to my test console app:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Security;
using System.ServiceModel;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Globalization;

using Serialize.Linq;
using Serialize.Linq.Extensions;
using Serialize.Linq.Nodes;

using System.Linq.Expressions;

class Program
{
    public class MyType
    {
        public int Id { get; set; }
    }

    static void Main(string[] args)
    {
        var ids = new List<int> { 1, 2, 3, 4, 5, 6 };
        Expression<Func<MyType, bool>> expression1 = mt => mt.Id == 1 || mt.Id == 5 || mt.Id == 6;
        var node1 = expression1.ToExpressionNode();

        try
        {
            Expression<Func<MyType, bool>> expression2 = mt => ids.Contains(mt.Id);
            var node2 = expression2.ToExpressionNode();
        }
        catch
        {

        }
    }
}

Not using the WCF service in this, but it will break inside the try block.

from serialize.linq.

esskar avatar esskar commented on August 22, 2024

fixed in commit: c4e3ce8

from serialize.linq.

gromer avatar gromer commented on August 22, 2024

Nice! My ExpressionNode gets created fine now. I have one more issue, but I'm thinking it's because I'm not implementing your library correctly. I'm getting an exception when serialization occurs:

System.ServiceModel.CommunicationException was unhandled
  Message=There was an error while trying to serialize parameter http://tempuri.org/:query. The InnerException message was 'Type 'System.Int32[]' with data contract name 'ArrayOfint:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.
  Source=mscorlib

I'm almost certain my code isn't using your serializers at all. I saw you added all the known type code to the DataSerializer class, but my code never hits GetKnownTypes, nor any of the To[Format] extension methods. I think I'm missing some small step somewhere. Like I said, I don't think this is a bug, just me not using the library correctly. I'm getting the ExpressionNode, and passing it to my service. Works find with expressions like x => x.Id == 5, but when I introduce the .Contains into it, it breaks when serializing.

from serialize.linq.

esskar avatar esskar commented on August 22, 2024

is it possible for you to switch from List to int[], like
var ids = new [] { 1, 2, 3, 4, 5, 6 };

from serialize.linq.

gromer avatar gromer commented on August 22, 2024

Actually, that's what I did. First run I got that error for the List<int>, so I tried int[]. Same error, just with int[]. The exception message I included above was for the array.

from serialize.linq.

esskar avatar esskar commented on August 22, 2024

hmm. can you try to build a unit test that reproduces the problem.
it is hard for me to reproduce it.

from serialize.linq.

gromer avatar gromer commented on August 22, 2024

Not a unit test per say, but code you can run in a console app to see what I'm talking about.

[DataContract]
public class Person
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}

[ServiceContract]
public interface IPersonService
{
    [OperationContract]
    Person[] GetAllPersons();

    [OperationContract]
    Person[] QueryPersons(ExpressionNode query);
}

public class PersonService : IPersonService
{
    private IList<Person> _persons = new List<Person>
    {
        new Person { FirstName="Bill", LastName="Gates" },
        new Person { FirstName="John", LastName="Doe" },
        new Person { FirstName="Jane", LastName="Smith" },
        new Person { FirstName="Fred", LastName="Gates" }
    };

    public Person[] GetAllPersons()
    {
        return _persons.ToArray();
    }

    public Person[] QueryPersons(ExpressionNode query)
    {
        var expression = query.ToExpression<Func<Person, bool>>();
        return _persons.Where(expression.Compile()).ToArray();
    }
}

static void Main(string[] args)
{
    ServiceHost personHost = null;

    try
    {
        personHost = new ServiceHost(typeof(PersonService));

        ServiceEndpoint productEndpoint = personHost.AddServiceEndpoint(typeof(IPersonService), new NetTcpBinding(), "net.tcp://localhost:9010/ProductService");
        personHost.Open();

        Console.WriteLine("The Product service is running and is listening on:");
        Console.WriteLine("{0} ({1})", productEndpoint.Address.ToString(), productEndpoint.Binding.Name);
        Console.WriteLine("\nPress any key to stop the service.");

        var myBinding = new NetTcpBinding();
        var myEndpoint = new EndpointAddress("net.tcp://localhost:9010/ProductService");
        var myChannelFactory = new ChannelFactory<IPersonService>(myBinding, myEndpoint);
        IPersonService client = null;
        try
        {
            client = myChannelFactory.CreateChannel();

            // This works fine.
            var persons = client.GetAllPersons();
            Console.WriteLine("{0} persons", persons.Count());

            Expression<Func<Person, bool>> expression1 = p => p.LastName == "Gates";
            // This works fine.
            var matches1 = client.QueryPersons(expression1.ToExpressionNode());
            Console.WriteLine("{0} persons", matches1.Count());

            var firstNames = new string[] { "Bill", "Jim", "Teddy", "Jane" };
            Expression<Func<Person, bool>> expression2 = p => firstNames.Contains(p.FirstName);
            // This will break.
            var matches2 = client.QueryPersons(expression2.ToExpressionNode());
            Console.WriteLine("{0} persons", matches2.Count());

            ((ICommunicationObject)client).Close();
        }
        catch
        {
            if (client != null)
            {
                ((ICommunicationObject)client).Abort();
            }
        }

        Console.ReadKey();
    }
    finally
    {
        if (personHost.State == CommunicationState.Faulted)
        {
            personHost.Abort();
        }
        else
        {
            personHost.Close();
        }
    }
}

The only one to break is when I'm involving an array.

from serialize.linq.

esskar avatar esskar commented on August 22, 2024

this is different, i think this now on WCF. WCF uses its own DataContractSerializer.
Find here http://msdn.microsoft.com/en-us/library/ee358759.aspx an example on binding an own datacontractresolver. maybe i will add one in the next version.
thanks for testing and reporting.

from serialize.linq.

gromer avatar gromer commented on August 22, 2024

Oh, I thought the library worked with WCF already, as one of your blog posts on the library was using it with WCF. No worries, thanks!

from serialize.linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.