我正在尝试将 WCF 服务与原始消息一起使用。
1)WCF服务代码:
[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)客户端代码:
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();
}
当我运行客户端时: int value = 1 一切正常。但是,当我使用:int value = 2 时,出现以下错误:
第 1 行位置 276 出错。从命名空间“http://schemas.microsoft.com/2003/10/Serialization/”中期待元素“字符串”。遇到名称为“故障”的“元素”,命名空间“http://” www.w3.org/2003/05/soap-envelope'。
在线:
string firstName = reply.GetBody<string>();
服务已启动,我在 VS2008 中通过“添加服务引用...”添加了服务引用。我使用 .NET Framework 3.5。
我不确定为什么会收到此错误。
提前感谢您的帮助。
戈兰