admin管理员组文章数量:1395381
I'm running through a couple of tutourials on WCF and run into a snag with my own custom classes.
I have the following data class housed within a WCF service (Class Library):
[DataContract]
public class Hello
{
[DataMember]
public int HelloId { get; set; }
[DataMember]
public string HelloName { get; set; }
}
The service class is set up as follows:
public class HelloService : IHelloService
{
public string GetMessage() => "Hi";
public Hello GetHello() => new Hello() { HelloId = 1, HelloName= "Hi" };
}
This service is hosted by a basic console application:
static void Main(string[] args)
{
using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService.HelloService))) // Both the solution and class go by the same name - HelloService
{
serviceHost.Open();
Console.WriteLine("Host started @ " + DateTime.Now.ToString("HH:mm:ss"));
Console.ReadLine();
}
}
Then comes my problem. Next, I set up another windows form application to consume this hosted service. I connected to the service by adding it as a service reference. In the code behind, I set up a button click event handler like this:
private void button1_Click(object sender, EventArgs e)
{
HelloService.HelloServiceClient helloServiceClient = new HelloService.HelloServiceClient("NetTcpBinding_IHelloService");
label1.Text = helloServiceClient.GetMessage(textBox1.Text); // This works
HelloClient.Hello foobar = (Hello)helloServiceClient.GetHello(); // This does not compile
}
I understand that the client Hello data class is different from the connected service Hello data class, hence the error. I'd still like to know how to pass the contents over since I made sure that the property names, data types and ordering match up. Aside from manually assigning these one by one, is there no way of passing the values? I couldn't quite figure out how to get the DataContractSerializer or DataContractResolver to do the job.
I figure that if the string classes can pass the value implicitly, there must be an efficient way of doing so with other reference types.
本文标签: cHow to deserializepass values from proxy service to data classStack Overflow
版权声明:本文标题:c# - How to deserializepass values from proxy service to data class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744627019a2616319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论