WCF:使用System.ServiceModel.Channels.Message类的问题?

时间:2012-10-11 05:20:07

标签: wcf message

我尝试将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时,我收到以下错误:

System.Runtime.Serialization.SerializationException未处理   消息=&#34;第1行位置错误276.期望元素&#39;字符串&#39;来自命名空间&#39; http://schemas.microsoft.com/2003/10/Serialization/&#39; ..遇到&#39;元素&#39;名称&#39;故障&#39;,命名空间&#39; http://www.w3.org/2003/05/soap-envelope&#39;。 &#34;   源=&#34; System.Runtime.Serialization&#34;   堆栈跟踪:        在System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader,Boolean verifyObjectName)        在System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader,Boolean verifyObjectName)        在System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)        在System.ServiceModel.Channels.Message.GetBody [T](XmlObjectSerializer序列化程序)        在System.ServiceModel.Channels.Message.GetBodyT        atE:\ Projekti \ WCF \ Parus \ ClientTestService \ ClientTestService \ Program.cs中的ClientTestService.Program.Main(String [] args):第22行        在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)        在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)        在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()        在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)        在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)        在System.Threading.ThreadHelper.ThreadStart()   InnerException:

在线:

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

服务已启动,我已通过&#34;添加服务参考...&#34;添加了服务参考。在VS2008中。我使用的是.NET Framework 3.5。

我不确定为什么我会收到此错误。

当我不使用Message类时,一切正常。我的意思是:

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    Person GetPersonById(int id);
}

public class TestService : ITestService
{
    public Person GetPersonById(int id)
    {
        Person person = Employees.CreateEmployees().First(e => e.Id == id);
        return person;
    }
}

2)客户代码:

static void Main(string[] args)
{
    TestServiceClient client = new TestServiceClient();
    int value = 1;
    Person person = client.GetPersonById(value);

    Console.WriteLine(person.FirstName);
    client.Close();
}

如果有人可以提供帮助,我会非常感激。

1 个答案:

答案 0 :(得分:1)

Message reply = client.GetPersonById(request); 
if(reply.IsFault)
  throw new GoransException(reply.toString());
string firstName = reply.GetBody<string>();
//...