tesicg
tesicg

Reputation: 4053

How to use WCF service with raw messages?

I'm trying to use WCF service with raw messages.

1) WCF service code:

[DataContract]
public class Person
{
    [DataMember]
    public int Id { get; set; }

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

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

public static List<Person> CreateEmployees()
{
    List<Person> lstPersons = new List<Person>()
    {
        new Person { Id = 1, FirstName = "Andrey", LastName = "Andreyev" },
        new Person { Id = 2, FirstName = "Sergey", LastName = "Sergeyev" }
    };

    return lstPersons;
}

[ServiceContract]
public interface ITestService
{
    [OperationContract(Action = TestService.RequestAction, ReplyAction = TestService.ReplyAction)]
    Message GetPersonById(Message id);
}

public class TestService : ITestService
{
    public const String ReplyAction = "http://localhost:4249/Message_ReplyAction";
    public const String RequestAction = "http://localhost:4249/Message_RequestAction";

    public Message GetPersonById(Message id)
    {
        string firstName = Employees.CreateEmployees().First(e => e.Id == id.GetBody<int>()).FirstName;
        Message response = Message.CreateMessage(id.Version, ReplyAction, firstName);
        return response;
    }
}

2) Client code:

static void Main(string[] args)
{
    TestServiceClient client = new TestServiceClient();
    String RequestAction = "http://localhost:4249/Message_RequestAction";
    int value = 1;
    Message request = Message.CreateMessage(MessageVersion.Default, RequestAction, value);
    Message reply = client.GetPersonById(request);
    string firstName = reply.GetBody<string>();

    Console.WriteLine(firstName);
    client.Close();
}

When I run the client with: int value = 1 everything works fine. But, when I use: int value = 2 I get the following error:

Error in line 1 position 276. Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'Fault', namespace 'http://www.w3.org/2003/05/soap-envelope'.

At line:

string firstName = reply.GetBody<string>();

The service is started and I've added the service reference through "Add Service Reference..." in VS2008. I use .NET Framework 3.5.

I'm not sure why I'm getting this error.

Thank you in advance for help.

Goran

Upvotes: 1

Views: 1062

Answers (1)

Eugene Osovetsky
Eugene Osovetsky

Reputation: 6541

Well, you're getting a SOAP Fault. Try logging the message (either with Fiddler, with WCF Logging, or by just reading from Message.GetReaderAtBodyContents from the message you get from the server on the client-side), and see what the fault actually says. Make sure you turn on IncludeExceptionDetailInFaults on the server side.

Upvotes: 1

Related Questions