gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:47:09 25 4
gpt4 key购买 nike

我正在尝试对原始消息使用 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 未处理 Message="第 1 行位置 276 出错。期望来自命名空间'http://schemas.microsoft.com/2003/10/Serialization/'的元素'string'.. 遇到名称为'Fault'、命名空间'http://www.w3.org/2003/05/soap-envelope'的'Element'。" Source="System.Runtime.Serialization" 堆栈跟踪: 在 System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader,Boolean verifyObjectName) 在 System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator 读取器,Boolean verifyObjectName) 在 System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader 阅读器) 在 System.ServiceModel.Channels.Message.GetBody[T](XmlObjectSerializer 序列化程序) 在 System.ServiceModel.Channels.Message.GetBodyT 在 ClientTestService.Program.Main(String[] args) 在 E:\Projekti\WCF\Parus\ClientTestService\ClientTestService\Program.cs:line 22 在 System.AppDomain._nExecuteAssembly(Assembly 程序集,String[] args) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,字符串 [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:

在线:

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

服务已启动,我在 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();
}

如果有人能提供帮助,我将不胜感激。

最佳答案

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

关于WCF:使用 System.ServiceModel.Channels.Message 类的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12832705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com